Click here to Skip to main content
11,415,390 members (78,384 online)
Click here to Skip to main content

Drag and Drop in a Dialog

, 6 Dec 2012 CPOL
Rate this:
Please Sign up or sign in to vote.
Simple Drag and Drop functionality in a dialog.


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)
  ON_MESSAGE(WM_DROPFILES,OnDropFiles)// Message Handler for Drang and Drop

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



This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)


About the Author

Technical Lead
United States United States
a hard core developer

Comments and Discussions

QuestionI think you can post this article as a trick or tip. right? Pin
SajeeshCheviry4-Jan-13 19:49
memberSajeeshCheviry4-Jan-13 19:49 
AnswerRe: I think you can post this article as a trick or tip. right? Pin
jibesh14-Jan-13 14:18
memberjibesh14-Jan-13 14:18 
GeneralRe: I think you can post this article as a trick or tip. right? Pin
SajeeshCheviry14-Jan-13 18:33
memberSajeeshCheviry14-Jan-13 18:33 
GeneralMy vote of 5 Pin
justdownloads13-Dec-12 1:49
memberjustdownloads13-Dec-12 1:49 
QuestionGreat Article Pin
JosephFox19-Feb-12 3:38
memberJosephFox19-Feb-12 3:38 
GeneralRe: Great Article Pin
jibesh6-Dec-12 12:01
memberjibesh6-Dec-12 12:01 
GeneralMy vote of 5 Pin
ThatsAlok 7-Jul-11 22:16
member ThatsAlok 7-Jul-11 22:16 
Questionnice article Pin
ThatsAlok 7-Jul-11 22:16
member ThatsAlok 7-Jul-11 22:16 
GeneralMy vote of 5 Pin
DemiG19-Jul-10 19:16
memberDemiG19-Jul-10 19:16 
GeneralDragAcceptFile Pin
Gernot Frisch18-Nov-08 0:06
memberGernot Frisch18-Nov-08 0:06 
It's DragAcceptFiles(TRUE);

Maybe one can fix that typo?

In the "OnDropFiles" function, return 0.

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.

| Advertise | Privacy | Terms of Use | Mobile
Web02 | 2.8.150427.4 | Last Updated 6 Dec 2012
Article Copyright 2003 by Jibesh
Everything else Copyright © CodeProject, 1999-2015
Layout: fixed | fluid