windows 程式設計自學:視窗正中顯示Hello,World

來源:互聯網
上載者:User

標籤: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 }

 

相關文章

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.