gpt4 book ai didi

c++ - 无法在 Hook 中获取 WM_TOUCH 消息

转载 作者:行者123 更新时间:2023-11-28 07:41:55 27 4
gpt4 key购买 nike

我需要让我的应用程序兼容 Touch,这就是我最近深入研究 Windows Touch API 的原因。

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

这是我的问题:我已经下载了 Microsoft Sample of a simple Gesture App .它运行良好,但手势功能是在绑定(bind)到 HWND 创建的 WinProc 中添加处理的。问题是我的“真实”应用程序自行创建了它的 HWND,这就是为什么我想使用 Hook 来接收 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 中。事实上,我只在 Hook 中收到一条 WM_GESTURENOTIFY 消息,通知手势已经开始,但永远不会捕获以下应该给出值的“WM_TOUCH”消息......

有没有人遇到过这个问题?

提前感谢您的帮助

最佳答案

感谢 Raymond 的帮助。子类化是解决方案!所以最后我删除了所有的钩子(Hook)并像这样制作:

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

关于c++ - 无法在 Hook 中获取 WM_TOUCH 消息,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/15680766/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com