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:
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:
- 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:
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!