Getting Started with Windows Touch Gestures

本文详细介绍了使用Windows Touch手势的基本步骤,包括设置接收手势的窗口、处理手势消息以及解释手势消息的方法。通过简单的实例代码展示了如何初始化实例、处理手势消息和解析手势消息,帮助开发者深入理解Windows Touch手势的应用。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

This section describes the basic steps for using multitouch gestures.

The following steps are typically performed when using Windows Touch gestures:   

  1. Set up a window for receiving gestures.
  2. Handle the gesture messages.
  3. Interpret the gesture messages.

Setting up a Window to Receive Gestures

By default, you receive WM_GESTURE messages.      

Note  If you call     RegisterTouchWindow, you will     stop receiving WM_GESTURE messages.  If you are not receiving WM_GESTURE messages, make sure that    you haven't called RegisterTouchWindow.

The following code shows a simple InitInstance implementation.

C++
#include <windows.h>


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


Handling Gesture Messages

Similar to handling Windows Touch input messages, you can handle gesture messages in many ways.    If you are using Win32, you can check for the WM_GESTURE message in WndProc.  If you are    creating another type of application, you can add the WM_GESTURE message to the message map    and then implement a custom handler.   

The following code shows how gesture messages could be handled.

C++
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.            
            return DecodeGesture(hWnd, message, wParam, lParam);            


Interpreting the Gesture Messages

The GetGestureInfo    function is used to interpret a gesture message into a structure describing the gesture.  The     structure, GESTUREINFO,     has information about the gesture such as the location where the gesture was performed and     the type of gesture.  The following code shows how you can retrieve and interpret a gesture    message.   

C++
  LRESULT DecodeGesture(HWND hWnd, UINT message, WPARAM wParam, LPARAM lParam){
    // Create a structure to populate and retrieve the extra message info.
    GESTUREINFO gi;  
    
    ZeroMemory(&gi, sizeof(GESTUREINFO));
    
    gi.cbSize = sizeof(GESTUREINFO);

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

    if (bResult){
        // now interpret the gesture
        switch (gi.dwID){
           case GID_ZOOM:
               // Code for zooming goes here     
               bHandled = TRUE;
               break;
           case GID_PAN:
               // Code for panning goes here
               bHandled = TRUE;
               break;
           case GID_ROTATE:
               // Code for rotation goes here
               bHandled = TRUE;
               break;
           case GID_TWOFINGERTAP:
               // Code for two-finger tap goes here
               bHandled = TRUE;
               break;
           case GID_PRESSANDTAP:
               // Code for roll over goes here
               bHandled = TRUE;
               break;
           default:
               // A gesture was not recognized
               break;
        }
    }else{
        DWORD dwErr = GetLastError();
        if (dwErr > 0){
            //MessageBoxW(hWnd, L"Error!", L"Could not retrieve a GESTUREINFO structure.", MB_OK);
        }
    }
    if (bHandled){
        return 0;
    }else{
        return DefWindowProc(hWnd, message, wParam, lParam);
    }
  }


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值