Win32 Series - Nonclient-Area Mouse Messages

本文详细介绍了Windows中非客户区鼠标消息的工作原理及其处理方式,包括如何通过WM_NCHITTEST消息触发其他鼠标事件,以及如何转换屏幕坐标与客户区坐标。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

 

http://www-user.tu-chemnitz.de/~heha/petzold/

 

Nonclient-Area Mouse Messages

The 10 mouse messages discussed so far occur when the mouse is moved or clicked within the client area of a window. If the mouse is outside a window's client area but within the window, Windows sends the window procedure a "nonclient-area" mouse message. The nonclient area of a window includes the title bar, the menu, and the window scroll bars.

You do not usually need to process nonclient-area mouse messages. Instead, you simply pass them on toDefWindowProc so that Windows can perform system functions. In this respect, the nonclient-area mouse messages are similar to the system keyboard messages WM_SYSKEYDOWN, WM_SYSKEYUP, and WM_SYSCHAR.

The nonclient-area mouse messages parallel almost exactly the client-area mouse messages. The message identifiers include the letters "NC" to indicate "nonclient." If the mouse is moved within a nonclient area of a window, the window procedure receives the message WM_NCMOUSEMOVE. The mouse buttons generate these messages:

 

ButtonPressedReleasedPressed (Second Click)
LeftWM_NCLBUTTONDOWNWM_NCLBUTTONUPWM_NCLBUTTONDBLCLK
MiddleWM_NCMBUTTONDOWNWM_NCMBUTTONUPWM_NCMBUTTONDBLCLK
RightWM_NCRBUTTONDOWNWM_NCRBUTTONUPWM_NCRBUTTONDBLCLK

 

The wParam and lParam parameters for nonclient-area mouse messages are somewhat different from those for client-area mouse messages. ThewParam parameter indicates the nonclient area where the mouse was moved or clicked. It is set to one of the identifiers beginning with HT (standing for "hit-test") that are defined in the WINUSER.H.

The lParam parameter contains an x-coordinate in the low word and ay-coordinate in the high word. However, these are screen coordinates, not client-area coordinates as they are for client-area mouse messages. For screen coordinates, the upper-left corner of the display area hasx and y values of 0. Values of x increase as you move to the right, and values ofy increase as you move down the screen. (See Figure 7-3.)

You can convert screen coordinates to client-area coordinates and vice versa with these two Windows functions:

ScreenToClient (hwnd, &pt) ;
ClientToScreen (hwnd, &pt) ;

where pt is a POINT structure. These two functions convert the values stored in the structure without preserving the old values. Note that if a screen-coordinate point is above or to the left of the window's client area, thex or y value of the client-area coordinate could be negative.

Click to view at full size.

Figure 7-3. Screen coordinates and client-area coordinates.

The Hit-Test Message

If you've been keeping count, you know that so far we've covered 20 of the 21 mouse messages. The last message is WM_NCHITTEST, which stands for "nonclient hit test." This message precedes all other client-area and nonclient-area mouse messages. ThelParam parameter contains the x and y screen coordinates of the mouse position. ThewParam parameter is not used.

Windows applications generally pass this message to DefWindowProc. Windows then uses the WM_NCHITTEST message to generate all other mouse messages based on the position of the mouse. For nonclient-area mouse messages, the value returned fromDefWindowProc when processing WM_NCHITTEST becomes the wParam parameter in the mouse message. This value can be any of thewParam values that accompany the nonclient-area mouse messages plus the following:

HTCLIENT        Client area
HTNOWHERE       Not on any window
HTTRANSPARENT   A window covered by another window
HTERROR         Causes DefWindowProc to produce a beep

If DefWindowProc returns HTCLIENT after it processes a WM_NCHITTEST message, Windows converts the screen coordinates to client-area coordinates and generates a client-area mouse message.

If you remember how we disabled all system keyboard functions by trapping the WM_SYSKEYDOWN message, you may wonder if you can do something similar by trapping mouse messages. Sure! If you include the lines

case WM_NCHITTEST:
     return (LRESULT) HTNOWHERE ;

in your window procedure, you will effectively disable all client-area and nonclient-area mouse messages to your window. The mouse buttons will simply not work while the mouse is anywhere within your window, including the system menu icon, the sizing buttons, and the close button.

Messages Beget Messages

Windows uses the WM_NCHITTEST message to generate all other mouse messages. The idea of messages giving birth to other messages is common in Windows. Let's take an example. As you may know, if you double-click the system menu icon of a Windows program, the window will be terminated. The double-click generates a series of WM_NCHITTEST messages. Because the mouse is positioned over the system menu icon,DefWindowProc returns a value of HTSYSMENU and Windows puts a WM_NCLBUTTONDBLCLK message in the message queue withwParam equal to HTSYSMENU.

The window procedure usually passes that mouse message to DefWindowProc. WhenDefWindowProc receives the WM_NCLBUTTONDBLCLK message with wParam equal to HTSYSMENU, it puts a WM_SYSCOMMAND message withwParam equal to SC_CLOSE in the message queue. (This WM_SYSCOMMAND message is also generated when a user selects Close from the system menu.) Again the window procedure usually passes that message toDefWindowProc. DefWindowProc processes the message by sending a WM_CLOSE message to the window procedure.

If the program wants to require confirmation from a user before terminating, the window procedure can trap WM_CLOSE. Otherwise,DefWindowProc processes WM_CLOSE by calling the DestroyWindow function. Among other chores,DestroyWindow sends a WM_DESTROY message to the window procedure. Normally, a window procedure processes WM_DESTROY with the code

case WM_DESTROY:
     PostQuitMessage (0) ;
     return 0 ;

The PostQuitMessage causes Windows to place a WM_QUIT message in the message queue. This message never reaches the window procedure because it causesGetMessage to return 0, which terminates the message loop and the program.

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值