MFC Windows 程序设计->实现客户区拖动
2011-04-02 18:01
561 查看
One problem with a window without a title bar is that it can't be
repositioned with the mouse. Windows are dragged by their title bars, and when
there's no title bar, the user has nothing to grab onto. Clock solves this
little dilemma by playing a trick with the window's WM_NCHITTEST handler so that
the window can be dragged by its client area, a feature Windows programmers call
client-area drag.
In Windows, every mouse message is preceded by a WM_NCHITTEST message
containing screen coordinates identifying the cursor location. The message is
normally handled by ::DefWindowProc, which returns a code that tells
Windows what part of the window the cursor is over. Windows uses the return
value to decide what type of mouse message to send. For example, if the left
mouse button is clicked over the window's title bar, ::DefWindowProc's
WM_NCHITTEST handler returns HTCAPTION and Windows sends the window a
WM_NCLBUTTONDOWN message. If ::DefWindowProc returns HTCLIENT instead,
Windows converts the cursor coordinates from screen coordinates to client
coordinates and passes them to the window in a WM_LBUTTONDOWN message.
The fact that an application sees mouse messages in raw form makes for some
interesting possibilities. The following OnNcHitTest handler implements
client-area drag by fooling Windows into thinking that the mouse is over the
title bar when in fact it's over the window's client area:
With this OnNcHitTest handler in place, a window is as easily dragged
by its client area as by its title bar. And it works even if the window doesn't
have a title bar. Try it: click the left mouse button in Clock's client
area, and move the mouse with the button held down. The window should go
wherever the mouse goes.
Clock uses an OnNcHitTest handler similar to the one shown above. The
only difference is that Clock verifies that the left mouse button is down before
replacing an HTCLIENT return code with HTCAPTION so that other mouse
messages—particularly right-button mouse messages that precede WM_CONTEXTMENU
messages—will get through unscathed:
The call to ::GetAsyncKeyState checks the left mouse button and
returns a negative value if the button is currently down.
repositioned with the mouse. Windows are dragged by their title bars, and when
there's no title bar, the user has nothing to grab onto. Clock solves this
little dilemma by playing a trick with the window's WM_NCHITTEST handler so that
the window can be dragged by its client area, a feature Windows programmers call
client-area drag.
In Windows, every mouse message is preceded by a WM_NCHITTEST message
containing screen coordinates identifying the cursor location. The message is
normally handled by ::DefWindowProc, which returns a code that tells
Windows what part of the window the cursor is over. Windows uses the return
value to decide what type of mouse message to send. For example, if the left
mouse button is clicked over the window's title bar, ::DefWindowProc's
WM_NCHITTEST handler returns HTCAPTION and Windows sends the window a
WM_NCLBUTTONDOWN message. If ::DefWindowProc returns HTCLIENT instead,
Windows converts the cursor coordinates from screen coordinates to client
coordinates and passes them to the window in a WM_LBUTTONDOWN message.
The fact that an application sees mouse messages in raw form makes for some
interesting possibilities. The following OnNcHitTest handler implements
client-area drag by fooling Windows into thinking that the mouse is over the
title bar when in fact it's over the window's client area:
UINT CMainWindow::OnNcHitTest (CPoint point) { UINT nHitTest = CFrameWnd::OnNcHitTest (point); if (nHitTest == HTCLIENT) nHitTest = HTCAPTION; return nHitTest; }[/code] |
by its client area as by its title bar. And it works even if the window doesn't
have a title bar. Try it: click the left mouse button in Clock's client
area, and move the mouse with the button held down. The window should go
wherever the mouse goes.
Clock uses an OnNcHitTest handler similar to the one shown above. The
only difference is that Clock verifies that the left mouse button is down before
replacing an HTCLIENT return code with HTCAPTION so that other mouse
messages—particularly right-button mouse messages that precede WM_CONTEXTMENU
messages—will get through unscathed:
UINT CMainWindow::OnNcHitTest (CPoint point) { UINT nHitTest = CFrameWnd::OnNcHitTest (point); if ((nHitTest == HTCLIENT) && (::GetAsyncKeyState (MK_LBUTTON) < 0)) nHitTest = HTCAPTION; return nHitTest; }[/code] |
returns a negative value if the button is currently down.
相关文章推荐
- MFC Windows 程序设计->捕获鼠标
- MFC Windows 程序设计->键盘加速键
- MFC Windows 程序设计->消息反射
- MFC Windows 程序设计->WinMain 简单Windows程序 命令行编译
- MFC Windows 程序设计->hello world
- MFC Windows 程序设计->GetDeviceCaps
- MFC Windows 程序设计->最顶层窗口
- MFC Windows 程序设计->取消对GDI对象的选定
- <<windows程序设计>> Charles Petzold VS 王艳平
- #error WINDOWS.H already included. MFC apps must not #include <windows.h>
- WINDOWS.H already included. MFC apps must not #include <windows.h>
- 3dsmax 二次开发里面加入对话框时提示:WINDOWS.H already included. MFC apps must not #include <windows.h>
- Windows程序设计-王艳平.pdf==> 第二章
- Control Study -> 实现:用鼠标拖动图片
- fatal error C1189: #error : WINDOWS.H already included. MFC apps must not #include <windows.h>
- java jdk1.5、 windows 32位字节 ----->实现系统托盘功能
- C语言Windows程序设计->第三天->属于自己的窗口
- STM32存储器 — <3> STM32实现IAP(上位机和IAP程序设计)
- 项目day02--<客户上下线时间程序实现>(中)
- MFC客户区拖动窗口实现