Click here to Skip to main content
Licence 
First Posted 29 Mar 2004
Views 48,557
Bookmarked 17 times

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

By | 29 Mar 2004 | Article
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

About the Author

Aris Adrianto S

Business Analyst
HSBC, Citi
Indonesia Indonesia

Member

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.

Sign Up to vote   Poor Excellent
Add a reason or comment to your vote: x
Votes of 3 or less require a comment

Comments and Discussions

 
You must Sign In to use this message board. (secure sign-in)
 
Search this forum  
 FAQ
    Noise  Layout  Per page   
  Refresh
Generaleasily PinmemberX - Man6:04 30 Jan '07  
GeneralFast Dragging PinmemberChristopher J. Holland13:44 10 Dec '05  
GeneralAn easier way PinmemberLuis Alonso Ramos3:11 30 Mar '04  
GeneralRe: An easier way PinmemberJim A. Johnson13:33 30 Mar '04  
GeneralRe: An easier way PinsussAnonymous13:59 30 Mar '04  
GeneralRe: An easier way PinmemberLuis Alonso Ramos20:38 30 Mar '04  
GeneralRe: An easier way Pinmemberquangtin317:47 17 Oct '05  
GeneralRe: An easier way PinmemberTim Smith16:21 30 Mar '04  
GeneralRe: An easier way PinmemberVardis12:50 29 Dec '05  

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.

Permalink | Advertise | Privacy | Mobile
Web04 | 2.5.120517.1 | Last Updated 30 Mar 2004
Article Copyright 2004 by Aris Adrianto S
Everything else Copyright © CodeProject, 1999-2012
Terms of Use
Layout: fixed | fluid