Windows Applications Program Recognition of several special mouse events in
As a computer input device, the mouse becomes the standard configuration of the computer with the popularity of windows. In
In Windows, mouse operations can generate more than 20 messages, mainly divided into the customer area mouse messages and non-customer Areas
There are two types of mouse messages, including moving the mouse, right-clicking the left, releasing, and double-clicking. In fact
Applications often use some special mouse events, such as three-click mouse, left and right mouse at the same time
, Double-click the independent identification or processing. Next, let's talk about it with my programming experience.
How to identify these special mouse events in windows.
1. Independent Processing click, double-click, and three-click
Let's take a look at windows's response to the mouse. For the left button, if you press the mouse
Wm_lbuttondown message. Then, the mouse is released to generate the wm_lbuttonup message. However, if you double-click the mouse,
In Windows, not only wm_lbuttondblclk messages are generated, but wm_lbuttondown is generated first.
And then generate wm_lbuttondblclk messages, including wm_lbuttonup and other messages.
Theory. Windows does not provide independent messages for the trigger of the mouse, but we may consider that the trigger is
After the wm_lbuttondblclk message is sent, a wm_lbuttondown message is sent. Therefore, in Application Programming
When you want the window to Independently respond to the mouse click, double-click, three-click message, only the user to go
. For example, renaming a folder in winows95 is an example. Click a highlighted file with the mouse
You can change the name of the folder after you click it. If you double-click it, you can open the file. In this case, it must be processed separately.
Click and double-click the mouse. Otherwise, the execution sequence should first respond to the Message wm_lbuttondown and then respond.
Wm_lbuttondblclk, that is, it is counterproductive to change the name and then open it.
To identify the three mouse messages separately, we cannot directly use wm_lbuttondown and
Wm_lbuttondblclk message to determine the mouse click and double-click. Here, three "pseudo" messages are defined.
Wm_mysngclk, wm_mydblclk, and wm_mythrclk, respectively.
Click Event. Because we cannot determine whether there are three clicks after two consecutive mouse clicks, we add another
Wm_mydblclkt pseudo messages, further judge double-click and three-click when processing this pseudo message. We only process
Wm_lbuttondown message, so the cs_dblclks style is not set when registering the window class.
The specific process is as follows: Set the logical flag1 and flag2. The initial values are false.
Set flag1 to true when you click it, and set flag2 to true when you double-click it. When processing the wm_lbuttondown message
When settimer is used, a timer id_timer1 is added, and the timer time parameter is set to double-click
And set flag1 to true. If the timer
When d_timer1 sends a message, it indicates that there are no buttons within the specified time. You can determine that the mouse is clicked and the mouse can be issued.
Wm_mysngclk message, set flag1 to false at the same time; If the timer message is not generated, it indicates
Press the mouse key within the specified time. Double-click the mouse and set flag2 to true.
You need to continue to determine. At this time, the wm_mydblclkt message is sent, and we use the same method in the pseudo message
Wm_mydblclkt defines another timer id_timer2. Similarly, if the timer id_timer2 emits
If you do not have a button within the specified time, you can double-click the mouse to send a wm_mydblclk message.
If the timer id_timer2 does not send a message, it indicates that a mouse key is pressed within the specified time.
You can send the wm_mythrclk message by clicking the mouse. The recognition has been completed. Of course, the timer must be used.
Killtimer is deleted in time. Note that the time interval between double-clicking the mouse is not too large. Otherwise
Time is too obvious.
The following is a specific implementation method. This article only provides part of wndproc in the window process.
Generally, Windows applications are similar.
# Define id_timer1 1001/* timer 1 */
# Define id_timer2 1002/* Timer 2 */
# Define wm_mydblclkt wm_user + 100/* If you have double-clicked but cannot decide to trigger the trigger */
# Define wm_mysngclk wm_user + 101/* sent when clicked */
# Define wm_mydblclk wm_user + 102/* sent when double-click is determined */
# Define wm_mythrclk wm_user + 103/* sent when it is identified as a trigger */
Lresult callback wndproc (hwnd, uint umessage, wparam, lparam)
{
Static int flag1, flag2;
Int wtime;
Point pt;
Switch (umessage)
{
Case wm_lbuttondown:/* The program only processes the clicked message, but does not process the double-click message */
Getcursorpos (& pt);/* use the cursor position to transfer the lparam in the message */
Screentoclient (hwnd, & pt );
If (! Flag2)/* When flag2 is not true, the mouse state is the first or second click */
{If (! Flag1)/* When flag1 is not true, click it for the first time */
{Wtime = getdoubleclicktime ();/* double-click the time interval */
Settimer (hwnd, id_timer1, wtime, null);/* Create a timer after the first click */
}
If (flag1)/* flag1 is true, double-click it, and send wm_mydblclkt to continue judgment */
{Postmessage (hwnd, wm_mydblclkt, 0, makelparam (Pt. X, Pt. y ));
Flag1 = false;
Killtimer (hwnd, id_timer1 );
Break;
}
Flag1 = true;
}
If (flag2)/* flag2 is true, it is determined to be three-click */
{Postmessage (hwnd, wm_mythrclk, 0, makelparam (Pt. X, Pt. y ));
Flag2 = false;
Killtimer (hwnd, id_timer2 );
Break;
}
Break;
Case wm_timer:/* the timer message is generated, indicating that the mouse has no buttons */
Switch (wparam)
{
Case id_timer1:/* no subsequent buttons after the first button, so click */
{Killtimer (hwnd, wparam );
Flag1 = false;
Getcursorpos (& pt );
Screentoclient (hwnd, & pt );
Postmessage (hwnd, wm_mysngclk, 0, makelparam (Pt. X, Pt. y ));
}
Break;
Case id_timer2:/* no subsequent buttons after the second key, so double-click it */
{Killtimer (hwnd, wparam );
Flag2 = false;
Getcursorpos (& pt );
Screentoclient (hwnd, & pt );
Postmessage (hwnd, wm_mydblclk, 0, makelparam (Pt. X, Pt. y ));
}
Break;
}
Break;
Case wm_mousemove:/* If the mouse moves, it is not considered double-click or three-click */
Flag1 = false;
Flag2 = false;
Break;
Case wm_mydblclkt:/* double-click and wait for the mouse to press the mouse button again */
Wtime = getdoubleclicktime ();
Settimer (hwnd, id_timer2, wtime, null );
Flag2 = true;
Break;
Case wm_mysngclk:/* processing click */break;
Case wm_mydblclk:/* double-click Processing */break;
Case wm_mythrclk:/* handle three hits */break;
// Other messages
}
2. Process click, double-click, and three-click operations in sequence
You can also click, double-click, or trigger a three-click operation. For example, in word and other word processing software
Use the mouse to select the processing method of the editing area, click to locate the insertion point, double-click to select a word, three-click
Select a paragraph. This method is to process clicks, double-clicks, and three clicks in sequence. The following describes
The mouse event processing method is basically the same as the independent identification method described above. The difference is that
The window style is set to cs_dblclks, so that the window class can recognize and double-click the mouse.
The specific method is to send the wm_mydblclk message when processing the double-click message, and set the timing.
Flag is true (the initial value is false), so that it can be determined when wm_lbuttindown is processed.
Flag value: If it is false, click to send the wm_mysngclk message. If it is true, double-click
Send the wm_mythrclk message by one click (namely, three clicks) to complete message recognition. Timer Function
, Just wait until the double-click button appears. If the timer message has been sent, it indicates there is no
Press the button to delete the timer and click it to re-identify it.
The following is part of the window function wndproc. The program annotation section illustrates the difference with the previous example.
# Define id_timer 1001
# Define wm_mysngclk wm_user + 101
# Define wm_mydblclk wm_user + 102
# Define wm_mythrclk wm_user + 103
Lresult callback wndproc (hwnd, uint umessage, wparam, lparam)
{
Static int flag;
Int wtime;
Point pt;
Switch (umessage)
{
Case wm_lbuttondown:
Getcursorpos (& pt );
Screentoclient (hwnd, & pt );
If (FLAG) {/* if it is true, double-click the button (namely, three-click )*/
Postmessage (hwnd, wm_mythrclk, 0, makelparam (Pt. X, Pt. y ));
Flag = false;
Killtimer (hwnd, id_timer );
}
Else/* Otherwise, click */
Postmessage (hwnd, wm_mysngclk, 0, makelparam (Pt. X, Pt. y ));
Break;
Case wm_timer:
Switch (wparam)
{
Case id_timer:
{Killtimer (hwnd, wparam);/* Delete the timer when it is generated */
Flag = false;
}
Break;
}
Break;
Case wm_lbuttondblclk:
Getcursorpos (& pt );
Screentoclient (hwnd, & pt );
Wtime = getdoubleclicktime ();
Settimer (hwnd, id_timer, wtime, null);/* set the timer */
Postmessage (hwnd, wm_mydblclk, 0, makelparam (Pt. X, Pt. y ));
Flag = true;
Break;
Case wm_mousemove:/* move the mouse, and start to judge again from the click */
Flag = false;
Break;
Case wm_mysngclk:/* processing click */break;
Case wm_mydblclk:/* double-click Processing */break;
Case wm_mythrclk:/* handle three hits */break;
// Other messages
}
3. Recognition of simultaneous mouse and keyboard Press
Have you ever played a Windows clearance game? This game has an operation that simultaneously presses the left and right keys of the mouse.
It is not complicated to determine whether to press both the left and right buttons simultaneously. How to determine whether the mouse buttons are consistent with CTRL and
Shift and press the same method at the same time. Here we need to use the wparam item in the mouse message, which contains
The status of several buttons. Definition:
(1) mk_control: set the value to 1 when the ctrl key is pressed;
(2) mk_lbutton: set to 1 when the left mouse button is pressed;
(3) mk_mbutton: set the value to 1 when the key is pressed;
(4) mk_rbutton: Right-click the mouse and set the value to 1;
(5) mk_shift: set the value to 1 when the shift key is pressed.
By judging these flags, you can obtain the statuses of several keys simultaneously pressed to determine whether they exist.
It is key-pressed at the same time.
Mouse messages are often processed in Windows. The above methods are some of
The implementation method is for reference only. The above program is passed in Borland C ++ 5.0.