Code Snippets
What's That?
I am using it actively
I planning to use/learn it
I don't plan to lean/use it
5th option
Home :: Programming :: Code Snippets :: MfcWin :: Drag & Drop Of Files

[ Drag & Drop Of Files ]

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..
  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 );

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
      //Call userdefined function Open(..) with the current file

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!

 Last edited 2002-12-08 00:45:23 by André Stein - printable version
» copyright by andré stein
» using stCM v1.0
» steinsoft.net revision 5.0