天下

记录修行的印记

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 天下 阅读(295) 评论(0)  编辑 收藏 引用 所属分类: Win32WTL


只有注册用户登录后才能发表评论。
【推荐】超50万行VC++源码: 大型组态工控、电力仿真CAD与GIS源码库
网站导航: 博客园   IT新闻   BlogJava   知识库   博问   管理


<2012年2月>
2930311234
567891011
12131415161718
19202122232425
26272829123
45678910

导航

统计

常用链接

留言簿(3)

随笔分类(369)

随笔档案(320)

链接

最新随笔

搜索

最新评论