Not long ago, I saw Huang Yingming, Huang Yingming, Huang, Nanjing Navy Command.
With the MFC implementation of the document to the edit box, "I am very interested in the final question, I checked the VC's MSDN help, found that DragacceptFiles () is also a member function of the CWND class, which is that, as long as it is The derived class of the CWND class should be able to register with the Windows File Manager to become the recipient of the Drag file manager, which means that I can respond to WM_Dropfiles, so I use AppWizard to generate an SDI-based project. Document Drag, and add a dialog CDRAGDIALOG, then I will use ClassWizard to see if CWnd derived classes such as CMAINFRAME, CDRAGVIEW, and CDRagDialog have WM_DROPFILES messages, and the result is not, but when settings Dialog's Extended Style. Obviously, I saw that there is an Accept File option. This is getting strange, how can I not think about this? Later, I didn't want to think again, since I can't use ClassWizard to add a response to WM_DropFiles messages, why not use manual addition to add After the manual addition, then compile, debug, run, there is no problem. Although the program is written, I still haven't clear.
---- Huang Yingming Mission explained the implementation of CFormView and Dialog, and I will briefly introduce the implementation based on CVIEW, and another implementation based on Dialog. In order to facilitate the performance of the program, there is no edit box in the program, and It is ListBox to accept the dominant file name, which is also easy to demonstrate the drag of multiple files.
---- One, file drag and drop to the listbox in CVIEW
---- 1, create an SDI-based new project with MFC AppWizard, named DRAG, in addition to the first step, the rest is used by default.
---- 2, Dragview.h file
Clistbox m_view_list;
---- 3, Dragview.cpp file
---- Add a WM_CREATE message response with ClassWizard and add the following code in the oncreate function:
DragacceptFiles (); // Register yourself to File Manager,
Enable yourself to accept DROPED FILE (s)
CRECTRECT (0, 0, 100, 80); m_view_list.create
(WS_Visible | WS_VSCROLL, RECT, THIS,
IDC_View_list); // Create a listbox
---- 4, Resource.h
---- Modify the resource.h file, assign a value for the ListBox ID (IDC_VIEW_LIST)
---- For example: #define IDC_View_list 1000
---- 5, Dragview.cpp file
---- Add a WM_SIZE message response with ClassWizard and add the following code in the onsize function:
---- m_view_list.movewindow (0, 0, cx, cy); // Make the local area of ListBox always remain as large as the customer area
---- 6, Dragview.cpp file
---- Add a WM_DROPFILES message response with ClassWizard and add the following code in the onDropfiles function:
Void CDRAGVIEW :: Ondropfiles (HDROP HDROPINFO)
{
INT DROPCOUNT = DragQueryFile (HDropinfo, -1, NULL, 0); // Number of dragged files
For (int i = 0; i { INT namesize = DragQueryFile (HDropinfo, I, NULL, 0); // Number of items accounting by Item I Dragings Handle hheap = getProcessHeap (); Char * PNAME = (LPSTR) Heapalloc (hheap, heap_zero_memory, NameSize ); / / / / According to the byte number of buffers IF (PNAME == NULL) { MessageBox ("Error when allocating the file name!", "Error Information", MB_ICONERROR); Return; } DragQueryfile (HDropinfo, I, PNAME, NameSize); // Copy the file name to the buffer m_view_list.addstring (PNAME); // File Name Add to listbox HeapFree (hheap, heap_zero_memory, pname); // Release buffer } CView :: Ondropfiles (HDropInfo); } ---- When you finish the document drag and drop to the listbox in View, you can compile and run. ---- Second, the file drag and drop to the listbox in the dialog ---- 1, the DRAG engineering generated on the upper side adds a dialog box, IDD_DRAG_DIALOG, generate a new class CDRagDialog, select the Accept File option in Extend Style. ---- 2, add a listbox control in the dialog box, ID is IDC_DIALOG_LIST, remove Sort Style. ---- 3, manually add a WM_DropFiles message response, this is similar to the custom message, but it is simpler because there is no need here. ---- You define wm_dropfiles; ---- (1) Declaration Message Processing Function in DragDialog.h file: AFX_MSG Void OnDropfiles (HDROP HDROPInfo); ---- (2) Secondly, in the message map of DragDialog.cpp, the message processing entry is pointed out: ON_MESSAGE (WM_DROPFILES, OONFILES) (Or macro ON_WM_DROPFILES ()); ---- (3) Finally define the message processing function, which is similar to the sixth step in CVIEW: Void CDRAGDIALOG :: OnDropfiles (HDrop HDropinfo) { INT DROPCOUNT = DragQueryFile (HDropinfo, -1, NULL, 0); // Number of draws to be dragged For (int i = 0; i { INT namesize = DragQueryFile (HDropinfo, I, NULL, 0); // Number of items accounting by Item I Dragings Handle hheap = getProcessHeap (); Char * pname = (lpstr) Heapalloc (hheap, heap_zero_memory, NameSize ); / / / / According to the byte number of buffers IF (PNAME == NULL) { MessageBox ("Error when allocating the file name!", "Error Information", MB_ICONERROR); Return; } DragQueryfile (HDropinfo, I, PNAME, NameSize); // Copy the file name to the buffer m_dialog_list.addstring (PNAME); // File Name Add to listbox HeapFree (hheap, heap_zero_memory, pname); // Release buffer } CDIALOG :: Ondropfiles (HDropInfo); } ---- 4, modify the MENU from the resource, add a menu "test" to pop up the dialog box we just added, the menu ID is ID_DRAG_TEST; ---- 5, add the dialog header file in Mainfrm.cpp, and add ID_DRAG_TEST response functions with ClassWizard; #include "DragDialog.h" ...... Void cmainframe :: ondragtest () { CDRAGDIALOG DLG; Dlg.domodal (); }