2012-11-02 33 views
5

當註冊窗口類WNDCLASSEX wcex時,我使用wcex.hIcon = LoadIcon(hInstance, (LPCTSTR) IDI_APPLICATION)來設置窗口的圖標。動態加載窗口圖標

有沒有辦法從文件中動態加載圖標來註冊窗口? 類似LoadIcon (hInstance, "iconfile.ico")或可能使用該文件創建圖標資源。

回答

9

您可以使用LoadImage

wcex.hIcon = (HICON) LoadImage(// returns a HANDLE so we have to cast to HICON 
    NULL,    // hInstance must be NULL when loading from a file 
    "iconfile.ico", // the icon file name 
    IMAGE_ICON,  // specifies that the file is an icon 
    0,    // width of the image (we'll specify default later on) 
    0,    // height of the image 
    LR_LOADFROMFILE| // we want to load a file (as opposed to a resource) 
    LR_DEFAULTSIZE| // default metrics based on the type (IMAGE_ICON, 32x32) 
    LR_SHARED   // let the system release the handle when it's no longer used 
); 

確保要麼設置​​3210(小圖標)爲NULL或加載一個小圖標。當它設置爲NULL時,它將自動使用由hIcon指定的圖像。使用LoadImage加載小圖標時,應將寬度和高度設置爲16,並刪除LR_DEFAULTSIZE標誌。如果它是設計爲具有透明部分的圖標,請添加LR_LOADTRANSPARENT標誌

+1

從[documentation](http://msdn.microsoft.com/en-us/library/windows/desktop/ms648045.aspx):請勿使用LR_SHARED用於具有非標準大小的圖像,加載後可能會更改,或者從文件加載。 – Paul