Handling the WM GESTURE Message


This article was sent to us by: Tanya G. at 01282010

1 Windows 7 » Handling the WM GESTURE Message
Bookmark and Share

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 messages in your application’s WndProc functions. The following code shows how gesture messages can be handled in Win32 applications:

LRESULT CALLBACK WndProc(HWND hWnd, UINT message, WPARAM wParam, LPARAM lParam)
   {
   int wmId, wmEvent;
   PAINTSTRUCT ps;
   HDC hdc;
   switch (message){
   case WM_GESTURE:
   /* insert handler code here to interpret the gesture */
   break;
   ...
   default:
   return DefWindowProc(hWnd, message, wParam, lParam);
   }
   return 0;
   }
   ..
   

WM_GESTURE is the generic message used for all gestures. Therefore, to determine which gesture you need to handle, first you need to decode the gesture message. The information about the gesture is found in the lParam parameter, and you need to use a special function GetGestureInfo to decode the gesture message. This function receives a pointer to a GESTUREINFO structure and lParam; if it’s successful, the function fills the gesture information structure with all the information about the gesture:

   GESTUREINFO gi;
   ZeroMemory(&gi, sizeof(GESTUREINFO));
   gi.cbSize = sizeof(gi);
   BOOL bResult = GetGestureInfo((HGESTUREINFO)lParam, &gi);
   

Here you can see that you prepare the GESTUREINFO, gi, structure by clearing its content with zeros except for its size and then passing its pointer to the function to fill it with the gesture message information.

After obtaining a GESTUREINFO structure, you can check dwID, one of the structure members, to identify which gesture was performed. However, dwID is just one of the structure members.

There are several other important members:

  • dwFlags The state of the gesture, such as begin, inertia, or end.
  • dwID The identifier of the gesture command. This member indicates the gesture type.
  • cbSize The size of the structure, in bytes; to be set before the function call.
  • ptsLocation A POINTS structure containing the coordinates associated with the gesture. These coordinates are always relative to the origin of the screen.
  • dwInstanceID and dwSequenceID These are internally used identifiers for the structure, and they should not be used or handled.
  • ullArguments This is a 64-bit unsigned integer that contains the arguments for gestures that fit into 8 bytes. This is the extra information that is unique for each gesture type.
  • cbExtraArgs The size, in bytes, of extra ullArguments that accompany this gesture.

Now you can complete the switch-case clause to handle all the different Windows 7 gestures, as shown in the following code snippet:

   void CMTTestDlg::DecodeGesture(WPARAM wParam, LPARAM lParam)
   {
   // create a structure to populate and retrieve the extra message info
   GESTUREINFO gi;
   gi.cbSize = sizeof(GESTUREINFO);
   ZeroMemory(&gi, sizeof(GESTUREINFO));
   GetGestureInfo((HGESTUREINFO)lParam, &gi);
   // now interpret the gesture
   switch (gi.dwID){
   case GID_ZOOM:
   // Code for zooming goes here
   break;
   case GID_PAN:
   // Code for panning goes here
   break;
   case GID_ROTATE:
   // Code for rotation goes here
   break;
   case GID_TWOFINGERTAP:
   // Code for two-finger tap goes here
   break;
   case GID_PRESSANDTAP:
   // Code for roll over goes here
   break;
   default:
   // You have encountered an unknown gesture
   break;
   CloseGestureInfoHandle((HGESTUREINFO)lParam);
   }
 

In the preceding code segment, you can see how we set the stage for handling each gesture separately, as the dwID flag indicates the type of gesture. Also note that at the end of the function we call the CloseGestureInfoHandle function, which closes resources associated with the gesture information handle. If you handle the WM_GESTURE message, it is your responsibility to close the handle using this function. Failure to do so can result in memory leaks.

If you look carefully in the Windows 7 Software Development Kit (SDK), you’ll find that the dwID member can also have values indicating when a gesture is starting (GID_BEGIN), which is essentially when the user places his fingers on the screen. The SDK also defines the GID_END flag, which indicates when a gesture ends. Gestures are exclusive, meaning you can’t achieve the effect of zooming and rotating at the same time as using gestures.

Your application can receive at a given time either a zoom or rotate gesture, but not both. But gestures can be compound, because the user can perform several gestures during one long touch episode. GID_BEGIN and GID_END are the start and end markers for such gesture sequences. To achieve the effect of zooming and rotating at the same time, you need to handle raw touch events and use the manipulation process as described in the next article.

Most applications should ignore the GID_BEGIN and GID_END messages and pass them to DefWindowProc. These messages are used by the default gesture handler; the operating system cannot provide any touch support for legacy applications without seeing these messages. The dwFlags member contains the information needed to handle the beginning and ending of a particular gesture.

By now, you can see that handling gesture messages is not that difficult. Handling gesture messages has a fixed process that includes configuration (which we will describe later in the article), decoding the gesture message, and handling each specific gesture according to your application’s needs. Next, you’ll learn what unique information each gesture includes and how to handle it.

Legal Disclaimer

Webworldarticles.com is not responsible for the information contained by this article as well for any and all copyright infringements by authors and writers. Webworldarticles.com is a free information resource. If you suspect this article for any copyright infringement, please read the terms of service and contact us to investigate the problem.

Related Articles

1. 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 ...

2. 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. Thi...

3. 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 e...

4. 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...

5. 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...

6. 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 work...

7. 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 w...