我有一个HICON
手柄标识的图标,我想以自定义控件为中心绘制。
如何确定图标的大小以便我可以计算出正确的绘图位置?
答案 0 :(得分: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;
}
答案 1 :(得分: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
答案 2 :(得分:0)
这是代码的 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()