2009-12-16 43 views
13

我有一個由HICON句柄標識的圖標,我想以自定義控件爲中心進行繪製。如何確定HICON圖標的大小?

如何確定圖標的大小,以便我可以計算出正確的圖紙位置?

回答

5

作爲響應的一部分,Win32 GetIconInfo調用將返回圖標的源位圖。您可以從中獲取圖標圖像大小。

Dim IconInf As IconInfo 
Dim BMInf As Bitmap 

If (GetIconInfo(hIcon, IconInf)) Then 
    If (IconInf.hbmColor) Then ' Icon has colour plane 
     If (GetObject(IconInf.hbmColor, Len(BMInf), BMInf)) Then 
      Width = BMInf.bmWidth 
      Height = BMInf.bmHeight 
      BitDepth = BMInf.bmBitsPixel 
     End If 

     Call DeleteObject(IconInf.hbmColor) 
    Else ' Icon has no colour plane, image data stored in mask 
     If (GetObject(IconInf.hbmMask, Len(BMInf), BMInf)) Then 
      Width = BMInf.bmWidth 
      Height = BMInf.bmHeight \ 2 
      BitDepth = 1 
     End If 
    End If 

    Call DeleteObject(IconInf.hbmMask) 
End If 
+0

謝謝,正常工作與我的彩色圖標。掩碼中「Height = BMInf.bmHeight \ 2」的用途是什麼? – Timbo 2009-12-16 09:53:12

+0

@Timbo:參見msdn:ICONINFO,一個單色圖標包含hbmMask中的圖像和XOR掩碼。 – peterchen 2011-01-18 07:35:57

8

這裏是一個代碼C++版本:

struct MYICON_INFO 
{ 
    int  nWidth; 
    int  nHeight; 
    int  nBitsPerPixel; 
}; 

MYICON_INFO MyGetIconInfo(HICON hIcon); 

// ======================================= 

MYICON_INFO MyGetIconInfo(HICON hIcon) 
{ 
    MYICON_INFO myinfo; 
    ZeroMemory(&myinfo, sizeof(myinfo)); 

    ICONINFO info; 
    ZeroMemory(&info, sizeof(info)); 

    BOOL bRes = FALSE; 

    bRes = GetIconInfo(hIcon, &info); 
    if(!bRes) 
     return myinfo; 

    BITMAP bmp; 
    ZeroMemory(&bmp, sizeof(bmp)); 

    if(info.hbmColor) 
    { 
     const int nWrittenBytes = GetObject(info.hbmColor, sizeof(bmp), &bmp); 
     if(nWrittenBytes > 0) 
     { 
      myinfo.nWidth = bmp.bmWidth; 
      myinfo.nHeight = bmp.bmHeight; 
      myinfo.nBitsPerPixel = bmp.bmBitsPixel; 
     } 
    } 
    else if(info.hbmMask) 
    { 
     // Icon has no color plane, image data stored in mask 
     const int nWrittenBytes = GetObject(info.hbmMask, sizeof(bmp), &bmp); 
     if(nWrittenBytes > 0) 
     { 
      myinfo.nWidth = bmp.bmWidth; 
      myinfo.nHeight = bmp.bmHeight/2; 
      myinfo.nBitsPerPixel = 1; 
     } 
    } 

    if(info.hbmColor) 
     DeleteObject(info.hbmColor); 
    if(info.hbmMask) 
     DeleteObject(info.hbmMask); 

    return myinfo; 
}