如何确定 HICON 中图标的大小?

发布于 2024-08-14 17:36:41 字数 86 浏览 1 评论 0原文

我有一个由 HICON 句柄标识的图标,我想将其绘制在自定义控件的中心。

如何确定图标的大小以便计算正确的绘图位置?

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?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

怪我太投入 2024-08-21 17:36:41

这是代码的 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;
}

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;
}
烟凡古楼 2024-08-21 17:36:41

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 

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 
本宫微胖 2024-08-21 17:36:41

这是代码的 Python 版本:

import win32gui

hIcon = some_icon
info = win32gui.GetIconInfo(hIcon)

if info:
    if info[4]: # Icon has colour plane
        bmp = win32gui.GetObject(info[4])
        
        if bmp:
            Width = bmp.bmWidth
            Height = bmp.bmHeight
            BitDepth = bmp.bmBitsPixel
    else: # Icon has no colour plane, image data stored in mask
        bmp = win32gui.GetObject(info[4])
        
        if bmp:
            Width = bmp.bmWidth
            Height = bmp.bmHeight // 2
            BitDepth = 1

    info[3].close()
    info[4].close()

Here is a Python version of the code:

import win32gui

hIcon = some_icon
info = win32gui.GetIconInfo(hIcon)

if info:
    if info[4]: # Icon has colour plane
        bmp = win32gui.GetObject(info[4])
        
        if bmp:
            Width = bmp.bmWidth
            Height = bmp.bmHeight
            BitDepth = bmp.bmBitsPixel
    else: # Icon has no colour plane, image data stored in mask
        bmp = win32gui.GetObject(info[4])
        
        if bmp:
            Width = bmp.bmWidth
            Height = bmp.bmHeight // 2
            BitDepth = 1

    info[3].close()
    info[4].close()
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文