最简单的windows GUI程序,由WMain.cpp和WinProc.cpp组成,前者负责初始化窗口,负责处理消息。
File: WMain.cpp
#include <windows.h> extern LRESULT CALLBACK WindowF(HWND, UINT, WPARAM, LPARAM); char szWinName[] = "MyWin"; char szTitle[] = "My WINDOW"; int WINAPI WinMain(HINSTANCE hThisInst, HINSTANCE hPrevInst, LPSTR lpszArgs, int nWinMode) { // Define window class HWND hwnd; MSG msg; WNDCLASSEX wcl; wcl.cbClsExtra = 0; // extra bytes wcl.cbSize = sizeof(WNDCLASSEX); wcl.cbWndExtra = 0; wcl.hbrBackground = (HBRUSH)GetStockObject(WHITE_BRUSH); wcl.hCursor = LoadCursor(NULL, IDC_ARROW); wcl.hIcon = LoadIcon(NULL, IDI_WINLOGO); wcl.hIconSm = NULL; wcl.hInstance = hThisInst; wcl.lpfnWndProc = WindowF; wcl.lpszClassName = szWinName; wcl.lpszMenuName = NULL; wcl.style = 0; // Registration of class if (!RegisterClassEx(&wcl)) return 0; // Create Window hwnd = CreateWindow( szWinName, szTitle, WS_OVERLAPPEDWINDOW | WS_HSCROLL | WS_VSCROLL | WS_SYSMENU, CW_USEDEFAULT, CW_USEDEFAULT, CW_USEDEFAULT, CW_USEDEFAULT, HWND_DESKTOP, NULL, hThisInst, NULL ); ShowWindow(hwnd, SW_RESTORE); UpdateWindow(hwnd); // Serve Messages while (GetMessage(&msg, NULL, 0, 0)) { TranslateMessage(&msg); DispatchMessage(&msg); } return msg.wParam; }
File: WinProc.cpp
#include <windows.h> LRESULT CALLBACK WindowF(HWND hwnd, UINT message, WPARAM wParam, LPARAM lParam) { switch (message) { case WM_DESTROY: // User Closed the window PostQuitMessage(0); break; default: break; } // Call the default window handler return DefWindowProc(hwnd, message, wParam, lParam); }
运行结果示例:
时间: 2024-10-23 20:12:43