windows 程式設計自學:添加表徵圖資源

來源:互聯網
上載者:User

標籤:des   style   blog   color   os   for   

 1 #include <windows.h> 2 #include "resource.h" 3  4 LRESULT CALLBACK MyWndProc(  HWND hwnd,      // handle to window 5   UINT uMsg,      // message identifier 6   WPARAM wParam,  // first message parameter 7   LPARAM lParam   // second message parameter 8   ); 9 10 int WINAPI WinMain( HINSTANCE hInstance, HINSTANCE hPrevInstance, LPSTR lpCmdLine, int nShowCmd )11 {12     TCHAR szAppName[] = TEXT("IconDemo");13     WNDCLASS wnd;14     HWND hwnd;15     MSG msg;16     wnd.style = CS_HREDRAW | CS_VREDRAW; //水平或垂直改變視窗都被重繪 與MyWndProc的WM_PAINT訊息關聯17     wnd.lpfnWndProc = MyWndProc;18     wnd.cbClsExtra = 0;19     wnd.cbWndExtra = 0;20     wnd.hIcon = LoadIcon(hInstance, MAKEINTRESOURCE(IDI_ICON));21     wnd.hCursor = LoadCursor(NULL, IDC_ARROW);22     wnd.hbrBackground = (HBRUSH)GetStockObject(WHITE_BRUSH);23     wnd.lpszMenuName = NULL;24     wnd.lpszClassName = szAppName; //視窗類別標識,用在CreateWindow的第一個參數25     wnd.hInstance = hInstance;26     if(!RegisterClass(&wnd))27     {28         MessageBox(NULL, TEXT("無法建立視窗"), TEXT("ERROR"), MB_OK|MB_ICONERROR);29         return 0;30     }31     32     hwnd = CreateWindow(szAppName, TEXT("Hello"), WS_OVERLAPPEDWINDOW, CW_USEDEFAULT, CW_USEDEFAULT, CW_USEDEFAULT, CW_USEDEFAULT, NULL, NULL, hInstance, NULL); //第二個參數為視窗標題33     ShowWindow(hwnd, nShowCmd);34     UpdateWindow(hwnd);35     while (GetMessage(&msg, NULL, 0, 0))36     {37         TranslateMessage(&msg);38         DispatchMessage(&msg);39     }40     return 0;41 }42 43 LRESULT CALLBACK MyWndProc(  HWND hwnd, UINT uMsg, WPARAM wParam, LPARAM lParam)44 {45     HDC hdc; //定義裝置環境控制代碼46     PAINTSTRUCT ps; //繪製結構47 48     static HICON hIcon;49     static int cxIcon, cyIcon, cxClient, cyClient;50     HINSTANCE hInstance;51     int x, y;52 53     switch(uMsg)54     {55 56     case WM_CREATE:57     {58         hInstance = ((LPCREATESTRUCT) lParam)->hInstance;//WM_CREATE訊息的lParam參數是一個CREATESTRUCT結構的指標,包含hInstance.59         hIcon = LoadIcon(hInstance, MAKEINTRESOURCE(IDI_ICON));60         cxIcon = GetSystemMetrics(SM_CXICON); //擷取表徵圖大小,上一條LoadIcon關聯61         cyIcon = GetSystemMetrics(SM_CYICON);62         return 0;63     }64     break;65     66     case WM_PAINT:67     {68         hdc = BeginPaint(hwnd, &ps);69         for (y = 0; y < cyClient; y += cyIcon)70         {71             for (x = 0; x < cxClient; x += cxIcon)72             {73                 DrawIcon(hdc, x, y, hIcon); //繪製74             }75             76         }77         EndPaint(hwnd, &ps);78         79     }80     break;81 82     case WM_SIZE:83     {    84         cxClient = LOWORD(lParam);85         cyClient = HIWORD(lParam);86     }87     break;88 89     case WM_DESTROY:90         PostQuitMessage(0);91         return 0;92     }93 94     return DefWindowProc(hwnd, uMsg, wParam, lParam);    95 }

注:參閱windows 程式設計第五版334頁

相關文章

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在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.