A simple method for implementing file drag and drop
The method described in this article only requires you to call a Windows API function dragqueryfile to drag and drop files, and completely overcome the above three shortcomings. The following describes the function.
---- The prototype of dragqueryfile is:
Uint dragqueryfile (hdrop, uint ifile, lptstr lpszfile, uintcch)
---- Hdrop is the handle pointing to the drag file structure;
---- Ifile is the serial number of the file to be moved from 0, because multiple files may be dragged at a time. When this parameter
---- If it is set to 0 xffffffff, the function returns the number of files to be dragged;
---- Lpszfile is the buffer pointing to the file name;
---- CCH indicates the size of the file name buffer, that is, the number of characters in the file name.
---- After clarifying the parameters of the function, programming is very simple. The following is a complete example,
After the program runs, a dialog box is displayed. You can drag any number of files or directories to it,
After you release the mouse, the program first displays the number of drag-and-drop files, and then displays all the drag-and-drop file names in a list box control.
The procedure is as follows:
Create a project drop based on the dialog box, and use the default values for all other options.
Select the accept files attribute of extended styles in the idd_drop_dialog dialog box.
Add a list box control idc_list1 to the idd_drop_dialog dialog box.
Add the member variable m_list to the cdropdlg class in classwizard, And the type is clistbox.
Add the wm_dropfiles function to the cdropdlg class to process Windows messages,
Accept the default name ondropfiles given by the system, and enter the following code:
Void cdropdlg: ondropfiles (hdrop hdropinfo)
{Char * lpszfilename = new char [512], cfilecount [10];
Int nfilecount, I;
Nfilecount =: dragqueryfile
(Hdropinfo, 0 xffffffff, null, 512 );
: Afxmessagebox (ITOA (nfilecount, cfilecount, 10 ));
For (I = 0; I <nfilecount; I ++)
{
Uint nchars =: dragqueryfile
(Hdropinfo, I, & lpszfilename [0], 512 );
Cstring STR (& lpszfilename [0], nchars );
M_list.addstring (STR); // For cedit, use m_edit1.setwindowtext (STR );
}
: Dragfinish (hdropinfo); // release the memory.
I = 0;
Delete [] lpszfilename;
// Cdialog: ondropfiles (hdropinfo); comment out this statement
}
---- Note: In the program: dragfinish (hdropinfo); the statement is indispensable and is used to release the memory allocated by windows for processing file drag and drop.
In the single document, add three lines of code before the initinstance () function of the APP returns the function.
M_pmainwnd-> dragacceptfiles ();
Enableshellopen ();
Registershellfiletypes (true );
Then, add the ondropfiles (hdrop hdropinfo) function to mainframe)
Void cmainframe: ondropfiles (hdrop hdropinfo)
{
Char * lpszfilename = new char [512];
Int nfilecount;
Uint nchars;
Nfilecount =: dragqueryfile
(Hdropinfo, 0 xffffffff, null, 512 );
If (nfilecount> 1) // number of files
{
MessageBox ("only one file can be opened at a time! ");
// Return;
}
Else
{
Nchars =: dragqueryfile (hdropinfo, 0, lpszfilename, 512 );
Cstring filepath (lpszfilename, nchars );
M_firstview-> openfile (filepath );
}
: Dragfinish (hdropinfo); // release the memory.
Delete [] lpszfilename;
}