我已经测试了创建一个默认窗口,并且我已经在与主线程不同的线程上测试了一个仅消息窗口。
目标是从主线程外部接收来自HID设备的
WM_INPUT
。原因是主线程循环的运行速度比处理输入所需的速度慢得多。
只要主线程的主窗口不是活动窗口,两个窗口似乎都可以工作。
这个问题有什么可能的解决方案?
编辑:
我没有添加代码,因为它基本上是样板代码,但由于许多评论希望看到一些代码,我将添加它,因为它可能真的很有帮助。
//FIRST: CLASS REGISTRATION:
WNDCLASSEXW wcex;
wchar_t local_name[] = L"ClassGenericName";
wcsncat(local_name,
std::to_wstring(GetCurrentThreadId()).c_str(),
std::to_wstring(GetCurrentThreadId()).size());
//You can ignore naming process as it was made to make more than one window for more than one thread, just for testing purposes.
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_WINAPITEST));
wcex.hCursor = LoadCursor(nullptr, IDC_ARROW);
wcex.hbrBackground = (HBRUSH)(COLOR_WINDOW + 1);
wcex.lpszMenuName = MAKEINTRESOURCEW(IDC_WINAPITEST);
wcex.lpszClassName = local_name;
wcex.hIconSm = LoadIcon(wcex.hInstance, MAKEINTRESOURCE(IDI_SMALL));
RegisterClassExW(&wcex);
hWnd = CreateWindowEx(0, wcex.lpszClassName, L"no title", 0, 0, 0, 0, 0, HWND_MESSAGE, 0, 0, 0);
ShowWindow(hWnd, 3);//Testing, I switch it on and off.
UpdateWindow(hWnd);
//SECOND: DEVICE REGISTRATION:
RAWINPUTDEVICE RID;
RID.usUsagePage = 0x01;
RID.usUsage = 0x05;
RID.dwFlags = 0;
RID.hwndTarget = 0;
RegisterRawInputDevices(&RID, 1, sizeof(RID));
//THIRD: THREAD FUNCTION MESSAGE LOOP.
HACCEL hAccelTable = LoadAccelerators(hInstance, MAKEINTRESOURCE(IDC_WINAPITEST));
MSG msg;
while (GetMessageW(&msg, nullptr, 0, 0))
{
if (!TranslateAccelerator(msg.hwnd, hAccelTable, &msg))
{
TranslateMessage(&msg);
DispatchMessage(&msg);
}
}
//FOURTH: WINDOW PROCEDURE MESSAGE PROCESSING.
//I don't think I need to add this as it is boilerplate and tested to work all the time. If wanted I'd add it tho.
//FIVE: Thanks for helpful comments. My bad for not adding code ahead of time.
编辑:
解决:注册时设置HID目标窗口句柄的问题。我将它设置为 null,这使得活动窗口成为目标。相反,我应该将它设置为单独线程的窗口。我做到了,它奏效了。
解决:注册时设置HID目标窗口句柄的问题。我将它设置为 null,这使得活动窗口成为目标。相反,我应该将它设置为单独线程的窗口。我做到了,它奏效了。