1 #include <windows.h> 2 3 LRESULT CALLBACK MyWndProc( HWND hwnd, // handle to window 4 UINT uMsg, // message identifier 5 WPARAM wParam, // first message parameter 6 LPARAM lParam // second message parameter 7 ); 8 9 int WINAPI WinMain( HINSTANCE hInstance, HINSTANCE hPrevInstance, LPSTR lpCmdLine, int nShowCmd ) 10 { 11 WNDCLASS wnd; 12 HWND hwnd; 13 MSG msg; 14 wnd.style = CS_HREDRAW | CS_VREDRAW; //水平或垂直改变窗口都被重绘 与MyWndProc的WM_PAINT消息关联 15 wnd.lpfnWndProc = MyWndProc; 16 wnd.cbClsExtra = 0; 17 wnd.cbWndExtra = 0; 18 wnd.hIcon = LoadIcon(NULL, IDI_APPLICATION); 19 wnd.hCursor = LoadCursor(NULL, IDC_ARROW); 20 wnd.hbrBackground = (HBRUSH)GetStockObject(WHITE_BRUSH); 21 wnd.lpszMenuName = NULL; 22 wnd.lpszClassName = "HelloClass"; //窗口类标识,用在CreateWindow的第一个参数 23 wnd.hInstance = hInstance; 24 if(!RegisterClass(&wnd)) 25 { 26 MessageBox(NULL, TEXT("无法创建窗口"), TEXT("ERROR"), MB_OK|MB_ICONERROR); 27 return 0; 28 } 29 30 hwnd = CreateWindow("HelloClass", TEXT("Hello"), WS_OVERLAPPEDWINDOW, CW_USEDEFAULT, CW_USEDEFAULT, CW_USEDEFAULT, CW_USEDEFAULT, NULL, NULL, hInstance, NULL); //第二个参数为窗口标题 31 ShowWindow(hwnd, nShowCmd); 32 while (GetMessage(&msg, NULL, 0, 0)) 33 { 34 TranslateMessage(&msg); 35 DispatchMessage(&msg); 36 } 37 return 0; 38 } 39 40 LRESULT CALLBACK MyWndProc( HWND hwnd, UINT uMsg, WPARAM wParam, LPARAM lParam) 41 { 42 HDC hdc; //定义设备环境句柄 43 PAINTSTRUCT ps; //绘制结构 44 RECT rect; //矩形结构 45 switch(uMsg) 46 { 47 case WM_PAINT: 48 { 49 hdc = BeginPaint(hwnd, &ps); 50 GetClientRect(hwnd, &rect); 51 DrawText(hdc, TEXT("Hello,World!"), -1, &rect, DT_SINGLELINE|DT_CENTER|DT_VCENTER); 52 EndPaint(hwnd, &ps); 53 } 54 break; 55 case WM_DESTROY: 56 PostQuitMessage(0); 57 return 0; 58 } 59 60 return DefWindowProc(hwnd, uMsg, wParam, lParam); 61 }
windows 程序设计自学:窗口正中显示Hello,World
时间: 2024-10-11 06:07:15