Hands playing computer Windows7 system two-finger touch API reveals secret

  

Windows 7 has some cool features that XP, Vista system does not have, such as window group preview, quickly hide desktop icons, shake other windows... these cool How is the function designed? How do personal software we develop use these features to surprise users?

In the "Minority Report", Tomang directly used two fingers to operate the program on the touch screen, which was awkward. This feature is also supported by Win 7. As long as there is a touch device, we can also use both hands to play the computer on Win 7. Two-finger touch such as zoom, two-finger zoom, two-finger flip and other effects can be implemented in Win 7.

Two Commonly Used Gestures

1. Zoom Gestures

Zoom gestures are used to zoom in or out of the interface, and are often used in programs such as browsing pictures and reading articles. The gesture of the two-finger zoom is shown in Figure 1: The two fingers are placed together in the form, one pointing up and one pointing down, this is zooming in, the two fingers are placed on the form separately, and the two points are sliding in the middle, then Zoom out.

The program needs to recognize this gesture. The first step is to get a message from the finger to the screen. In the second step, the distance is calculated according to the coordinates of the two fingers, and the distance between the two fingers is compared according to an empirical value to obtain that the two fingers are separated or the two fingers are close together. In the third step, the coordinates of the finger point can be captured in real time according to a timer to calculate the moving direction and the speed, and further, whether the two fingers are reverse sliding or opposite sliding can be determined.

2. Pan gestures

Pan gestures are often used for navigation. For example, in an image viewer, use the pan left gesture to navigate to the previous one and the pan pan gesture to navigate to the next image. The pan gesture is shown in Figure 2: both fingers slide to the left or to the right. How to distinguish whether the user wants to change the position of the picture or turn the page? The acceleration must be used here.

You can capture the finger coordinates in real time in a timer, calculate the velocity and acceleration, and compare it with the acceleration through an empirical value. When the acceleration is relatively large, it can be considered as turning pages, otherwise it is dragging. move. Of course, this is not enough. When dragging, you need to further judge whether you reach the program boundary. If you reach the boundary, it is also considered to be page turning. For the processing of turning pages, moving to the left or reaching the left border is considered to be the previous page.

Two-finger touch API reveals

The core of two-finger touch is to put the original touch data into the motion model to calculate the user's gesture. The raw touch data can be obtained from the WM_TOUCH message. The zoom and pan gestures are recognized by WM_TOUCH.

Step 1: Check Multi-Touch Hardware

Use the GetSystemMetrics API here to get the hardware ready:

BYTE digitizerStatus = (BYTE)GetSystemMetrics(SM_DIGITIZER);
if ((digitizerStatus & (0x80 + 0x40)) == 0){
//No multi-touch device detected or device not ready
}
//Check device support Touch points, it should be noted that Win 7 currently supports 2 touch points better
BYTE nInputs = (BYTE)GetSystemMetrics(SM_MAXIMUMTOUCHES);

Step 2: Switch to the original touch message < Br>

By default, Win 7 provides a WM_GESTURE (gesture) message by default. This gesture is a gesture that the system recognizes based on the original touch data. The RegisterTouchWindow API function allows the system to return the WM_TOUCH original touch input message. The core code:

RegisterTouchWindow(hWnd, 0)) //hwnd is the current window Handle.

Step 3: Get the raw touch data

Use the GetTouchInputInfo API to get the original touch data, core code:

GetTouchInputInfo((HTOUCHINPUT)lParam, numInputs, ti, sizeof( TOUCHINPUT)))
numInputs is a pre-declared variable for storing several touch points. If 5 fingers are simultaneously sliding on the screen, then numInputs is 5.
ti is a TOUCHINPUT structure that contains the X and Y coordinates of the contact, the time of occurrence, and the type of touch (similar to mouse press, mouse movement, mouse release, etc.).

for(unsigned int i=0; i<numInputs; ++i){ //Loop according to touch input point
if (ti[i].dwFlags & TOUCHEVENTF_DOWN){
OnTouchDownHandler (hWnd, ti[i]); //handed over to the contact press function
} else if (ti[i].dwFlags & TOUCHEVENTF_MOVE){
OnTouchMoveHandler(hWnd, ti[i]); //Handed to the contact move function processing
}else if (ti[i].dwFlags & TOUCHEVENTF_UP){
OnTouchUpHandler(hWnd, ti[i]);} //Handed over to the contact release function
}

The function of pressing, moving, releasing, etc. of the contact is actually a motion simulation model. In the model, we need to generate several motion curves for the trajectory of the contact and calculate The azimuth, velocity, normal acceleration, tangential acceleration, etc. of each point are extracted to identify specific gestures.

Tip: The easiest way to get multi-touch support is to use Win 7's gesture recognition function, mainly using the following API:
GetSystemMetrics, used to check whether multi-touch hardware devices Exist and ready.
GetGestureInfo, used to get the gesture type and related data.

Copyright © Windows knowledge All Rights Reserved