Click here to Skip to main content
6,629,377 members and growing! (24,194 online)
Email Password   helpLost your password?
Desktop Development » Dialogs and Windows » Dialogs     Intermediate License: The Code Project Open License (CPOL)

How to move a dialog which does not have a caption

By Igor Vigdorchik

Two ways to move a dialog by dragging its client area.
VC8.0Win2K, WinXP, Win2003, Vista, WTL, VS2005, Dev
Posted:13 Apr 2007
Updated:21 Apr 2007
Views:15,614
Bookmarked:14 times
Announcements
Loading...
 
Search    
Advanced Search
Add to IE Search
printPrint   add Share
      Discuss Discuss   Broken Article?Report  
9 votes for this article.
Popularity: 3.21 Rating: 3.37 out of 5
1 vote, 11.1%
1
2 votes, 22.2%
2

3
2 votes, 22.2%
4
4 votes, 44.4%
5

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


Member

Occupation: Web Developer
Location: United States United States

Other popular Dialogs and Windows articles:

Article Top
You must Sign In to use this message board.
FAQ FAQ 
 
Noise Tolerance  Layout  Per page   
 Msgs 1 to 10 of 10 (Total in Forum: 10) (Refresh)FirstPrevNext
Generaldrag to where the dialog's title bar is way off screen Pinmembercameos15:31 18 Sep '09  
Question? Doesn’t Work for Right Mouse Button (System Menu) PinmemberSynetech12:43 10 Feb '09  
GeneralNice PinmemberDavid Nash22:44 21 Dec '08  
GeneralRe: Nice PinmemberIgor Vigdorchik15:13 22 Dec '08  
GeneralThe correct way to drag dialog PinmemberT800G9:49 20 Jun '08  
GeneralRe: The correct way to drag dialog PinmemberIgor Vigdorchik14:57 22 Jun '08  
GeneralWhy? Its nonsens! PinmemberDevil for ever1:53 15 Apr '07  
GeneralRe: Why? Its nonsens! PinmemberIgor Vigdorchik5:51 15 Apr '07  
GeneralRe: Why? Its nonsens! PinmemberDevil for ever3:33 16 Apr '07  
GeneralRe: Why? Its nonsens! PinmemberIgor Vigdorchik10:59 16 Apr '07  

General General    News News    Question Question    Answer Answer    Joke Joke    Rant Rant    Admin Admin   

PermaLink | Privacy | Terms of Use
Last Updated: 21 Apr 2007
Editor: Smitha Vijayan
Copyright 2007 by Igor Vigdorchik
Everything else Copyright © CodeProject, 1999-2009
Web19 | Advertise on the Code Project