天下

记录修行的印记

How to move a dialog which does not have a caption(非标题栏移动对话框)

How to move a dialog which does not have a caption
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
|HTCAPTION);
    
return 0;
}


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;
}

//For MFC
//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);
}

posted on 2012-02-16 10:24 天下 阅读(474) 评论(0)  编辑 收藏 引用 所属分类: Win32WTL


只有注册用户登录后才能发表评论。
网站导航: 博客园   IT新闻   BlogJava   博问   Chat2DB   管理


<2013年3月>
242526272812
3456789
10111213141516
17181920212223
24252627282930
31123456

导航

统计

常用链接

留言簿(4)

随笔分类(378)

随笔档案(329)

链接

最新随笔

搜索

最新评论