快速创建窗口,省去创建窗口类,注册窗口的麻烦
[編輯] [转简体] (简体译文)
|
作者:huidong
| 分類:【編程】Win32
[
7 瀏覽
0 評論
0 贊
0 踩
]
概要
CreatWnd();OnMsg();完事!
正文
#include <Windows.h> #include <tchar.h> LRESULT __stdcall WindowProcedure(HWND window, unsigned int msg, WPARAM wp, LPARAM lp) { // 简单的绘图示例中的变量 HDC dc; LPCTSTR str = L"hello world"; switch (msg) { case WM_PAINT: // 简单的绘图示例 dc = GetDC(window); TextOut(dc, 50, 50, str, lstrlen(str)); ReleaseDC(window, dc); default: return DefWindowProc(window, msg, wp, lp); } } // 创建窗口 HWND CreatWnd(LPCTSTR strTitle = _T("title"), LPCTSTR strClass = _T("WndClass")) { // 窗口类 WNDCLASSEX wc; wc.cbSize = sizeof(WNDCLASSEX); /* Win 3.x */ wc.style = CS_DBLCLKS; wc.lpfnWndProc = WindowProcedure; wc.cbClsExtra = 0; wc.cbWndExtra = 0; wc.hInstance = GetModuleHandle(0); wc.hIcon = LoadIcon(0, IDI_APPLICATION); wc.hCursor = LoadCursor(0, IDC_ARROW); wc.hbrBackground = (HBRUSH)(COLOR_WINDOW + 1); wc.lpszMenuName = 0; wc.lpszClassName = strClass; /* Win 4.0 */ wc.hIconSm = LoadIcon(0, IDI_APPLICATION); if (RegisterClassEx(&wc)) { RECT rect; rect.left = 300; rect.top = 300; rect.right = 640; rect.bottom = 480; HWND window = CreateWindowEx(0, strClass, strTitle, WS_OVERLAPPEDWINDOW, rect.left, rect.top, rect.right, rect.bottom, 0, 0, GetModuleHandle(0), 0); if (window) { ShowWindow(window, SW_SHOWDEFAULT); return window; } else { return NULL; } } else { return NULL; } } // 派发消息 void OnMsg() { MSG msg; GetMessage(&msg, 0, 0, 0); DispatchMessage(&msg); } int main() { HWND wnd = CreatWnd(L"My Wnd"); while (true) { OnMsg(); } return 0; }