Use MFC to drag and drop files to ListBox
I. Dialog Box project:
1. Create a dialog box program and select the accept file option in the extend Style dialog box.
2. Add a ListBox control in the dialog box. The ID is idc_dialog_list and sort style is removed.
3. manually add the wm_dropfiles message response, which is similar to the custom message, but it is simpler, because you do not need to define wm_dropfiles here;
(1) Declare the message processing function in the dialog. h file:
Afx_msg void ondropfiles (hdrop hdropinfo );
(2) In the message ing of dialog. cpp, point out the message processing entry:
On_message (wm_dropfiles, ondropfiles)
(It can also be macro on_wm_dropfiles ());
(3) Finally define the Message Processing Function
Void clianxidlg: ondropfiles (hdrop hdropinfo)
{
Int nfilecount = 0;
Cstring strcount;
// Obtain the number of dragged files
Nfilecount =: dragqueryfile (hdropinfo, 0 xffffffff, null, 512 );
Strcount. Format ("you have selected % d Files", nfilecount );
: Afxmessagebox (strcount );
// For (I = 0; I <PRE>
For (INT I = 0; I <nfilecount; I ++)
{
// Obtain the bytes occupied by the I-th drag file name
Int namesize = dragqueryfile (hdropinfo, I, null, 0 );
Handle hheap = getprocessheap ();
// Allocate a buffer based on the number of bytes
Char * pname = (lpstr) heapalloc (hheap, heap_zero_memory, namesize ++ );
If (pname = NULL)
{
MessageBox ("An error occurred while allocating temporary storage space to file names! "," Error message ", mb_iconerror );
Return;
}
// Copy the file name to the buffer
Dragqueryfile (hdropinfo, I, pname, namesize );
// Add the file name to ListBox for display
M_list.addstring (pname );
// Release the buffer
Heapfree (hheap, heap_zero_memory, pname );
}
Cdialog: ondropfiles (hdropinfo );
}
II.SDIProject
Based on the Implementation of cview, ListBox is used to accept the dragged file name, which also facilitates the demonstration of the drag of multiple files.
1. Drag and Drop the file to the ListBox in cview.
(1) use MFC Appwizard to create an SDI-based new project named drag. The default settings are used for all projects except for the first step.
(2) dragview. h file
Clistbox m_list;
(3) dragview. cpp File
Use classwizard to add the wm_create message response, and then add the following code in the oncreate function:
Dragacceptfiles (); // register yourself with file manager,
Make yourself accept droped file (s)
Crect rect (, 80 );
M_list.create (ws_visible | ws_vscroll, rect, this, idc_view_list); // create a ListBox
4. Resource. h
Modify the resource. h file and assign a value to the ListBox ID (idc_view_list ).
Example: # define idc_view_list 1000
5. dragview. cpp File
Use classwizard to add the wm_size message response, and then add the following code to the onsize function:
M_list.movewindow (, CX, CY); // keep the area occupied by ListBox as large as the customer area
6. dragview. cpp File
Use classwizard to add the wm_dropfiles message response, and then add the following code in the ondropfiles function:
Void cdragview: ondropfiles (hdrop hdropinfo)
{
Int nfilecount = 0;
Cstring strcount;
// Obtain the number of dragged files
Nfilecount =: dragqueryfile (hdropinfo, 0 xffffffff, null, 512 );
Strcount. Format ("you have selected % d Files", nfilecount );
: Afxmessagebox (strcount );
// For (I = 0; I <PRE>
For (INT I = 0; I <nfilecount; I ++)
{
// Obtain the bytes occupied by the I-th drag file name
Int namesize = dragqueryfile (hdropinfo, I, null, 0 );
Handle hheap = getprocessheap ();
// Allocate a buffer based on the number of bytes
Char * pname = (lpstr) heapalloc (hheap, heap_zero_memory, namesize ++ );
If (pname = NULL)
{
MessageBox ("An error occurred while allocating temporary storage space to file names! "," Error message ", mb_iconerror );
Return;
}
// Copy the file name to the buffer
Dragqueryfile (hdropinfo, I, pname, namesize );
// Add the file name to ListBox for display
M_list.addstring (pname );
// Release the buffer
Heapfree (hheap, heap_zero_memory, pname );
}
Cview: ondropfiles (hdropinfo );
}