在副线程中创建窗体.doc_第1页
在副线程中创建窗体.doc_第2页
在副线程中创建窗体.doc_第3页
在副线程中创建窗体.doc_第4页
全文预览已结束

下载本文档

版权说明:本文档由用户提供并上传,收益归属内容提供方,若内容存在侵权,请进行举报或认领

文档简介

在非主线程中创建窗口/=/TITLE: / 在非主线程中创建窗口 /AUTHOR: / norains /DATE: / Saturday 29-December-2007 /Environment: / VS2005 + SDK-WINCE5.0-MIPSII /= 很多朋友都会有过这样的经历,为什么在主线程中创建窗口且窗口工作很正常,但一移到非主线程(有的朋友喜欢叫它为工作线程),却无法正常工作.本文就这个问题和各位探讨,可能无法做到尽善尽美,但能抛砖引玉也算是欣慰了. 在主线程中创建一个能够正常工作的窗口,估计地球人都知道. 这是一段工作正常的代码: #include windows.h HWND g_hWnd = NULL; HINSTANCE g_hInst; LRESULT WndProc(HWND hWnd,UINT wMsg,WPARAM wParam,LPARAM lParam) return DefWindowProc(hWnd,wMsg,wParam,lParam); void CreateWnd(void) WNDCLASS wc = 0; wc.style = 0; wc.lpfnWndProc = WndProc; wc.cbClsExtra = 0; wc.cbWndExtra = 0; wc.hInstance = g_hInst; wc.hIcon = NULL; wc.hCursor = LoadCursor(NULL, IDC_ARROW); wc.hbrBackground = (HBRUSH)GetSysColorBrush(COLOR_WINDOW); wc.lpszMenuName = NULL; wc.lpszClassName = TEXT(SimpleWindow); RegisterClass(&wc); g_hWnd = CreateWindowEx(0, TEXT(SimpleWindow), TEXT(SimpleWindow), WS_VISIBLE, 0, 0, 200, 200, NULL, Page NULL, g_hInst, 0); int WINAPI WinMain( HINSTANCE hInstance, HINSTANCE hPrevInstance, LPTSTR lpCmdLine, int nCmdShow) / TODO: Place code here. g_hInst = hInstance; CreateWnd(); /The message loop MSG msg; while(GetMessage(&msg,NULL,0,0) TranslateMessage(&msg); DispatchMessage(&msg); return 0; 如果我们创建一个线程,然后在这个线程中创建窗口,看看带给我们的是什么: #include windows.h HWND g_hWnd = NULL; HINSTANCE g_hInst; LRESULT WndProc(HWND hWnd,UINT wMsg,WPARAM wParam,LPARAM lParam) return DefWindowProc(hWnd,wMsg,wParam,lParam); void CreateWnd(void) WNDCLASS wc = 0; wc.style = 0; wc.lpfnWndProc = WndProc; wc.cbClsExtra = 0; wc.cbWndExtra = 0; wc.hInstance = g_hInst; wc.hIcon = NULL; wc.hCursor = LoadCursor(NULL, IDC_ARROW); wc.hbrBackground = (HBRUSH)GetSysColorBrush(COLOR_WINDOW); wc.lpszMenuName = NULL; wc.lpszClassName = TEXT(SimpleWindow); RegisterClass(&wc); g_hWnd = CreateWindowEx(0, Page TEXT(SimpleWindow), TEXT(SimpleWindow), WS_VISIBLE, 0, 0, 200, 200, NULL, NULL, g_hInst, 0); DWORD CreateThread(PVOID pArg) CreateWnd(); return 0; int WINAPI WinMain( HINSTANCE hInstance, HINSTANCE hPrevInstance, LPTSTR lpCmdLine, int nCmdShow) / TODO: Place code here. g_hInst = hInstance; HANDLE hThrd = CreateThread(NULL,0,CreateThread,NULL,0,NULL); CloseHandle(hThrd); /The message loop MSG msg; while(GetMessage(&msg,NULL,0,0) TranslateMessage(&msg); DispatchMessage(&msg); return 0; 我们似乎什么都没见到,只是窗口一闪,啥都没了.因为g_hWnd为全局变量,我们的理智告诉我们,在主线程没有退出之前,g_hWnd是不会销毁的.而用断点调试,将会发现在WndProc函数中只能接收WM_CREATE及以后一些消息,之后的再也收不到了,特别是WM_PAINT似乎就凭空消失了!那么,代码什么都没变更,只是移动到了分线程中,为何会出现这个问题呢? Page 一切似乎很简单,在MSDN中我们找到了答案(原文见:/kb/90975/en-us): In a multithreaded application, any thread can call the CreateWindow() API to create a window. There are no restrictions on which thread(s) can create windows. It is important to note that the message loop and window procedure for the window must be in the thread that created the window. If a different thread creates the window, the window wont get messages from DispatchMessage(), but will get messages from other sources. Therefore, the window will appear but wont show activation or repaint, cannot be moved, wont receive mouse messages, and so on. 该段话大意是:窗口在任何线程中都可以创建,但消息循环必须要和创建窗口在同一线程,否则窗口将无法从DispatchMessage()获取任何消息! 原来如此,最重要是这么一句:It is important to note that the message loop and window procedure for the window must be in the thread that created the window. 好吧,那么我们在支线程中放置消息循环代码,看看是什么结果吧: #include windows.h HWND g_hWnd = NULL; HINSTANCE g_hInst; LRESULT WndProc(HWND hWnd,UINT wMsg,WPARAM wParam,LPARAM lParam) return DefWindowProc(hWnd,wMsg,wParam,lParam); void CreateWnd(void) WNDCLASS wc = 0; wc.style = 0; wc.lpfnWndProc = WndProc; wc.cbClsExtra = 0; wc.cbWndExtra = 0; wc.hInstance = g_hInst; wc.hIcon = NULL;wc.hCursor = LoadCursor(NULL, IDC_ARROW); wc.hbrBackground = (HBRUSH)GetSysColorBrush(COLOR_WINDOW); wc.lpszMenuName = NULL; wc.lpszClassName = TEXT(SimpleWindow); RegisterClass(&wc); g_hWnd = CreateWindowEx(0, TEXT(SimpleWindow), TEXT(SimpleWindow), Page WS_VISIBLE, 0, 0, 200, 200, NULL, NULL, g_hInst, 0); DWORD CreateThread(PVOID pArg) CreateWnd(); /The message loop MSG msg; while(GetMessage(&msg,NULL,0,0) TranslateMessage(&msg); DispatchMessage(&msg); return 0; int WINAPI WinMain( HINSTANCE hInstance, HINSTANCE hPrevInstance, LPTSTR lpCmdLine, int nCmdShow) / TODO: Place code here. g_hInst = hInstance; HANDLE hThrd = CreateThread(NULL,0,CreateThread,NULL,0,NULL); CloseHandle(hThrd); MSG msg; while(GetMessage(&msg,NULL,0,0) TranslateMessage(&msg); DispatchMessage(&msg); return 0; 一切正常,如同在主线程创建一样! 当然了,还有点需要注意的,在这个例子中,由于

温馨提示

  • 1. 本站所有资源如无特殊说明,都需要本地电脑安装OFFICE2007和PDF阅读器。图纸软件为CAD,CAXA,PROE,UG,SolidWorks等.压缩文件请下载最新的WinRAR软件解压。
  • 2. 本站的文档不包含任何第三方提供的附件图纸等,如果需要附件,请联系上传者。文件的所有权益归上传用户所有。
  • 3. 本站RAR压缩包中若带图纸,网页内容里面会有图纸预览,若没有图纸预览就没有图纸。
  • 4. 未经权益所有人同意不得将文件中的内容挪作商业或盈利用途。
  • 5. 人人文库网仅提供信息存储空间,仅对用户上传内容的表现方式做保护处理,对用户上传分享的文档内容本身不做任何修改或编辑,并不能对任何下载内容负责。
  • 6. 下载文件中如有侵权或不适当内容,请与我们联系,我们立即纠正。
  • 7. 本站不保证下载资源的准确性、安全性和完整性, 同时也不承担用户因使用这些下载资源对自己和他人造成任何形式的伤害或损失。

评论

0/150

提交评论