問題描述
使用 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 嗎?的文章就介紹到這了,希望我們推薦的答案對大家有所幫助,也希望大家多多支持html5模板網!