How to determine the size of an icon from a HICON?

前端 未结 2 1704
孤城傲影
孤城傲影 2021-02-13 07:40

I have an icon identified by an HICON handle that I want to draw centered on a custom control.

How do I determine the size of the icon so that I can calcula

2条回答
  •  南笙
    南笙 (楼主)
    2021-02-13 07:43

    The Win32 GetIconInfo call will return, as part of its response, the icon's source bitmap. You can get the icon image size from this.

    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 
    

提交回复
热议问题