procedure TForm1.Image1Click(Sender: TObject);
var
icon: TIcon;
begin
inc(i);
Image1.Canvas.FillRect(ClientRect);
icon := TIcon.Create;
try
ImageList1.GetIcon(i mod 4, icon);
DrawIconEx(Image1.Canvas.Handle, 0, 0, icon.Handle, Image1.Width, Image1.Height, 0, 0, DI_NORMAL);
finally
icon.Free;
end
end;
更好的方法
有时使用 Delphi 有点尴尬,因为 VCL 和本地 Windows API 之间的合作程度有些不清楚。如果上面的代码不起作用(我感觉它正在泄漏图标),这是一个纯原生方法(uses ImgList, CommCtrl
):
procedure TForm1.Image1Click(Sender: TObject);
var
icon: HICON;
begin
inc(i);
Image1.Canvas.FillRect(ClientRect);
icon := ImageList_GetIcon(ImageList1.Handle, i mod 4, ILD_NORMAL);
try
DrawIconEx(Image1.Canvas.Handle, 0, 0, icon, Image1.Width, Image1.Height, 0, 0, DI_NORMAL);
finally
DestroyIcon(icon);
end
end;