delphi 7 TImage和TImageList

时间:2010-11-29 14:33:57

标签: delphi delphi-7 timage timagelist

让我成为整数私有

代码

procedure TForm1.Image1Click(Sender: TObject);
begin
  inc(i);
  ImageList1.GetIcon(i mod 4,Image1.Picture.Icon);
end;

procedure TForm1.FormCreate(Sender: TObject);
begin
  i:=0;
  ImageList1.GetIcon(i mod 4,Image1.Picture.Icon);
end;

如何从列表中拉伸图标以适合Image1的大小?

1 个答案:

答案 0 :(得分:5)

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;