2009-12-16 11 views
13

Mam ikonę oznaczoną za pomocą uchwytu HICON, którą chcę narysować na środku niestandardowego elementu sterującego.Jak określić rozmiar ikony z HICON?

Jak określić rozmiar ikony, aby móc obliczyć prawidłową pozycję rysowania?

Odpowiedz

5

Wywołanie Win32 GetIconInfo powróci, jako część odpowiedzi, do mapy bitowej źródła ikony. Możesz uzyskać rozmiar obrazu ikony z tego.

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

Dziękuję, działa dobrze z moich kolorowych ikon. Jaki jest cel "Wysokość = BMInf.bmHeight \ 2" w przypadku maski? – Timbo

+0

@Timbo: patrz msdn: ICONINFO, ikona monochromatyczna zawiera obraz i maskę XOR w hbmMask. – peterchen

8

Oto C++ wersja kodu:

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; 
} 
Powiązane problemy