如何克隆图标

在Windows中,给定一个图标HICON )的句柄,你如何克隆它?

例如

 HICON CloneIcon(HICON OriginalIcon) { ICON clone; //... return clone; } 

使用Win32 API函数DuplicateIcon

 HICON CloneIcon(HICON OriginalIcon) { return DuplicateIcon(NULL, OriginalIcon); //first parameter is unused } 

这里是克隆图标的Delphi代码。

 function CloneIcon(ico: HICON): HICON; var info: ICONINFO; bm: BITMAP; cx, cy: Integer; begin //Get the icon's info (eg 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 (eg 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; } 

注意 :任何代码发布到公共领域。 不需要归属