65.9K
CodeProject is changing. Read more.
Home

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

starIconstarIconemptyStarIconemptyStarIconemptyStarIcon

2.00/5 (13 votes)

Mar 30, 2004

1 min read

viewsIcon

70291

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.