Message about wm_nchittest

Source: Internet
Author: User

To move a form without a title bar, I used the wm_nchittest message, which is roughly as follows:

We usually drag the title bar of the dialog box to move the window, but sometimes we want to move the window by dragging the mouse over the customer area.

An easy-to-think solution is to process the mouse messages wm_lbuttondown and wm_lbuttonup. The onlbuttonup function calculates the changes in the mouse position and calls movewindow to move the window.

Note: When you drag the title bar to move the window, a rectangular box is displayed, prompting you to move the current position of the window. When the left mouse button is opened, the window is moved to the position of the rectangle. This function is not available in our implementation scheme.

To implement this function, we must draw these rectangles by ourselves.

In fact, we don't have to do this on our own, because Windows is already ready for us.

Imagine that if I could cheat windows and tell it that the title bar is being dragged by the mouse instead of the customer area, windows would be responsible for moving the window.

It is not as difficult or even very simple to cheat windows.

We use a message: wm_nchittest.

Msdn explains it as follows:

TheWm_nchittestMessage is sent to a window when the cursor moves, or when a mouse button is pressed or released. if the mouse is not captured, the message is sent to the window beneath the cursor. otherwise, the message is sent to the window that has captured the mouse.

This message is sent when you move the mouse or press the mouse key.

What does Windows use this message? "Hittest" indicates "hit test". The wm_nchittest message is used to obtain the current hit position of the mouse.

The message response function of wm_nchittest determines which part of the window the mouse hits based on the current coordinate of the mouse. the return value of the message response function indicates the part. For example, it may return htcaption, or htclient. (There are many return values. Please refer to msdn ).

For ease of understanding, let me first describe the response process for pressing the mouse key in Windows:

1. Determine the window to which the mouse key is clicked. Windows uses a table to record the regional coordinates of each window on the current screen.ProgramWindows is notified that the mouse key is pressed. Windows determines which window it is clicked Based on the mouse coordinates.

2. Determine which part of the window is clicked by the mouse key. Windows sends the wm_nchittest message to the mouse key clicking window to ask which part of the window is clicked by the mouse key. (Windows will be notified of the return value of the message response function of wm_nchittest ). Generally, wm_nchittest messages are processed by the system, and users generally do not take the initiative to process them (that is, the wm_nchittest message response function usually uses the Windows Default processing function ).

3. Send the corresponding message to the window based on the mouse key clicked part. For example, if the return value of the message response function of wm_nchittest is htclient, indicating that the user clicks the client area, Windows will send the wm_lbuttondown message to the window; if the return value of the message response function of wm_nchittest is not htclient (probably htcaption, htclose, htmaxbutton, etc.), that is, if you click the non-customer area with the mouse, Windows will send the wm_nclbuttondown message to the window.

It is necessary to discuss in detail: If the return value of the message response function of wm_nchittest is htcaption, it indicates that the title bar is clicked by the mouse. What is the next process of windows?

As mentioned above, Windows will send the wm_nclbuttondown message to the window.

Msdn describes the wm_nclbuttondown message as follows:

Wm_nclbuttondown

Nhittest = (INT) wparam; // hit-test value

PTS = makepoints (lparam); // position of cursor

Wm_nclbuttondown wparam indicates the position of the window clicked by the mouse, and lparam indicates the coordinates of the current mouse.

If the application does not respond to the message, the system processes the message by default.

What is the default processing? Wparam indicates that the title bar is clicked, and the current window is displayed in the Drag and Drop status (the status information of each window is recorded in Windows ). Because the "Drag and Drop status" is identified, your mouse movement status is completely tracked by Windows from now until the mouse key is opened. It moves the window synchronously Based on the mouse.

Note: during this process, the window will not receive the wm_ncmousemove message, because the window and the mouse are "synchronized", and your mouse is static relative to the window.

 

However, the problem also occurs. I want to pop up a menu when right-clicking the form. When I finish the message msg_wm_rbuttondown, I find that the form cannot receive the message, remove the implementation of the wm_nchittest message. The reason is:

Because you have processed the mouse message in wm_nchittest and positioned it as htcaption, that is, the mouse is on the title bar, and the title bar belongs to the non-customer area (NC );
Event messages that are not in the customer zone start with wm_nc. That is to say, when your wm_nchittest returns htcaption, you can use wm_lbuttonup to process the messages. You can only use wm_nclbuttonup to process the messages.

 

Solution:

Wm_nchittest and wm_ncrbuttonup are processed simultaneously, but wm_rbuttonup is not processed.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.