標籤:des style blog color os io
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 }