Can the HWND from CreateWindow/CreateDialog be GetMessage#39;d from another thread?(CreateWindow/CreateDialog 中的 HWND 可以从另一个线程获取 GetMessage 吗?)
问题描述
使用 Win32 API,是否可以在一个线程中创建一个窗口或对话框,然后从另一个线程为它收集事件?
HWND 是否与线程相关联?
尝试下面的人为示例,我从未看到 GetMessage() 触发.
<前>HWND g_hWnd;DWORD WINAPI myThreadProc(LPVOID lpParam){while(GetMessage(&msg, hWnd, 0, 0) > 0){...}}int WINAPI WinMain(HINSTANCE hInstance, HINSTANCE hPrevInstance, LPSTR lpCmdLine, int nShowCmd){hWnd = CreateDialog(hInstance, MAKEINTRESOURCE(IDD_MYDIALOG), 0, myDlgProc);CreateThread(NULL, 0 myThreadProc, NULL, 0, NULL);...}但在这里,我愿意.
<前>HWND g_hWnd;HINSTANCE g_hInstance;DWORD WINAPI myThreadProc(LPVOID lpParam){hWnd = CreateDialog(hInstance, MAKEINTRESOURCE(IDD_MYDIALOG), 0, myDlgProc);while(GetMessage(&msg, hWnd, 0, 0) > 0){...}}int WINAPI WinMain(HINSTANCE hInstance, HINSTANCE hPrevInstance, LPSTR lpCmdLine, int nShowCmd){g_hInstance = hInstance;CreateThread(NULL, 0 myThreadProc, NULL, 0, NULL);...}有人能解释一下我看到了什么吗?
没有
GetMessage 在当前线程的输入队列上返回消息.HWND 参数是一个过滤器,因此 GetMessage 只返回当前线程的输入队列中用于该窗口的消息.
Windows 具有线程关联性 - 用于窗口的消息在创建并因此拥有该窗口的线程上得到处理.
Using the Win32 APIs, is it possible to create a Window or Dialog in one thread then collect events for it from another thread?
Are HWNDs tied to threads?
Trying the contrived example below I never see GetMessage() fire.
HWND g_hWnd; DWORD WINAPI myThreadProc(LPVOID lpParam) { while(GetMessage(&msg, hWnd, 0, 0) > 0) { ... } } int WINAPI WinMain(HINSTANCE hInstance, HINSTANCE hPrevInstance, LPSTR lpCmdLine, int nShowCmd) { hWnd = CreateDialog(hInstance, MAKEINTRESOURCE(IDD_MYDIALOG), 0, myDlgProc); CreateThread(NULL, 0 myThreadProc, NULL, 0, NULL); ... }
But here, I do.
HWND g_hWnd; HINSTANCE g_hInstance; DWORD WINAPI myThreadProc(LPVOID lpParam) { hWnd = CreateDialog(hInstance, MAKEINTRESOURCE(IDD_MYDIALOG), 0, myDlgProc); while(GetMessage(&msg, hWnd, 0, 0) > 0) { ... } } int WINAPI WinMain(HINSTANCE hInstance, HINSTANCE hPrevInstance, LPSTR lpCmdLine, int nShowCmd) { g_hInstance = hInstance; CreateThread(NULL, 0 myThreadProc, NULL, 0, NULL); ... }
Can somebody explain what I'm seeing?
No.
GetMessage returns messages on the current thread's input queue. The HWND parameter is a filter, so that GetMessage only returns messages in the current thread's input queue intended for that window.
Windows have thread affinity - messages intended for a window get handled on the thread that created and therefore owns the window.
这篇关于CreateWindow/CreateDialog 中的 HWND 可以从另一个线程获取 GetMessage 吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!
本文标题为:CreateWindow/CreateDialog 中的 HWND 可以从另一个线程获


- 静态初始化顺序失败 2022-01-01
- C++ 协变模板 2021-01-01
- Stroustrup 的 Simple_window.h 2022-01-01
- 与 int by int 相比,为什么执行 float by float 矩阵乘法更快? 2021-01-01
- 使用/clr 时出现 LNK2022 错误 2022-01-01
- 一起使用 MPI 和 OpenCV 时出现分段错误 2022-01-01
- 近似搜索的工作原理 2021-01-01
- STL 中有 dereference_iterator 吗? 2022-01-01
- 从python回调到c++的选项 2022-11-16
- 如何对自定义类的向量使用std::find()? 2022-11-07