Click here to Skip to main content
65,938 articles
CodeProject is changing. Read more.
Articles
(untagged)

Drag and Drop in a Dialog

0.00/5 (No votes)
6 Dec 2012 2  
Simple Drag and Drop functionality in a dialog.

Introduction

Here is a simple method to have a Drag and Drop feature in your dialog based applications. To provide Drag and Drop we have a Windows Message Handler called WM_DROPFILES. Handle this message through the ON_MESSAGE message map to capture the dropped files.

Message Handler

//
 BEGIN_MESSAGE_MAP(CComGuidFinderDlg, CDialog)
  //{{AFX_MSG_MAP(CComGuidFinderDlg)
  ON_WM_PAINT()
  ON_WM_LBUTTONDOWN()
  ON_MESSAGE(WM_DROPFILES,OnDropFiles)// Message Handler for Drang and Drop
  //}}AFX_MSG_MAP
END_MESSAGE_MAP()
//

Now it is time to handle the WM_DROPFILES messages through a user defined method. The function prototype should be like this:

LRESULT  OnDropFiles(WPARAM wParam,LPARAM lParam);

Finally we have a function to capture the drop events. wParam is a handle to the HDROP structure describing the dropped files. To get info about the dropped file, i.e., the file name used:

DragQueryFile(hDrop,    // Struture Identifier
        -1,        // -1 to Drop more than one file or ( integer 0 to max )
                   // to drop selected No of files
        szDroppedFile,// Droped File Name
        MAX_PATH);   // Max char

So now we have done all possible coding in our dialog based application to handle the Drag & Drop feature. But still it is handicapped. Handling the Drop event is not enough to ensure the Drag & Drop feature. We need to register our window to accept the dropped file using:

BOOL CComGuidFinderDlg::OnInitDialog()
{
  ......
  .....
  DragAcceptFiles(TRUE) // To Accept Dropped file Set this TRUE
}

Good... That's all, and we have done well!

I added one more feature in this sample, i.e., moving our dialog by clicking on anywhere on the window. This can be done by posting the WM_NCLBUTTONDOWN message to HTCAPTION handle this statement in OnLButtonDownMessage(...) .

PostMessage(WM_NCLBUTTONDOWN,HTCAPTION,MAKELPARAM(point.x,point.y))

License

This article has no explicit license attached to it but may contain usage terms in the article text or the download files themselves. If in doubt please contact the author via the discussion board below.

A list of licenses authors might use can be found here