本文介绍了如何将图标从资源加载到TImage?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我尝试以下代码,但无法正常工作... LoadIconWithScaleDown 返回错误代码.

I try the following code and it's not working... LoadIconWithScaleDown returns a negative error code.

unit Unit1;

interface

uses
  Winapi.Windows, Winapi.Messages, System.SysUtils, System.Variants, System.Classes, Vcl.Graphics,
  Vcl.Controls, Vcl.Forms, Vcl.Dialogs, Vcl.ExtCtrls,
  Vcl.StdCtrls;

type
  TForm1 = class(TForm)
    Image1: TImage;
    procedure FormCreate(Sender: TObject);
    procedure LoadResToImg(RID: String; const Img: TImage);
  end;

var
  Form1: TForm1;

implementation

{$R *.dfm}
{$R UserResources.res}

uses Winapi.CommCtrl;

procedure TForm1.LoadResToImg(RID: String; const Img: TImage);
var Ico: TIcon;
    hI: HICON;
    HR: HResult;
begin
 Ico:= TIcon.Create;
 HR:= LoadIconWithScaleDown(HInstance, PChar(RID), Img.Width, Img.Height, hI);
 Ico.Handle:= hI;
 Img.Picture.Bitmap.Assign(Ico);
 Ico.Free;
end;

procedure TForm1.FormCreate(Sender: TObject);
begin
 LoadResToImg('OFFLINE', Image1);
end;

end.

UserResources.rc

UserResources.rc

OFFLINE      ICON    "gray_button.ico"
ONLINE       ICON    "green_button.ico"

推荐答案

这可能是因为该Win32函数的VCL包装器(在 Winapi.CommCtrl.pas 中)有故障,或者至少不能立即使用.

This is probably because the VCL's wrapper (in Winapi.CommCtrl.pas) for this Win32 function is faulty, or at least cannot be used straight away.

因此,您可以自己声明:

So instead declare it yourself:

function LoadIconWithScaleDown(hinst: HINST; pszName: LPCWSTR; cx: Integer;
    cy: Integer; var phico: HICON): HResult; stdcall; external 'ComCtl32';

但是请注意,此功能仅在Windows Vista +(IIRC)中存在.

But beware that this function is only present on Windows Vista+ (IIRC).

这篇关于如何将图标从资源加载到TImage?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

07-25 07:00