Click here to Skip to main content
15,892,005 members
Articles / Desktop Programming / MFC
Article

A simple way to make a dialog “dragable” without using Title Bar

Rate me:
Please Sign up or sign in to vote.
2.00/5 (18 votes)
29 Mar 20041 min read 69.6K   20   11
A simple way to make a dialog “dragable” without using Title Bar

Introduction

I often make dialogs in my projects in random shapes. This will make the dialog’s title bar missing, or actually, I miss it in purpose (he he). By default, a dialog can only be moved by dragging the mouse on the title bar. But what will you do if you don’t have any title bar? This short article will show you how.

How It Works

If we don’t have any client windows in our dialog, we can simply use OnMouseMove function which will receive the WM_MOUSEMOVE from the system. But in this article, I assume that the dialog is filled with many controls, and because of that, the OnMouseMove function cannot be used.

Then, what’s the solution? Since usually, the cursor is within the dialog itself in order to move it, I chose to block the message before it reached the child. Then if the message has fulfill the condition (that is: mouse pressed, then mouse moved), the window position should change. So, we can then use the

PreTranslateMessage(..) 
to do this. Just filter the necessary message, do something with it, and it’s done.

Note: for some controls, the method I used is not very friendly. So, just modify it as you need. This can easily be done by retrieving the control’s rectangle, and do some necessary filters by checking the current cursor position (is it within the rectangle? Or not?) so that it won’t reach the main code (that will move the window).

A very simple code

Here is the code I used to make a dialog dragable.

BOOL CYourClassDlg::PreTranslateMessage(MSG* pMsg) 
{
     //The code starts here
     static bool mouse_down = false;
     static CRect MainRect;
     static HCURSOR cursor; // use this if you want the 
       // "dragging" cursor different from the normal one
     static CPoint point;
     switch(pMsg->message)
     {
     case WM_LBUTTONDOWN:
         //save current dialog’s rectangle
         GetWindowRect(&MainRect);
         //save current cursor coordinate
         point = pMsg->pt;
         ScreenToClient(&point);
         
         //change the sign
         mouse_down = true;
         cursor = ::AfxGetApp()->LoadCursor(IDC_CURSOR1);
         break;
     case WM_LBUTTONUP:
         //stop the sign
         mouse_down = false;
         //gimme a standard cursor now!!
         cursor = ::AfxGetApp()->LoadStandardCursor(IDC_ARROW);
         break;
     case WM_MOUSEMOVE :
         cursor = ::AfxGetApp()->LoadStandardCursor(IDC_ARROW);
         if(mouse_down)
         {     
              //if here, then the mouse is dragging
              cursor = ::AfxGetApp()->LoadCursor(IDC_CURSOR1);
              //finally, move the window
              MoveWindow(    pMsg->pt.x - point.x,
                  //count the relative position
                       pMsg->pt.y - point.y,
                       MainRect.Width(), 
                     //if the width doesn’t change 
                       MainRect.Height(),
                     //if the height doesn’t change
                       TRUE);
         }
     }
     SetCursor(cursor);
     //The code ends here
     return CDialog::PreTranslateMessage(pMsg);
}

That’s it. That’s the only code you need to add to your project. I Hope this is useful.

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
Business Analyst HSBC, Citi
Indonesia Indonesia
Multi Platform System Analyst, Application Developer, Database Designer, and Project Manager in a wide variety of Business Applications and Industrial Automations.

Experienced for in-depth data analysis, data warehousing, reporting, and actively involve in supporting the business growth.

Comments and Discussions

 
QuestionHow to move window with no border using picture control Pin
Premnath Mali9-Mar-17 20:51
professionalPremnath Mali9-Mar-17 20:51 
Like I have one picture control with a image which is wide as long as width of dialog

and with this code also if I drag mouse over control still it is moving I want to avoid that also

How do I do that?
Generaleasily Pin
X - Man30-Jan-07 6:04
X - Man30-Jan-07 6:04 
GeneralRe: easily Pin
JCSF11-Jan-15 6:40
JCSF11-Jan-15 6:40 
GeneralFast Dragging Pin
Christopher J. Holland10-Dec-05 13:44
Christopher J. Holland10-Dec-05 13:44 
GeneralAn easier way Pin
Luis Alonso Ramos30-Mar-04 3:11
Luis Alonso Ramos30-Mar-04 3:11 
GeneralRe: An easier way Pin
Jim A. Johnson30-Mar-04 13:33
Jim A. Johnson30-Mar-04 13:33 
GeneralRe: An easier way Pin
Anonymous30-Mar-04 13:59
Anonymous30-Mar-04 13:59 
GeneralRe: An easier way Pin
Luis Alonso Ramos30-Mar-04 20:38
Luis Alonso Ramos30-Mar-04 20:38 
GeneralRe: An easier way Pin
quangtin317-Oct-05 17:47
quangtin317-Oct-05 17:47 
GeneralRe: An easier way Pin
Tim Smith30-Mar-04 16:21
Tim Smith30-Mar-04 16:21 
GeneralRe: An easier way Pin
Vardis29-Dec-05 12:50
Vardis29-Dec-05 12:50 

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.