2009年6月19日 星期五

Windows 7 Touch SDK - Get Gesture Information

下面的步驟說明如何取得Gestre的資料:

1.
Set up a window for receiving gestures
如果要收到WM_GESTURE Message, 先確保程式中沒有呼叫RegisterTouchWindow.如下所示:

BOOL InitInstance(HINSTANCE hInstance, int nCmdShow)
{
HWND hWnd;

hInst = hInstance; // Store instance handle in our global variable.

hWnd = CreateWindow(szWindowClass, szTitle, WS_OVERLAPPEDWINDOW,
CW_USEDEFAULT, 0, CW_USEDEFAULT, 0, NULL, NULL, hInstance, NULL);

if (!hWnd)
{
return FALSE;
}

ShowWindow(hWnd, nCmdShow);
UpdateWindow(hWnd);

return TRUE;
}


2. Handling Gesture Messages
和處理WM_TOUCH的方式一樣,你可以在WndProc的函數中取得WM_GESTURE Message, 如下所示:

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. */
DecodeGesture(wParam, lParam);
break;
}
}


3. Interpreting the Gesture Messages

可以利用GetGestureInfo函式將Gesture message(lParam)轉換成GESTUREINFO的資料結構. 其資料結構包含gesture的作用點和類型等, 如下所示:

void 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));

BOOL bResult = GetGestureInfo((HGESTUREINFO)lParam, &gi);

if (bResult){
// 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;
}
}else{
DWORD dwErr = GetLastError();
if (dwErr > 0){
//GetDlgItem(IDC_STATIC)->SetWindowTextW(L"Error!");
}
}
CloseGestureInfoHandle((HGESTUREINFO)lParam);
}


參考資料 : http://msdn.microsoft.com/en-us/library/dd371578(VS.85).aspx

沒有留言:

張貼留言