1. Use MFC Appwizard to create an SDI-based new project named drag. In addition to the first step, use the default settings for all other projects.
2. dragview. h file
CListBox m_View_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 so that you can accept droped file (s) crect rect (,); m_view_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_view_list.movewindow (0, 0, CX, CY); // keep the region occupied by ListBox as large as the customer Zone
6. dragview. cpp File
---- Use classwizard to add the wm_dropfiles message response, and then add the following code in the ondropfiles function:
Void cmydragview: ondropfiles (hdrop hdropinfo) {// todo: add your message handler code here and/or call default int dropcount = dragqueryfile (hdropinfo,-1, null, 0 ); // obtain the number of dragged files for (INT I = 0; I <dropcount; I ++) {// get 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 = new char [++ namesize]; char * pname = (lpstr) heapalloc (hheap, heap_zero_memory, namesize ++ );
If (pname = NULL) {MessageBox ("An error occurred while allocating a temporary space to the file name! "," Error message ", mb_iconerror); return;} dragqueryfile (hdropinfo, I, pname, namesize); // copy the file name to the buffer m_view_list.addstring (pname ); // Add the file name to ListBox. // Delete [] pname; // release the buffer heapfree (hheap, heap_zero_memory, pname );}
Dragfinish (hdropinfo); // The memory is released after the drag and drop operation is complete.
CView::OnDropFiles(hDropInfo);}
---- At this point, the file is dragged to the ListBox in the view. You can compile and run the file.