码迷,mamicode.com
首页 > 编程语言 > 详细

非主线程创建窗口也能工作正常,只要我们注意一点:消息循环必须要和创建窗口在同一线程!

时间:2015-02-06 23:03:09      阅读:250      评论:0      收藏:0      [点我收藏+]

标签:

#include "stdafx.h" #include "win32.h" #include "windows.h" #include #define MAX_LOADSTRING 100 HWND g_hWnd = NULL; HINSTANCE g_hInst; LRESULT CALLBACK WndProc(HWND, UINT, 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, NULL, g_hInst, 0); } DWORD ThreadBaseFunc(LPVOID lpParam) { CreateWnd(); MSG msg; while(GetMessage(&msg,NULL,0,0)) { TranslateMessage(&msg); DispatchMessage(&msg); } return 0; } int APIENTRY _tWinMain(HINSTANCE hInstance, HINSTANCE hPrevInstance, LPTSTR lpCmdLine, int nCmdShow) { g_hInst = hInstance; HANDLE hThrd =(HANDLE)_beginthreadex( NULL,0,(unsigned int (__stdcall*)(void*))ThreadBaseFunc,NULL,0,NULL); CloseHandle(hThrd); MSG msg; while(GetMessage(&msg,NULL,0,0)) { TranslateMessage(&msg); DispatchMessage(&msg); } return 0; } LRESULT CALLBACK WndProc(HWND hWnd,UINT wMsg,WPARAM wParam,LPARAM lParam) { return DefWindowProc(hWnd,wMsg,wParam,lParam); }

非主线程创建窗口也能工作正常,只要我们注意一点:消息循环必须要和创建窗口在同一线程!

标签:

原文地址:http://www.cnblogs.com/chunyou128/p/4278164.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!