Winapi 如何从HICON确定图标的大小?
我有一个由Winapi 如何从HICON确定图标的大小?,winapi,icons,Winapi,Icons,我有一个由HICON句柄标识的图标,我想在自定义控件的中心绘制该图标 如何确定图标的大小,以便计算正确的绘图位置?Win32GetIconInfo调用将返回图标的源位图,作为其响应的一部分。您可以从中获取图标图像大小 Dim IconInf作为IconInfo 将BMInf设置为位图 如果(GetIconInfo(hIcon,IconInf))那么 如果(IconInf.hbmColor),则“图标具有颜色平面” 如果是(GetObject(IconInf.hbmColor,Len(BMInf)
HICON
句柄标识的图标,我想在自定义控件的中心绘制该图标
如何确定图标的大小,以便计算正确的绘图位置?Win32
GetIconInfo
调用将返回图标的源位图,作为其响应的一部分。您可以从中获取图标图像大小
Dim IconInf作为IconInfo
将BMInf设置为位图
如果(GetIconInfo(hIcon,IconInf))那么
如果(IconInf.hbmColor),则“图标具有颜色平面”
如果是(GetObject(IconInf.hbmColor,Len(BMInf),BMInf)),那么
宽度=BMInf.bmWidth
高度=BMInf.bmHeight
BitDepth=BMInf.bmBitsPixel
如果结束
调用DeleteObject(IconInf.hbmColor)
Else’图标没有颜色平面,图像数据存储在遮罩中
如果是(GetObject(IconInf.hbmMask,Len(BMInf),BMInf)),那么
宽度=BMInf.bmWidth
高度=BMInf.bmHeight\2
比特深度=1
如果结束
如果结束
调用DeleteObject(IconInf.hbmMask)
如果结束
<代码> < p>这里是代码的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;
}
以下是Python版本的代码:
导入win32gui
hIcon=某个图标
info=win32gui.GetIconInfo(hIcon)
如果信息:
如果信息[4]:#图标有颜色平面
bmp=win32gui.GetObject(信息[4])
如果bmp:
宽度=bmp.bmWidth
高度=bmp.bmHeight
BitDepth=bmp.bmBitsPixel
否则:#图标没有颜色平面,图像数据存储在遮罩中
bmp=win32gui.GetObject(信息[4])
如果bmp:
宽度=bmp.bmWidth
高度=bmp.bmHeight//2
比特深度=1
信息[3]。关闭()
信息[4]。关闭()
谢谢,我的彩色图标很好用。掩码框中“Height=BMInf.bmHeight\2”的用途是什么?@Timbo:请参阅msdn:ICONINFO,单色图标在hbmMask中包含图像和XOR掩码。