A simple method for realizing file drag and drop

xiaoxiao2021-03-06  65

1. Self-prepared class, complicated, maintenance, trouble; 2, you must drag the file to the editing control in the dialog, the file name dragged is accepted, and the standard Windows program only needs to drag the file. Go to the dialog box to be accepted; 3, only one drag and drop file can be accepted. This method you want to introduce this method only needs to call a Windows API function DragQueryFile to realize the drag and drop operation of the file, and completely overcome the above 3 points. Let's first introduce this function.

---- DragQueryFile prototype:

Uint Dragqueryfile (HDROP HDROP, UINT IFILE, LPTSTSTSZZFILE, UINT CCH)

---- HDROP is a handle to drag the file structure;

---- ifile is the serial number of the hosted file starting with 0 because it may be dragged multiple files at a time. When this parameter

--- Set to 0xfffffffff, the function returns the number of drag files;

---- lpszfile is a buffer to the file name;

---- CCH is the size of the file name buffer, namely the number of file names.

---- After the parameters of the function are clarified, the programming is a very simple thing. The following is given, and a dialog box will pop up after running, you can drag any number of files or directories to the above, after release the mouse, the program first displays the number of drag and drop files, and then drag and drop the file name Displayed in a List box control. Specific steps are as follows:

Establish a dialog-based engineering DROP, then other options are all default. Select the Accept Files property of Extended Styles in the IDD_DROP_DIALOG dialog. Add a list box control idc_list1 to the IDD_DROP_DIALOG dialog. Add a member variable m_list to the CDROPDLG class in ClassWizard, type CLISTBOX. Add a function that handles Windows message WM_DROPFILES in the CDROPDLG class, accept the default name ondropfiles given by the system, then enter the following code:

void CDropDlg :: OnDropFiles (HDROP hDropInfo) {char * lpszFileName = new char [512], cFileCount [10]; int nFileCount, i; nFileCount = :: DragQueryFile (hDropInfo, 0xFFFFFFFF, NULL, 512); :: AfxMessageBox (itoa (nfilecount, cfilecount, 10)); for (i = 0; i

---- Note :: Dragfinish (HDropInfo); statement is essential, it is used to release Windows to process the memory to handle the file.

---- Run this program You can see that the file that dragged as long as you enter the boundary of the dialog, the mouse will now be " ", release the mouse on any position on the dialog, and the files dragged will be accepted. It is completely solved "Drag and drop to the edit box" with the MFC implementation file "must be dragged to the shortage of the edit box; and dragging is achieved by calling the Windows API function, so the program is very strong, there is little maintenance. The program is passed in IndoWS98, VC 5.0.

转载请注明原文地址:https://www.9cbs.com/read-87120.html

New Post(0)