Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

Tags:

icons

winapi

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 calculate the correct drawing position?

like image 904
Timbo Avatar asked Dec 16 '09 09:12

Timbo


People also ask

How do I find the size of an icon?

Right-click (or press and hold) the desktop, point to View, and then select Large icons, Medium icons, or Small icons.

What is the perfect icon size?

The most common sizes are 16x16, 24x24, 32x32, 48x48, 64x64, 96x96; The size of the grid depends on the reason for using icons, guidelines and operating system features. For example, for Android, the grid size is 24 x 24 dp.


2 Answers

Here is a C++ version of code:

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;
}
like image 166
Sergey Avatar answered Oct 09 '22 18:10

Sergey


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 
like image 36
Jez Avatar answered Oct 09 '22 20:10

Jez