With the pan gesture, you can control the scrolling of content in a scrollable area. Or you can apply the pan gesture to a specific object, moving it in any direction by simply touching it with one or two fingers and moving it. This is also known as transformation because you are partially transforming the object from being located in one location to being located in another.
In the illustration, you can see two touch points, marked with the numbers 1 and 2. By default, the pan gesture supports both single-finger and two-finger panning. You’ll see how to configure pan gestures and other gestures later in this article.
Now let’s see what code you need to implement in your GID_PAN switch to achieve this panning effect. Our application is simple; it displays a single rectangle. We show only the parts of the code that are required to handle gesture messages. Because this is part of a larger example that is written in C++, we do not describe in detail any of the other elements, such as painting the rectangle.
The gesture info structure includes the dwFlags member, which is used to determine the state of the gesture and can include any of the following values:
We’ll use the GF_BEGIN flag to save the initial start coordinates of the touch point as a variable as a reference for the following steps. The gesture information includes the ptsLocation member that contains the X and Y coordinates of the touch point. Then for each consecutive pan message, we’ll extract the new coordinates of the touch point. By using the initial start position that we saved before and the new coordinates, we can calculate the new position and apply the move operation to the object. Finally, we’ll repaint the object in its new position. The following code snippet shows the entire GID_PAN switch:
case GID_PAN:
switch(gi.dwFlags)
{
case GF_BEGIN:
_ptFirst.x = gi.ptsLocation.x;
_ptFirst.y = gi.ptsLocation.y;
ScreenToClient(hWnd,&_ptFirst);
break;
default:
// We read the second point of this gesture. It is a middle point
// between fingers in this new position
_ptSecond.x = gi.ptsLocation.x;
_ptSecond.y = gi.ptsLocation.y;
ScreenToClient(hWnd,&_ptSecond);
// We apply the move operation of the object
ProcessMove(_ptSecond.x-_ptFirst.x,_ptSecond.y-_ptFirst.y);
InvalidateRect(hWnd,NULL,TRUE);
// We have to copy the second point into the first one to prepare
// for the next step of this gesture.
_ptFirst = _ptSecond;
break;
}
break;
Here you can see that in the case of GF_BEGIN flag, we use _ptFirst, a simple POINT structure, to save the initial starting touch coordinates from the gesture information in ptsLocation. We call the ScreenToClient function to convert the screen coordinates of a given point on the display to the window of our application, because the coordinates in the gesture information are always relative to the origin of the screen.
The next pan message that arrives is handled by the default case. Now we save the coordinates in the _ptSecond variable and again calculate the coordinates relative to our application window. Then we simply subtract the first touch point from the second touch point to find the new location and call ProcessMove, which is a helper function, to update the new coordinates of the rectangle. We call InvalidateRect to repaint the whole window to show the rectangle at the new coordinates. Finally, we save the latest touch point in the _ptFirst for reference for the next gesture message. When a two-finger pan gesture is used, the coordinates of the touch point in the gesture information structure, ptsLocation, represent the current position of the pan that is the center of the gesture. The ullArgument member indicates the distance between the two touch points.
The Windows 7 pan gesture also includes support for inertia. Inertia is used to create some sort of continuation to the movement that was generated by the gesture. After you take your finger off the touch-sensitive device, the system calculates the trajectory based on the velocity and angle of motion and continues to send WM_GESTURE messages of type GID_PAN that are flagged with GF_INERTIA, but it reduces the speed of the movement up to a complete stop of the motion. The inertia-flagged messages continue to include updated coordinates, but with each message the delta between the previous coordinates decreases to the point of a complete stop. You’re given the option to distinguish between normal pan gestures and inertia to enable you to opt for special behavior for inertia if needed.
In the previous section, you learned how to move an object using the pan gesture. The pan gesture is widely used for document reading as well as for more graphical purposes such as picture viewing and manipulation. For all these cases, the zoom gesture is also widely used by both developers and users.
The zoom gesture is usually implemented by users as a pinch movement involving two touch points, where the user moves her fingers closer together to zoom out and moves them farther apart to zoom in. For simplicity, we’ll refer to zoom in as zoom and explicitly say zoom out for the opposite gesture. The zoom gesture allows you to scale the size of your objects.
Our website is not responsible for the information contained by this article. Webworldarticles.com is a free articles resource thus practically any visitor can submit an article. However if you notice any copyrighted material, please contact us and we will remove the article(s) in discussion right away.
This article was sent to us by:
Tanya G. at
01282010
1. Creating a Performance Counter Log in Windows 7
All articles in this directory are property of their respective authors. Additionally, read our Privacy Policy
© 2010 WebWorldarticles.com - All Rights Reserved.