Currently, many software products support the drag and drop function. By dragging a file to a certain position in the software window, the software can respond accordingly. For example, if you drag a media file to its main window, Windows Media Player, a playback software that comes with windows, will play the file.
How can we make our software have this function?
In fact, it is not very difficult, as long as you need a drag-and-drop control to respond to the wm_dropfiles message. The following uses the Edit Control and ListBox control as examples.
First, inherit from cedit and customize an edit control. Add the following message response functions to the header file:
Afx_msg void ondropfiles (hdrop hdropinfo );
Then, add the following to the Message ing in the CPP file:
On_wm_dropfiles ()
Finally, add the ondropfiles function to the CPP file to process the dragged file. The complete code is as follows:
// Lxedit. h
Class clxedit: Public cedit
{
Declare_dynamic (clxedit)
Public:
Clxedit ();
Virtual ~ Clxedit ();
Afx_msg void ondropfiles (hdrop hdropinfo );
Protected:
Declare_message_map ()
};
// Lxedit. cpp
# Include "stdafx. H"
# Include "lxedit. H"
Implement_dynamic (clxedit, cedit)
Clxedit: clxedit ()
{}
Clxedit ::~ Clxedit ()
{}
Begin_message_map (clxedit, cedit)
On_wm_dropfiles ()
End_message_map ()
Void clxedit: ondropfiles (hdrop hdropinfo)
{
// File Name of the dragged object
Char szfilename [max_path + 1];
// Get the drag Object Name
Dragqueryfile (hdropinfo, 0, szfilename, max_path );
// Display the file name
Setwindowtext (szfilename );
Cedit: ondropfiles (hdropinfo );
}
As you can see, in the code above, the dragqueryfile function is used to drag the file name. This is an API function provided by windows to get the file name in a successful drag operation. The following is a prototype of the function:
// Parameter description:
// Hdrop handle, which is passed in by the message.
// Ifile file index.
// If the value of this parameter is 0 xffffffff, the function returns the number of files in the drag operation.
// If the value of this parameter is between 0 and the number of drag files, the function copies the file name of the corresponding index to the string specified by the parameter lpszfile.
// Lpszfile: string that stores the file name. If this parameter is null, the function returns the required String Length.
// The length of the string that stores the file name in CCH.
Uint dragqueryfile (hdrop, uint ifile, lptstr lpszfile, uint CCH );
From the descriptions of function parameters, we can see that this function supports multiple files to be selected at a time. Like Windows Media Player, you can drag multiple files to its window at a time to play the files in sequence. The following uses a ListBox with the drag and drop function as an example to describe how to drag multiple files at a time.
// Lxlistbox. h
Class clxlistbox: Public clistbox
{
Declare_dynamic (clxlistbox)
Public:
Clxlistbox ();
Virtual ~ Clxlistbox ();
Afx_msg void ondropfiles (hdrop hdropinfo );
Protected:
Declare_message_map ()
};
// Lxlistbox. cpp
# Include "stdafx. H"
# Include "lxlistbox. H"
Implement_dynamic (clxlistbox, clistbox)
Clxlistbox: clxlistbox ()
{}
Clxlistbox ::~ Clxlistbox ()
{}
Begin_message_map (clxlistbox, clistbox)
On_wm_dropfiles ()
End_message_map ()
Void clxlistbox: ondropfiles (hdrop hdropinfo)
{
Char szfilename [max_path];
Int ifilenumber;
// Obtain the number of objects in the drag-and-drop operation.
Ifilenumber = dragqueryfile (hdropinfo, 0 xffffffff, null, 0 );
For (INT I = 0; I <ifilenumber; I ++)
{
// Get each file name
Dragqueryfile (hdropinfo, I, szfilename, max_path );
If (findstring (0, szfilename )! = Lb_err)
Continue;
// Add the file name to the list
Addstring (szfilename );
}
Clistbox: ondropfiles (hdropinfo );
}
In addition, the file name obtained by the dragqueryfile function is a file name with a complete path.
Finally, let me remind you that you must set its accept files attribute to true if you want to drag your control! Otherwise, the code written above does not work!