Click here to Skip to main content
15,897,226 members
Articles / Desktop Programming / MFC
Tip/Trick

Drag and Drop in a Dialog

Rate me:
Please Sign up or sign in to vote.
4.79/5 (38 votes)
6 Dec 2012CPOL1 min read 145.5K   6.7K   46   24
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

C++
//
 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:

C++
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:

C++
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:

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

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

License

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


Written By
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 18:49
SajeeshCheviry4-Jan-13 18:49 
AnswerRe: I think you can post this article as a trick or tip. right? Pin
Jibesh14-Jan-13 13:18
professionalJibesh14-Jan-13 13:18 
GeneralRe: I think you can post this article as a trick or tip. right? Pin
SajeeshCheviry14-Jan-13 17:33
SajeeshCheviry14-Jan-13 17:33 
GeneralMy vote of 5 Pin
Matth Moestl13-Dec-12 0:49
professionalMatth Moestl13-Dec-12 0:49 
QuestionGreat Article Pin
JosephFox19-Feb-12 2:38
JosephFox19-Feb-12 2:38 
GeneralRe: Great Article Pin
Jibesh6-Dec-12 11:01
professionalJibesh6-Dec-12 11:01 
GeneralMy vote of 5 Pin
ThatsAlok7-Jul-11 21:16
ThatsAlok7-Jul-11 21:16 
Questionnice article Pin
ThatsAlok7-Jul-11 21:16
ThatsAlok7-Jul-11 21:16 
GeneralMy vote of 5 Pin
DemiG19-Jul-10 18:16
DemiG19-Jul-10 18:16 
GeneralDragAcceptFile Pin
Gernot Frisch17-Nov-08 23:06
Gernot Frisch17-Nov-08 23:06 
GeneralRe: DragAcceptFile Pin
Jibesh6-Dec-12 11:03
professionalJibesh6-Dec-12 11:03 
GeneralSolution: How to move your dialog by clicking on anywhere on the window in a Win32 API Non MFC App Pin
cffhgfdf22-Sep-06 1:34
cffhgfdf22-Sep-06 1:34 
Generalsimple, good, usable Pin
MGutmann11-Oct-04 20:21
MGutmann11-Oct-04 20:21 
GeneralI really like the simplicity Pin
hockeyman13-Sep-04 8:26
hockeyman13-Sep-04 8:26 
GeneralRe: I really like the simplicity Pin
hockeyman13-Sep-04 8:31
hockeyman13-Sep-04 8:31 
Generalgood job Pin
shaitan18@yahoo.com18-Feb-04 10:32
shaitan18@yahoo.com18-Feb-04 10:32 
QuestionWhom this article is targeted? Pin
super18-Nov-03 20:14
professionalsuper18-Nov-03 20:14 
AnswerRe: Whom this article is targeted? Pin
Jibesh18-Nov-03 20:35
professionalJibesh18-Nov-03 20:35 
GeneralRe: Whom this article is targeted? Pin
armentage19-Nov-03 3:50
armentage19-Nov-03 3:50 
GeneralRe: Whom this article is targeted? Pin
Jibesh19-Nov-03 4:20
professionalJibesh19-Nov-03 4:20 
QuestionHow the rating is found? Pin
kezhu18-Nov-03 12:18
kezhu18-Nov-03 12:18 
AnswerRe: How the rating is found? Pin
567890123419-Nov-03 6:44
567890123419-Nov-03 6:44 
Rating is counted proportionally to member status. It is described somewhere in CodeProject. For example, I am a gold member and my 5 changed the rating from 2.93 to 3.39.
I put 5 because I think this article is useful for somebody who needs this stuff. To my opinion, simple article is not bad article, and CodeProject is site both for experienced programmers and for beginners.

GeneralMissing File. Pin
WREY17-Nov-03 20:22
WREY17-Nov-03 20:22 
GeneralRe: Missing File. Pin
Jibesh17-Nov-03 20:34
professionalJibesh17-Nov-03 20:34 

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

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