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


    #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); }
  • 相关阅读:
    享元模式及php实现
    共享内存
    LCD触屏驱动
    I2C驱动
    C++ & java小结
    使用GlobalKey启动APP
    socketpair通信
    inotify和epoll
    C语言之二叉树
    灯光系统
  • 原文地址:https://www.cnblogs.com/chunyou128/p/4278164.html
Copyright © 2020-2023  润新知