Open the Windows Explorer, pick a HTML file, drag it into the Netscape browser and.. what happens? The file will be opened. If you want to do the same with your application then you're right here. I will show you how to do this using a dialog based MFC project.
First thing to do is to make your main window accepts dragged and dropped files. In a dialog based project, this is done best in CYourDialog::OnInitDialog(). Here's the code:
BOOL CYourDialog::OnInitDialog() { //Other things that are done..... //.. //.. //App is now ready for dragged files.. DragAcceptFiles(); return TRUE; }
If you want to do this in a SDI/MDI project use CMainFrame::OnCreate(..). Next thing to do is to handle the Window message WM_DROPFILES. In VC++, right click on the window class - dialog or the SDI/MDI mainwindow - and click on something like Add Window Message Handler.
We will you use the following function to get info about the dropped files:
UINT DragQueryFile(HDROP hDrop, UINT iFile, LPTSTR lpszFile, UINT cch );
- hDrop: information about this "Drop-action". Parameter of WM_DROPFILES.
- iFile: index of the file that should be stored in lpszFile. 0 is the first file, 1 the second.. If iFile is 0xFFFFFFFF, the number of dragged files will be returned.
- lpszFile: pointer to a char-array that will hold the file specified by iFile.
- cch: Size of the char-buffer lpszFile.
An example in action: All files dragged to your diaog box will be opened:
void CYourDialog::OnDropFiles(HDROP dropInfo) { //char-Array that holds the filename char buffer[512]; //Get Count of dragged files int count = DragQueryFile(hDrop,0xFFFFFFFF,0,0); //Enumerate throught dragged files and open //one after another (you can also do many other things with them;) for (int i = 0; i < count; i += 1) { //Store the current file in buffer DragQueryFile(dropInfo,i,buffer,512); //Call userdefined function Open(..) with the current file Open(buffer); } }
That's it! There isn't much to comment about this code. It's really easy to do that. However if you have problems or questions, m@il me. Happy Coding!