触摸整个屏幕的手势

Touch Gesture for the entire screen

本文关键字:屏幕 触摸      更新时间:2023-10-16

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

我正在尝试编写一个代码,该代码使用Microsoft为Windows 10使用C 提供的触摸手势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);
    }
}
//----------------------------------------------------------------------//

显然,上述代码指向一个手柄,那是一个窗口,而不是整个屏幕。

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

上述方法不正确,并且使用全局钩的概念可以实现。

您可以挂在某种触摸手势上,并因此填充屏幕键盘