0

我需要让我的应用程序与触控兼容,这就是我这些天潜入 Windows 触控 API 的原因。

Windows API 对我来说很新,这就是为什么我错过了一些东西。

这是我的问题:我下载了 Microsoft Sample of a simple Gesture App。它工作得很好,但是手势功能是在绑定到 HWND 创建的 WinProc 中添加处理的。问题是我的“真实”应用程序自己创建了它的 HWND,这就是为什么我想使用挂钩来接收 WM_TOUCH 消息。

我在一个独立的应用程序中尝试过这样的操作(没有像在我的真实应用程序中那样使用 DLL)

//Window Parameters (Can't be modified in my original App)
//------Automatically called by an external lib in my "real app"------------
ATOM BaseApp_RegisterClass(HINSTANCE hInstance)
{
    WNDCLASSEX wcex;

    wcex.cbSize = sizeof(WNDCLASSEX);

    wcex.style            = CS_HREDRAW | CS_VREDRAW;
    wcex.lpfnWndProc      = WndProc;
    wcex.cbClsExtra       = 0;
    wcex.cbWndExtra       = 0;
    wcex.hInstance        = hInstance;
    wcex.hIcon            = LoadIcon(hInstance, MAKEINTRESOURCE(IDI_MTGEST));
    wcex.hCursor          = LoadCursor(NULL, IDC_ARROW);
    wcex.hbrBackground    = (HBRUSH)(COLOR_WINDOW+1);
    wcex.lpszMenuName     = MAKEINTRESOURCE(IDC_MTGEST);
    wcex.lpszClassName    = (LPSTR)g_wszWindowClass;
    wcex.hIconSm          = LoadIcon(wcex.hInstance, MAKEINTRESOURCE(IDI_SMALL));

    return RegisterClassEx(&wcex);
}    
//------/Automatically called by an external lib in my "real app"------------

//Create the Window of the Basic App
BOOL InitInstance(HINSTANCE hInstance, int nCmdShow)
{
    //------Automatically called by an external lib in my "real app"------------
    // (....)
    BaseApp_RegisterClass(hInstance);
    // (....)
    //------/Automatically called by an external lib in my "real app"------------

   //------This is what I can add to my "real app"------------
   //Register the Touch Window
   RegisterTouchWindow(hWnd, 0 );
   UpdateWindow(hWnd);

   unsigned long threadId = GetWindowThreadProcessId(hWnd, 0);
    //Hook the SENT Messages
    SetWindowsHookEx(
        WH_GETMESSAGE,
        (HOOKPROC) HookTouchSENT,
        NULL,
        threadId);  

    //Hook the POSTED Messages
   SetWindowsHookEx(
       WH_CALLWNDPROC,
       (HOOKPROC) HookTouchPOSTED,
       NULL,
       threadId);

   //------/This is what I can add to my "real app"------------
}

//------Automatically called by an external lib in my "real app"------------
//Primary WinProc of the Window (Can't be modified in my original App)
LRESULT CALLBACK WndProc(HWND hWnd, UINT message, WPARAM wParam, LPARAM lParam)
{
    if(message == WM_TOUCH)LogInConsole("Caught a WM_TOUCH in the Main WndProc\n");   //WM_TOUCH is ALWAYS caught here when touch the screen
    else if(message == WM_GESTURE)LogInConsole("Caught a WM_GESTURE in the Main WndProc\n");

    return DefWindowProc(hWnd, message, wParam, lParam);
}
//------/Automatically called by an external lib in my "real app"------------

//------This is what I can add to my "real app"------------
//Get the 'POSTED' Messages of the Window
LRESULT CALLBACK HookTouchPOSTED(int nCode, WPARAM wParam, LPARAM lParam)  
{
    HWND hWnd = m_hwnd; 

    UINT message = ((PCWPSTRUCT)lParam)->message; 

    if(message == WM_TOUCH)LogInConsole("Caught a WM_TOUCH in HookTouchPOSTED\n");   //WM_TOUCH is NEVER caught here when touch the screen
    else if(message == WM_GESTURE)LogInConsole("Caught a WM_GESTURE in HookTouchPOSTED\n");   


    std::stringstream strStream; strStream<<"Received POSTED Message:"<<message<<", Hex:0x"<<std::hex<<((PCWPSTRUCT)lParam)->message<<std::endl;  //Receive the normals windows messages and although WM_GESTURENOTIFY
    LogInConsole(strStream.str());

    return CallNextHookEx(0,nCode, wParam, lParam);
}

//Get the 'SENT' Messages of the Window
LRESULT CALLBACK HookTouchSENT(int nCode, WPARAM wParam, LPARAM lParam) 
{

    HWND hWnd = m_hwnd; 

    UINT message = ((PCWPSTRUCT)lParam)->message; 

    if(message == WM_TOUCH)LogInConsole("Caught a WM_TOUCH in HookTouchPOSTED\n");   //WM_TOUCH is NEVER caught here when touch the screen
    else if(message == WM_GESTURE)LogInConsole("Caught a WM_GESTURE in HookTouchPOSTED\n");

    std::stringstream strStream; strStream<<"Received POSTED Message:"<<message<<", Hex:0x"<<std::hex<<((PCWPSTRUCT)lParam)->message<<std::endl;
    LogInConsole(strStream.str());

    return CallNextHookEx(0,nCode, wParam, lParam);
}    
//------/This is what I can add to my "real app"------------

我的问题是 WM_TOUCH 消息从不通过 Hooks,而只在 WinProc 中。事实上,我只在挂钩中收到一条 WM_GESTURENOTIFY 消息,通知手势已经开始,但是以下应该给出值的“WM_TOUCH”消息永远不会被捕获......

以前有人遇到过这个问题吗?

提前感谢您的帮助

4

1 回答 1

1

感谢雷蒙德的帮助。子类化是解决方案!所以最后我去掉了所有的钩子,做了这样的:

BOOL InitInstance(HINSTANCE hInstance, int nCmdShow)
{
    //------Automatically called by an external lib in my "real app"------------
    // (....)
    BaseApp_RegisterClass(hInstance);
    // (....)
    //------/Automatically called by an external lib in my "real app"------------



   //------This is what I can add to my "real app"------------
   //Register the Touch Window
   RegisterTouchWindow(hWnd, 0 );   //If called then receive WM_TOUCH messages. Else receive WM_GESTURE messages

   //----SUBCLASS THE WINDOW
    UINT_PTR uIdSubclass;
    DWORD_PTR dwRefData;

   bool resultsc = SetWindowSubclass(
       hWnd,
       windowProcSubclass,
       uIdSubclass,
       dwRefData
       );
   if (resultsc) LogInConsole("Window Subclassed with success\n");
   //----/SUBCLASS THE WINDOW

   //------/This is what I can add to my "real app"------------
}

//------And the subclass Proc that now catches the WM_TOUCH and WM_GESTURE messages: ------------
LRESULT CALLBACK windowProcSubclass(HWND hWnd, UINT message, WPARAM wParam, LPARAM lParam,  UINT_PTR uIdSubclass, DWORD_PTR dwRefData)
{

    switch (message)
    {
    case WM_TOUCH:
        LogInConsole("CATCH A WM_TOUCH!!\n");
        break;
    case WM_GESTURENOTIFY:
        LogInConsole("WM_GESTURENOTIFY!!\n");
        break;      
    case WM_GESTURE:
        LogInConsole("CATCH A WM_GESTURE!!\n");        
        break;
    default:
        return DefWindowProc(hWnd, message, wParam, lParam);
    }
    return DefWindowProc(hWnd, message, wParam, lParam);
}
于 2013-03-29T09:21:42.633 回答