在Windows中,给定图标句柄(HICON
),如何克隆它?
e.g。
HICON CloneIcon(HICON OriginalIcon)
{
ICON clone;
//...
return clone;
}
答案 0 :(得分:2)
使用Win32 API函数DuplicateIcon
:
HICON CloneIcon(HICON OriginalIcon)
{
return DuplicateIcon(NULL, OriginalIcon); //first parameter is unused
}
答案 1 :(得分:1)
这是克隆图标的Delphi代码。
function CloneIcon(ico: HICON): HICON;
var
info: ICONINFO;
bm: BITMAP;
cx, cy: Integer;
begin
//Get the icon's info (e.g. its bitmap)
GetIconInfo(ico, {var}info);
//Get the bitmap info associated with the icon's color bitmap
GetObject(info.hbmColor, sizeof(bm), @bm);
//Copy the actual icon, now that we know its width and height
Result := CopyImage(ico, IMAGE_ICON, bm.bmWidth, bm.bmHeight, 0);
DeleteObject(info.hbmColor);
DeleteObject(info.hbmMask);
end;
在我的头脑中转换为C / C#风格的语言:
HICON CloneIcon(HICON ico)
{
ICONINFO info;
//Get the icon's info (e.g. its bitmap)
GetIconInfo(ico, ref info);
//Get the bitmap info associated with the icon's color bitmap
BITMAP bm;
GetObject(info.hbmColor, sizeof(bm), &bm));
//Copy the actual icon, now that we know its width and height
HICON result = CopyImage(ico, IMAGE_ICON, bm.bmWidth, bm.bmHeight, 0);
DeleteObject(info.hbmColor);
DeleteObject(info.hbmMask);
return result;
}
注意:任何已发布到公共领域的代码。无需归属。