Click here to Skip to main content
15,886,518 members
Articles / Desktop Programming / MFC
Article

Drag & Drop Dialog

Rate me:
Please Sign up or sign in to vote.
3.00/5 (6 votes)
27 Mar 20022 min read 97K   3.3K   28   8
A dialog-class which you can drag around the screen by clicking anywhere on it.

Sample Image

Introduction

I've written a DialogClass which supports Drag & Drop.

First I will describe how you can add the Drag&Drop behaviour to an existing dialog - That's really simple

Copy the DragDialog.h and DragDialog.cpp to your project directory. For example, the name of my dialog class is CTestDragDlg. Include DragDialog.h in your StdAfx.h or in the .h and .cpp file of your dialog. Open the header file of your dialog and search for a line looking like this:

class CTestDragDlg : public CDialog
Change CDialog to CDragDialog, it should then look like this:
class CTestDragDlg : public CDragDialog

Now open the .cpp file of your dialog and search for a line looking like this:

CTestDragDlg::CTestDragDlg(CWnd* pParent /*=NULL*/)
	: CDialog(CTestDragDlg::IDD, pParent)
{
	//{{AFX_DATA_INIT(CTestDragDlg)
Simply replace CDialog with CDragDialog...

Now search the code for something look like this:

BEGIN_MESSAGE_MAP(CTestDragDlg, CDialog)
	//{{AFX_MSG_MAP(CTestDragDlg)
	ON_WM_SYSCOMMAND()
	ON_WM_PAINT()
	ON_WM_QUERYDRAGICON()
	//}}AFX_MSG_MAP
END_MESSAGE_MAP()
Replace CDialog with CDragDialog.

Now try to compile it!

If you get any errors with a precompiled header, go to the project options and say that you won't use precompiled headers.

Congratulations, you have now a Drag&Drop Dialog!

The Options

Now you can change the drag behaviour of your DragDialog, here are some examples.

  • Normally the dialog can be dragged with the left mouse button, if you want to use the right mouse button, call SetDragButtons(FALSE, TRUE); Now the right mouse button can drag the dialog around.

  • Call SetDragButtons(TRUE, FALSE); and the left mouse button is activated for dragging.

  • If the user shouldn't be allowed to drag the dialog out of sight (out of the visible screen). Call
    KeepDialogInScreen(TRUE);<br>
        <br>
  • If you want to get the dialog back on screen, cause user dragged it out or something like this, simply call GetDialogBackToScreen();.

  • Are you bored of the standard mouse cursor while dragging? No problem, simply call SetDragCursor(IDC_CURSOR_DRAG);. You must replace IDC_CURSOR_DRAG with the ID of your mouse cursor. And now, if you're scared of the new cursor, call UseDragCursor(FALSE);.

  • Here is a cool effect, if you want to have a 'look-through' effect on your dialog while dragging, call SetFadeWhileDragging(100), where the value, in this case 100, will regulate how much other windows shine through. If the value is 0 the dialog is completely invisible, if it is 255 or the parameter NOFADE the dialog will be painted normal while dragging.

Have a lot of fun with it!

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


Written By
Web Developer
Germany Germany
This member has not yet provided a Biography. Assume it's interesting and varied, and probably something to do with programming.

Comments and Discussions

 
Questiongrt thanxxxxxxxxxxxxx Pin
omkarpardeshi1235-May-12 0:24
omkarpardeshi1235-May-12 0:24 
GeneralGreate Pin
Mehdi_Minaee16-Jun-09 3:21
Mehdi_Minaee16-Jun-09 3:21 
GeneralVery cool Pin
alex.barylski14-Feb-04 14:05
alex.barylski14-Feb-04 14:05 
GeneralPrecompiled Headers Pin
skallestad13-Oct-03 16:05
skallestad13-Oct-03 16:05 
GeneralRe: Precompiled Headers Pin
Jochen Duell30-Oct-03 4:10
Jochen Duell30-Oct-03 4:10 
GeneralTwo simple solutions Pin
Thomas Freudenberg28-Mar-02 1:09
Thomas Freudenberg28-Mar-02 1:09 
GeneralRe: Two simple solutions Pin
muff9917-Jan-05 12:28
muff9917-Jan-05 12:28 
GeneralCaption bar Pin
PJ Arends27-Mar-02 23:22
professionalPJ Arends27-Mar-02 23:22 

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.