Use the Pan Gesture to Move an Object


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.

Use the Zoom Gesture to Scale an Object

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.

Legal Disclaimer

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

Related Articles

1. Creating a Performance Counter Log in Windows 7
To create a performance counter log, follow these steps: 1. Open Performance Monitor, and navigate to the Data Collector Sets node in the tree on the left. ...

2. Performance of Windows 7 and Notebooks
Rediscover the Fundamentals: It’s All About Performance The single most important request customers have had about the next release of the Microsoft Win...

3. Handling the WM GESTURE Message
To work with gestures, you’ll need to handle the WM_GESTURE messages that are sent to your application. If you are a Win32 programmer, you can check for WM_GESTURE ...

4. How Multitouch Works in Windows 7
New hardware and API elements in the Windows 7 operating system provide applications the ability to receive and handle touch and multitouch input. This capability enables...

5. Organize My Data Libraries in Windows 7
This article details the new functionality offered by Windows 7 Libraries. We’ll review the Windows Explorer changes that promote the new user experience that Libra...

6. Changes Made to Windows Explorer in Windows 7
Changes Made to Windows Explorer in Windows 7 In Windows 7, Libraries address the problem of users’ data being stored all over the PC by allowing users to...

7. Make Your Windows 7 Application Library Aware
Libraries under the Hood Now that we have a better understanding of what libraries represent and how users as well as developers can benefit from them, let&rsqu...

8. Working with the Shell Namespace in Windows 7
Working with the Shell Namespace Before we dive into the Shell Libraries programming model, we need to understand how the Windows Shell works. The Windows Shell...

9. Windows 7 Multitouch Programming Models
Windows 7 Multitouch Programming Models The Windows 7 multitouch platform enables you to build touch-aware applications. Keep in mind that with time, the amount...

10. How to copy files in Windows 7 and the move them
Creating Copies of Files and Folders in Windows 7 In Windows 7 when you want to share a file with a friend or if you want to create a backup of a file, the eas...