Click here to Skip to main content
Licence CPOL
First Posted 13 Apr 2007
Views 25,542
Bookmarked 21 times

How to move a dialog which does not have a caption

By | 21 Apr 2007 | Article
Two ways to move a dialog by dragging its client area.

Introduction

This article is aimed at beginners, and presents two ways to move a dialog which does not have a caption by dragging its client area.

1. WM_SYSCOMMAND message

Sending the WM_SYSCOMMAND message starts the move operation. Add the following code to handle the mouse down event:

BEGIN_MSG_MAP(CMainDlg)
    ...
    MESSAGE_HANDLER(WM_LBUTTONDOWN, OnLButtonDown)
END_MSG_MAP()

LRESULT OnLButtonDown(UINT uMsg, WPARAM wParam, LPARAM lParam, BOOL& bHandled)
{
    SendMessage(WM_SYSCOMMAND, SC_MOVE|0x0002);
    return 0;
}

One note though: specifying just SC_MOVE in a WM_SYSCOMMAND message tells Windows that you are moving the dialog by using the keyboard. To indicate that you want to move the dialog by using the mouse, you must specify SC_MOVE|0x0002.

2. WM_NCHITTEST message

The idea is to handle the WM_NCHITTEST message to return HTCAPTION instead of HTCLIENT when the mouse is in the client area, to trick Windows to start moving the dialog.

BEGIN_MSG_MAP(CMainDlg)
    ...
    MESSAGE_HANDLER(WM_NCHITTEST, OnNcHitTest)
END_MSG_MAP()

LRESULT OnNcHitTest(UINT uMsg, WPARAM wParam, LPARAM lParam, BOOL& bHandled)
{
    if (::DefWindowProc(m_hWnd, uMsg, wParam, lParam) == 
        HTCLIENT && ::GetAsyncKeyState(MK_LBUTTON) < 0)
      return HTCAPTION;

    return 0;
}

Devil for ever supplied the MFC solution that is shown below (thanks!). The idea is the same - to handle the WM_NCHITTEST message.

UINT OnNcHitTest(CPoint point)
{
    UINT nHit = CDialog::OnNcHitTest(point);
    return (nHit == HTCLIENT ? HTCAPTION : nHit);
}

License

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

About the Author

Igor Vigdorchik

Web Developer

United States United States

Member



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
Generaldrag to where the dialog's title bar is way off screen Pinmembercameos14:31 18 Sep '09  
Question? Doesn’t Work for Right Mouse Button (System Menu) PinmemberSynetech11:43 10 Feb '09  
This trick does work to move the dialog by dragging the client area, but it does not work to bring up the system menu. It is confusing why it does not work since Windows should be thinking that you are right-clicking in the title bar (which does bring up the menu, but not when right-clicking in the client even though we’re passing back HTCAPTION).
 
--
Synetech

GeneralNice PinmemberDavid Nash21:44 21 Dec '08  
GeneralRe: Nice PinmemberIgor Vigdorchik14:13 22 Dec '08  
GeneralThe correct way to drag dialog PinmemberT800G8:49 20 Jun '08  
GeneralRe: The correct way to drag dialog PinmemberIgor Vigdorchik13:57 22 Jun '08  
GeneralWhy? Its nonsens! PinmemberDevil for ever0:53 15 Apr '07  
GeneralRe: Why? Its nonsens! PinmemberIgor Vigdorchik4:51 15 Apr '07  
GeneralRe: Why? Its nonsens! PinmemberDevil for ever2:33 16 Apr '07  
GeneralRe: Why? Its nonsens! PinmemberIgor Vigdorchik9:59 16 Apr '07  

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
Web01 | 2.5.120604.1 | Last Updated 21 Apr 2007
Article Copyright 2007 by Igor Vigdorchik
Everything else Copyright © CodeProject, 1999-2012
Terms of Use
Layout: fixed | fluid