触摸整个屏幕的手势

时间:2017-09-29 17:23:48

标签: c++ windows visual-studio winapi

我是为Windows编写程序的新手。道歉,如果我的问题是一个非常简单或基本的问题。

我正在尝试编写一个代码,该代码使用Microsoft为使用C ++的Windows 10提供的触摸手势api。我也在使用触摸屏显示器。

我能够创建一个窗口和一个句柄来使用触摸手势,如下面的代码所示:

LRESULT CALLBACK WindowProc(HWND hwnd, UINT uMsg, WPARAM wParam, LPARAM lParam);

LRESULT CALLBACK DecodeGesture(HWND hwnd, UINT uMsg, WPARAM wParam, LPARAM lParam);

int WINAPI wWinMain(HINSTANCE hInstance, HINSTANCE, PWSTR pCmdLine, int nCmdShow)
{
    // Register the window class.

    const wchar_t CLASS_NAME[] = L"Sample Window Class";

    WNDCLASS wc = {};

    wc.lpfnWndProc = WindowProc;
    wc.hInstance = hInstance;
    wc.lpszClassName = CLASS_NAME;

    RegisterClass(&wc);

    // Create the window.

    HWND hwnd = CreateWindowEx(
        0,                              // Optional window styles.
        CLASS_NAME,                     // Window class
        L"Learn to Program Windows",    // Window text
        WS_OVERLAPPEDWINDOW,            // Window style

                                        // Size and position
        CW_USEDEFAULT, CW_USEDEFAULT, CW_USEDEFAULT, CW_USEDEFAULT,

        NULL,       // Parent window    
        NULL,       // Menu
        hInstance,  // Instance handle
        NULL        // Additional application data
    );


    if (hwnd == NULL)
    {
        return 0;
    }

    ShowWindow(hwnd, nCmdShow);

    // Run the message loop.

    MSG msg = {};
    while (GetMessage(&msg, NULL, 0, 0))
    {   
        TranslateMessage(&msg);
        DispatchMessage(&msg);
    }

    return 0;
}

LRESULT CALLBACK WindowProc(HWND hwnd, UINT uMsg, WPARAM wParam, LPARAM lParam)
{

    switch (uMsg)
    {
    case WM_DESTROY:
        PostQuitMessage(0);
        return 0;

    case WM_PAINT:
    {
        PAINTSTRUCT ps;
        HDC hdc = BeginPaint(hwnd, &ps);

        FillRect(hdc, &ps.rcPaint, (HBRUSH)(COLOR_WINDOW + 1));

        EndPaint(hwnd, &ps);
    }
    return 0;

    case WM_GESTURE:
        // Insert handler code here to interpret the gesture.            
        return DecodeGesture(hwnd, uMsg, wParam, lParam);

    }
    return DefWindowProc(hwnd, uMsg, wParam, lParam);
}


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
            // Get all instances of the onscreen keyboard running on the local computer.
            // This will return an empty array if onscreen keyboard isn't running.
            array<Process^>^localByName = Process::GetProcessesByName("osk");
            if (localByName->Length < 1)
            {
                system("start powershell.exe Set-ExecutionPolicy RemoteSigned \n");
                system("start powershell.exe osk");
                //system("pause");
            }
            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);
    }
}

//----------------------------------------------------------------------//

显然,上面提到的代码指向一个句柄,即一个窗口,而不是整个屏幕。

如何修改此代码以创建指向整个屏幕的句柄,而不仅仅是指向该窗口内的区域?

1 个答案:

答案 0 :(得分:-1)

上述方法不正确,使用全局钩子的概念可以实现同样的目的。

您可以挂钩某种触摸手势并相应地填充屏幕键盘