Windows Programming _ 17 _ mouse _ 1

Source: Internet
Author: User

Windows is not updated for a long timeProgramThe content of the design, today I read a section of Windows program design file operations, while remembering that the mouse operation has not been updated

Text. Write a little text about the mouse.

I. Mouse

(1)

Windows supports single keys, double keys, and three keys. You can also use a joystick or a pen to simulate a mouse.

1. Determine whether the system has a mouse

Before using the mouse, you must determine whether the mouse exists in the system. You can use the getsystemmetrics function to determine whether the mouse exists.

Bmouse = getsystemmetrics (sm_mousepresent );

If the mouse is installed, bmouse returns true; otherwise, 0.

Key points:

In Windows 98, whether or not the mouse is installed, this function returns true.

2. determine the number of mouse keys

Through the getsystemmetrics function, you can determine the mouse and mouse that are installed. You only need to change the parameter passed to the function to sm_cmousebuttons.

The mouse key in the system. If the mouse is not installed, the function returns 0. in Windows 98, whether the mouse is installed or not, this function returns 2.

3. Determine whether the mouse has switched over the left and right hands

Pass the sm_swapbutton to the getsystemmetrics function to determine whether the switch is performed;

(2) Windows pre-Definition

When a Windows user moves the mouse, there is a small bitmap of "mouse cursor" on the screen, moving as the user moves. There is an exact point pointing to the screen on the mouse cursor

Single-pixel "Hotspot ".

Windows supports predefined mouse cursor: idc_arrow (arrow cursor), idc_cross (Cross cursor), idc_wait (cursor); Arrow cursor hotspot in

At the top of the arrow, the hot spot of the Cross cursor is in the center.

Windows allows you to customize the mouse cursor. when defining the window class, you can specify the predefined mouse cursor as the default mouse of the window.

Use the following statement to specify the default window cursor:

Wndclass. hcursor = loadcursor (null, idc_arrow );

Mouse operation terms:

Click: Press and release a mouse key.

Double-click: Press and release the mouse key twice.

Drag: Hold down the mouse key and move the mouse

Mouse key name:

Left click: lbutton

Key: mbutton

Right-click rbutton

The double-Key mouse has only the left and right buttons, and the single-key mouse has only one left button.

(3) Client mouse messages

The mouse moves in the user window and the key window receives messages. This is different from the keyboard message: Only windows with the input focus can receive keyboard messages.

Windows defines 21 types of mouse messages, 10 of which are customer messages, and 11 are non-customer messages. user programs often ignore non-customer messages. Move the cursor from the window

The window will receive the wm_mousemove message. When the user presses the mouse key in the window, the user will receive the following mouse message:

Only the three-Key mouse receives the Middle-Key mouse message. Only the double-key and three-Key mouse can receive the right-click message. Only after the defined window class can receive the dblclk double-click message, the window

To receive these messages.

For mouse messages received by the window, the value of the lparam parameter contains the cursor position: the low position is the X coordinate, the high position is the Y coordinate, and the lparam parameter is 32 bits, it is defined as 16 characters;

To process a mouse message, you can use the following method:

X = loword (lparam );

Y = hiword (lparam );

Obtain the hotspot coordinates of the current mouse message. The X and Y values are different from those in the upper left corner of the window client.

Windows defines the wparam parameter to indicate the status of the mouse key and shift and CTRL keys. in the program, you can use the bits defined by winuser. h to test the wparam parameter.

MK indicates the mouse message. You can use the following statement to test the button status:

If (wparam & mk_shift)

Statement;

If the above statement can be executed when the wm_lbuttondown message is received, the shift key is also pressed when the left button is pressed.

Windows cannot send the wm_mousemove message for every pixel passing through the hotspot while moving the mouse. the speed at which the window receives the message depends on the speed at which the mouse moves.

And the speed at which the Window Process processes mobile messages; that is, Windows cannot fill the Message Queue with unprocessed wm_mousemove messages. (To verify this feature, spy can be used.

Program to monitor mouse messages and window processing messages)

The process of processing mouse messages in the window is as follows:

Inactive window --> press the mouse --> change the window to an active window --> send the mouse message to the window,

Activity window -- press the mouse -- send the mouse message to the window

The above process is a general process, but sometimes it will become a different process. When a modal window exists, this process may become different and requires

Note.

The messages received by the window are not necessarily left-click or right-click to receive the first mouse message. For example, when we press the left mouse button in the first window

Then move the mouse to the second window to release the message. After receiving the mouse message in the second window, wm_mousemove and wm_lbuttonup messages are displayed,

Therefore, pay attention to the processing.

Exceptions:

 

(4) Handling shift, Ctrl, and mouse combination messages

As mentioned above, you can use the wparam parameter and the bitwise flag and operation to check whether a combination message such as the mouse and shift/CTRL is pressed.

In the mouse message, you can use the following statement to determine whether shift and CTRL are pressed.

If (wparam & mk_shift)

{

If (wparam & mk_control)

{Shift and CTRL keys are simultaneously pressed for the mouse message}

Else

{Press shift at the same time as the mouse key}

}

Else

{

If (wparam & mk_control)

{Press the ctrl key while holding the mouse message}

Else

{Only mouse messages}

}

In the user program, you can use the combination of the mouse and the keyboard to simulate the mouse message. In this way, you can right-click the mouse to process the message. Details

We don't need instances.CodeDescription.

In Windows, the getkeystate function is returned through the Virtual key code vk_lbutton, vk_rbutton, vk_mbutton, vk_shift, and vk_control.

The status of the mouse key and shift key. If getkeysate returns a negative value, the mouse or shift key is pressed. Because getkeystate returns the mouse key or shift currently being processed

Key status, so all status messages are synchronized with the corresponding message.

(5) double-click the mouse key

Double-click the mouse button to click twice in a short time.

Double-click condition: 1. When two clicks occur, the distance between the cursor and the hotspot must be within the specified position.

2. The time interval between two clicks is within the specified time range. You can change the time interval between double-clicking the mouse in the system control panel.

To enable the window to receive double-click mouse messages, the style of the window must have a cs_dblclks flag. As follows:

Wndclass. Style = cs_hredraw | cs_vredraw | cs_dblclks;

If the window style is not set, the window will receive the following series of messages when you double-click the window:

If the window style is set, the following message is received:

As shown above, only the third message has changed.

If the first click operation in the double-click operation completes a click function, it is easy to process the double-click message. The second click message is used to complete tasks other than the first click;

For example:

Windows Resource Manager mouse click selected, and double-click to open; of course, you can design in the system to point to selected and click to open, but this does not meet the needs of most people

Work habits.

(6) Non-customer mouse messages

If you place the cursor inside the window but outside the customer area, the window will receive the non-customer area mouse message sent by the system. The non-customer area of the window includes the title bar, menu bar, and

Window scroll bar. In general, the user program does not need to process non-window mouse messages, but these messages are processed by the defwindowproc function.

Windows uses "nc" to indicate a non-customer message. If you move the cursor in the non-customer area of the window, the wm_ncmousemove message is received during the window process. Non-customer

The user zone message is as follows:

Because there are several areas in the non-customer zone, we also need to identify the mouse messages in the title bar, menu bar, or scroll bar when processing non-customer zone messages.

To achieve resolution: wparam and lparam parameters can be used for resolution. The wparam and lparam parameters of the mouse messages in the customer zone are different from those in the non-customer zone. wparam

The parameter specifies the non-customer location where the mouse message is sent. When there is a non-customer mouse message, wparam is set to a macro identifier starting with HT (HT indicates hit test)

When there is a non-customer mouse message, the lparam parameter returns the coordinates of the hotspot when the mouse message occurs, but the coordinates are screen coordinates, rather than the customer zone coordinates. The screen coordinates are at the top of the upper left corner of the screen.

The point is the coordinate origin (0, 0); X increases when the mouse moves to the right, and Y increases when the mouse moves down.

You can use Windows functions to convert the screen coordinates and client coordinates:

Screentoclient (hwnd, & pt );

Clienttoscreen (hwnd, & pt );

(8) Hit Test message

In Windows, the hit Test message wm_nchittest is set. This message takes precedence over all other customer and non-customer mouse messages. The lparam parameter contains

X and Y screen coordinates. The wparam parameter is useless.

Windows applications usually pass the message to defwindowproc. Then, Windows uses the wm_nchittest message to generate other mouse messages based on the mouse position;

For non-customer mouse messages, when processing the wm_nchittest message, the value range returned from defwindowproc will become the wparam parameter in the mouse message. This value can be any

Add the following content to the wparam value of the non-customer mouse message:

Htclient: Customer Zone

Htnowhere: Not in the window

Httransparent: the window is overwritten by another window.

Warning ror: Make defwindowproc generate beep

If defwindowproc returns htclient after processing the wm_nchittest message, Windows converts the screen coordinate to the client coordinate and generates the client mouse message.

Because wm_nchittest messages are pre-processed before all mouse messages, we can use this to disable all mouse messages:

In the window procedure function, we can do this:

Case wm_nchittest:

Return (lresult) wm_nchittest;

In this way, all window mouse messages can be disabled.

 

Using the hit Test message, the windows program designs a message generation mechanism. We can analyze this process by double-clicking the system menu of the window:

Double-click the Window System Menu & gt; wm_nchittest message & gt; defwindowproc to process the wm_nchittest message. Then, return to & gt; defwindowproc

The wm_nclbuttondblclk message is put into the message queue and the wparam is set to htsysmenu -- generally, the user program will not process wm_nclbuttondblclk

Send the message to defwindowproc for function processing -- "defwindowproc receives wm_nclbuttondblclk of wparam = htsysmenu

After the message is sent, put the wm_syscommand message into the message queue, and set the wparam = SC _close parameter -- in the window, send the wm_syscommand message

Send to defwindowproc function processing --> defwindowproc send the wm_close message to the window.

If the user program does not process the wm_close message, defwindowproc will process the message. After the defwindowproc function receives the wm_close message, it will call

The destroywindow function is used to process wm_close. In addition to other processing functions, destroywindow also sends the wm_destroy message to the window process. The window process usually uses the following

Code to process the wm_destroy message:

Case wm_destroy:

Postqiutmessage (0 );

Retrun 0;

Postqiutmessage enables Windows to put the wm_quit message into the message queue. This message will never be processed through the window process, because the wm_destroy message

The getmessage function returns 0 to terminate the message loop and exit the program.

(9) capture the mouse

Sometimes, you may want to get the mouse message after the mouse leaves the window. In this case, you need to capture the mouse.

For example:

In the drawing program, we need to draw a rectangle. we press the left button of the mouse table, drag the mouse to the appropriate size, and then release the mouse to draw a rectangle.

When the mouse leaves the client area of the window, the original window will not be able to accept the message of the mouse release. At this time, there is no way to determine the size of the rectangle, and I do not know how to draw the rectangle,

To solve the problem above, you can use the mouse capture function.

Windows provides a function to capture the mouse, as shown below:

Setcapture (hwnd );

After this function is called, Windows will send all subsequent mouse messages to the Windows process whose window handle is hwnd, And the mouse messages will be client messages, even if the mouse is in a non-customer zone;

The lparam parameter indicates the cursor position in the coordinates. However, the values returned by loword (lparam) and hiord (lparam) may be positive or negative, which is related to the location where the Mouse sends messages.

When you want to release the mouse, call

Releasecapture (hwnd );

The message can be restored to the status before capturing.

We know that when you press the mouse key in window 1, the mouse in window 1 is captured, and then move to window 2, then release the mouse key, then window 1 will not receive the mouse release message, and window 2 will receive

The mouse message is received not by the window that captures the mouse, but by the window below the cursor;

To prevent capture exceptions, the mouse is captured only when the mouse key is pressed in the customer area of the window. The mouse is released only when the mouse key is released.

Related Article

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.