禁止窗口移动----API及MFC实现方法及原理
2014-11-05 15:00
786 查看
原文地址::http://blog.163.com/jiang891203@126/blog/static/8704411820111013102351582/
相关文章
1、如何禁止拖动窗口?----http://bbs.csdn.net/topics/290020376
2、对话框有标题栏,怎么禁止鼠标拖动窗口----http://bbs.csdn.net/topics/350059939
3、MFC对话框禁止移动 使标题栏不响应鼠标消息 通过客户区移动窗体 OnNcHitTest()的实现----/article/1536733.html
一、C API实现方法
思路:如果用户想通过鼠标移动窗口,一定会按下鼠标左键,那么我们只要捕获到鼠标左键按下的消息,然后删除它即可。鼠标左键按下会产生两种类型的消息:WM_LBUTTONDOWN和WM_NCLBUTTONDOWN消息,那么WM_LBUTTONDOWN和WM_NCLBUTTONDOWN的区别是什么呢?先看看MSDN怎么说:
The WM_LBUTTONDOWN message is posted when the user presses the left mouse button while the cursor is in the client area of a window.
If the mouse is not captured, the message is posted to the window beneath the cursor. Otherwise, the message is posted to the window that has captured the mouse.
The WM_NCLBUTTONDOWN message is posted when the user presses the left mouse button while the cursor is within the nonclient area of
a window. This message is posted to the window that contains the cursor. If a window has captured the mouse, this message is not posted.
由此可以知道:
WM_LBUTTONDOWN是在左击客户区时响应; WM_NCLBUTTONDOWN是在左击非客户区时响应。
那么什么是客户区?什么是非客户区?
标题栏,菜单就是非客户区,标题栏、菜单等以外的窗体就是客户区。
所以我们应该处理非客户区的WM_NCLBUTTONDOWN消息:
LRESULT CALLBACK WndProc(HWND hWnd, UINT message, WPARAM wParam, LPARAM lParam)
{
...
switch (message)
{
case WM_COMMAND:
wmId = LOWORD(wParam);
wmEvent = HIWORD(wParam);
// Parse the menu selections:
switch (wmId)
{
...
case WM_NCLBUTTONDOWN:
MSG msg;
PeekMessage(&msg, hWnd, 0, 0, PM_REMOVE);
break;
...
default:
return DefWindowProc(hWnd, message, wParam, lParam);
}
return 0;
}
二、接下来看看在MFC中如何实现的呢,同样也是处理非客户区的消息WM_NCLBUTTONDOWN,那么怎么去响应这个消息呢?
虚函数OnNcLButtonDown(UINT nHitTest, CPoint point) 即是用来响应这个消息的,添加步骤与平时的有些不同:
Ctrl+W -> MFC ClassWizard,在出现的对话框,切换到Class Info 选项卡,Class name中指定类,再在Message filter中选择Windows,点击OK。在Class name中指定类中右击Add Windows Message Headle,在New Windows message/events中选定WM_NCLBUTTONDOWN,再点击Add and Edit 就会出现OnNcLButtonDown。
添加绿色部分代码即可实现。
void CTestThreadDlg::OnNcLButtonDown(UINT nHitTest, CPoint point)
{
// TODO: Add your message handler code here and/or call default
if (HTCAPTION == nHitTest) {
return;
}
CDialog::OnNcLButtonDown(nHitTest, point);
}
HTCAPTION 的意思是the cursorIn's location is a title-bar area.
详细的可以查看MSDN中说明。
//=============================================================================================================
备注::
1》WINCE下不支持WM_NCLBUTTONDOWN和WM_NCHITTEST这2个消息,但用ModifyStyleEx(0, WS_EX_NODRAG, 0);是可以禁止对话框程序移动的!!!
相关文章
1、如何禁止拖动窗口?----http://bbs.csdn.net/topics/290020376
2、对话框有标题栏,怎么禁止鼠标拖动窗口----http://bbs.csdn.net/topics/350059939
3、MFC对话框禁止移动 使标题栏不响应鼠标消息 通过客户区移动窗体 OnNcHitTest()的实现----/article/1536733.html
一、C API实现方法
思路:如果用户想通过鼠标移动窗口,一定会按下鼠标左键,那么我们只要捕获到鼠标左键按下的消息,然后删除它即可。鼠标左键按下会产生两种类型的消息:WM_LBUTTONDOWN和WM_NCLBUTTONDOWN消息,那么WM_LBUTTONDOWN和WM_NCLBUTTONDOWN的区别是什么呢?先看看MSDN怎么说:
The WM_LBUTTONDOWN message is posted when the user presses the left mouse button while the cursor is in the client area of a window.
If the mouse is not captured, the message is posted to the window beneath the cursor. Otherwise, the message is posted to the window that has captured the mouse.
The WM_NCLBUTTONDOWN message is posted when the user presses the left mouse button while the cursor is within the nonclient area of
a window. This message is posted to the window that contains the cursor. If a window has captured the mouse, this message is not posted.
由此可以知道:
WM_LBUTTONDOWN是在左击客户区时响应; WM_NCLBUTTONDOWN是在左击非客户区时响应。
那么什么是客户区?什么是非客户区?
标题栏,菜单就是非客户区,标题栏、菜单等以外的窗体就是客户区。
所以我们应该处理非客户区的WM_NCLBUTTONDOWN消息:
LRESULT CALLBACK WndProc(HWND hWnd, UINT message, WPARAM wParam, LPARAM lParam)
{
...
switch (message)
{
case WM_COMMAND:
wmId = LOWORD(wParam);
wmEvent = HIWORD(wParam);
// Parse the menu selections:
switch (wmId)
{
...
case WM_NCLBUTTONDOWN:
MSG msg;
PeekMessage(&msg, hWnd, 0, 0, PM_REMOVE);
break;
...
default:
return DefWindowProc(hWnd, message, wParam, lParam);
}
return 0;
}
二、接下来看看在MFC中如何实现的呢,同样也是处理非客户区的消息WM_NCLBUTTONDOWN,那么怎么去响应这个消息呢?
虚函数OnNcLButtonDown(UINT nHitTest, CPoint point) 即是用来响应这个消息的,添加步骤与平时的有些不同:
Ctrl+W -> MFC ClassWizard,在出现的对话框,切换到Class Info 选项卡,Class name中指定类,再在Message filter中选择Windows,点击OK。在Class name中指定类中右击Add Windows Message Headle,在New Windows message/events中选定WM_NCLBUTTONDOWN,再点击Add and Edit 就会出现OnNcLButtonDown。
添加绿色部分代码即可实现。
void CTestThreadDlg::OnNcLButtonDown(UINT nHitTest, CPoint point)
{
// TODO: Add your message handler code here and/or call default
if (HTCAPTION == nHitTest) {
return;
}
CDialog::OnNcLButtonDown(nHitTest, point);
}
HTCAPTION 的意思是the cursorIn's location is a title-bar area.
详细的可以查看MSDN中说明。
//=============================================================================================================
备注::
1》WINCE下不支持WM_NCLBUTTONDOWN和WM_NCHITTEST这2个消息,但用ModifyStyleEx(0, WS_EX_NODRAG, 0);是可以禁止对话框程序移动的!!!
如何
//////////
禁止拖动窗口?
如何禁止拖动窗口?
相关文章推荐
- MFC中左键移动窗口实现方法
- MFC中左键移动窗口实现方法
- (VC/MFC)禁止对话框(窗口)的大小改变和移动
- MFC程序中让窗口不能移动、最大化、最小化,禁止系统菜单
- MFC控件随窗口大小变化原理及实现
- 易语言用窗口消息实现无边框调整和窗口的移动方法
- MFC中将窗口最小化到托盘实现原理及操作步骤
- [VC/MFC]实现程序运行时隐藏窗口 (多种方法,推荐方法2)
- JS实现窗口加载时模拟鼠标移动的方法
- C# Windows API应用之基于FlashWindowEx实现窗口闪烁的方法
- duilib 如何实现初始化时最大化窗口以及禁止窗口双击放大和移动窗口
- 用多线程方法实现在MFC/WIN32中调用OpenGL函数并创建OpenGL窗口
- Delphi 实现无窗口移动(详细使用WM_NCHITTEST和PtInRect API进行测试)
- MFC对话框禁止移动 使标题栏不响应鼠标消息 通过客户区移动窗体 OnNcHitTest()的实现
- MFC 禁止主窗口的移动
- (VC/MFC)禁止对话框(窗口)的大小改变和移动
- MFC对话框禁止移动 使标题栏不响应鼠标消息 通过客户区移动窗体 OnNcHitTest()的实现
- 关于在MFC中实用在基于对话框窗口上面绘制折线的实现方法
- 练习笔记——API实现QQ窗口“灵异”移动