如何使用Delphi从另一个文件的资源加载单个图标?
我想加载一个图标(来自另一个文件),该图标没有嵌入多个图标(它不是图标组)。 我不知道它的大小。 我现在使用此代码来检索图标的句柄并将其与 TIcon.Handle 一起使用:
function ResourceToIconHandle(hFile: hModule; IDname: PChar): HICON;
var
hGicon1,
hLoadIcon1: THandle;
pGIcon1: Pointer;
begin
hGicon1 := FindResource(hFile, IDName, RT_ICON);
if hGicon1 <> 0 then
begin
hLoadIcon1 := LoadResource(hFile, hGicon1);
pGicon1 := LockResource(hLoadIcon1);
Result := CreateIconfromResource(pGicon1,
SizeofResource(hFile, hGicon1),
True,
$00030000);
end;
end;
是的,它只是代码的一部分(如果您愿意,我将显示全部)。 它只适用于一个问题:CreateIconfromResource 函数给我任何拉伸为 32x32 的图标:
我知道 CreateIconfromResource 的设计目的是让它们具有相同的分辨率,这就是我正在寻找另一个函数的原因。 感谢您的帮助。
I want to load an icon (from another file) which doesn't have multiple icons embedded in it (it's not an icon group).
I don't know its size.
I use now this code to retrieve the handle of the icon and use it with a TIcon.Handle:
function ResourceToIconHandle(hFile: hModule; IDname: PChar): HICON;
var
hGicon1,
hLoadIcon1: THandle;
pGIcon1: Pointer;
begin
hGicon1 := FindResource(hFile, IDName, RT_ICON);
if hGicon1 <> 0 then
begin
hLoadIcon1 := LoadResource(hFile, hGicon1);
pGicon1 := LockResource(hLoadIcon1);
Result := CreateIconfromResource(pGicon1,
SizeofResource(hFile, hGicon1),
True,
$00030000);
end;
end;
Yes, it's only a part of the code (if you want I'll show all).
It works with only one problem: CreateIconfromResource function is giving me any icon streched at 32x32:
But I want to get the icons at their original resolution:
I know that CreateIconfromResource is designed to get them at the same resolution, that's why I'm looking for another function.
Thank you for your help.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
使用
CreateIconFromResourceEx
而不是CreateIconFromResource
。CreateIconFromResourceEx
允许您提供所需的宽度/高度,而CreateIconFromResource
使用默认系统指标(例如LR_DEFAULTSIZE
中的解释):Use
CreateIconFromResourceEx
instead ofCreateIconFromResource
.CreateIconFromResourceEx
lets you provide desired width/height, whileCreateIconFromResource
is using default system mertics for those (such as explained forLR_DEFAULTSIZE
):Roman R. 可能是对的,但我还补充说,您必须在设置 TIcon 对象的句柄之前设置其正确的尺寸。
Roman R. is probably right, but I also add that you must set proper dimensions of TIcon object before setting its Handle.