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
OFFLINE ICON "gray_button.ico"
ONLINE ICON "green_button.ico"
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';
But beware that this function is only present on Windows Vista+ (IIRC).