Click here to Skip to main content
15,885,985 members
Articles / Desktop Programming / WTL
Article

How to move a dialog which does not have a caption

Rate me:
Please Sign up or sign in to vote.
3.41/5 (10 votes)
21 Apr 2007CPOL 54.6K   1   23   10
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)


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

Comments and Discussions

 
Generaldrag to where the dialog's title bar is way off screen Pin
cameos18-Sep-09 14:31
cameos18-Sep-09 14:31 
Question? Doesn’t Work for Right Mouse Button (System Menu) Pin
Synetech10-Feb-09 11:43
Synetech10-Feb-09 11:43 
GeneralNice Pin
David Nash21-Dec-08 21:44
David Nash21-Dec-08 21:44 
GeneralRe: Nice Pin
Igor Vigdorchik22-Dec-08 14:13
Igor Vigdorchik22-Dec-08 14:13 
GeneralThe correct way to drag dialog Pin
T800G20-Jun-08 8:49
T800G20-Jun-08 8:49 
GeneralRe: The correct way to drag dialog Pin
Igor Vigdorchik22-Jun-08 13:57
Igor Vigdorchik22-Jun-08 13:57 
GeneralWhy? Its nonsens! Pin
Devil for ever15-Apr-07 0:53
Devil for ever15-Apr-07 0:53 
GeneralRe: Why? Its nonsens! Pin
Igor Vigdorchik15-Apr-07 4:51
Igor Vigdorchik15-Apr-07 4:51 
GeneralRe: Why? Its nonsens! Pin
Devil for ever16-Apr-07 2:33
Devil for ever16-Apr-07 2:33 
GeneralRe: Why? Its nonsens! Pin
Igor Vigdorchik16-Apr-07 9:59
Igor Vigdorchik16-Apr-07 9:59 

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.