2009年6月19日 星期五

Windows 7 Touch SDK - Get Touch Point

下面的範例程式介紹如何取得touch的相關資料, 可分成三個步驟:

1.
Create an Application and Enable Windows Touch
首先, 先在VC 2008中產生一個Win32 Project, 然後記得在targetver.h修改Windows Version, 否則會編譯不過.如下所示:
#ifndef WINVER                  // Specifies that the minimum required platform is Windows 7.
#define WINVER 0x0601 // Change this to the appropriate value to target other versions of Windows.
#endif



2.Registering to Receive Multitouch Input
你必須使用RegisterTouchWindow將應用程式註冊成touch window,才能收到WM_TOUCH Message, 否則, 預設只能收到WM_GESTURE Message.範例如下所示:

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;
}

//註冊成touch window
RegisterTouchWindow(hWnd, 0);

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

return TRUE;
}


3. Handling Multi-touch Messages
你可以在WinProc函式中操作touch messages, 如下所示:
LRESULT OnTouch(HWND hWnd, WPARAM wParam, LPARAM lParam )
{
UINT cInputs = LOWORD(wParam); //將wParam轉換touch點的數目
PTOUCHINPUT pInputs = new TOUCHINPUT[cInputs]; //產生一組大小為cInputs的PTOUCHINPUT資料結構

if (pInputs)
{

//利用GetTouchInputInfo函式取得TOUCHINPUT的資料
if (GetTouchInputInfo((HTOUCHINPUT)lParam, cInputs, pInputs, sizeof(TOUCHINPUT)))
{
for (int i=0; i <>(cInputs); i++)
{
TOUCHINPUT ti = pInputs[i];
// do something with your touch input handle
}
}

// if you handled the message and don't want anything else done with it, you can close it
CloseTouchInputHandle((HTOUCHINPUT)lParam);
delete [] pInputs;
}
else
{
/* handle the error here */
}

// if you didn't handle the message, let DefWindowProc handle:
return DefWindowProc(hWnd, WM_TOUCH, wParam, lParam);
}


LRESULT CALLBACK WndProc(HWND hWnd, UINT message, WPARAM wParam, LPARAM lParam)
{
int wmId, wmEvent;
PAINTSTRUCT ps;
HDC hdc;

switch (message)
{
// pass touch messages to the touch handler
case WM_TOUCH:
OnTouch(hWnd, wParam, lParam);
break;
}
}

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

沒有留言:

張貼留言