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:
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.
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. Windows 7 file system FAT32 and NTFS comparison
All articles in this directory are property of their respective authors. Additionally, read our Privacy Policy
© 2010 WebWorldarticles.com - All Rights Reserved.