开发者

delphi 7 TImage and TImageList

let i be integer private

the code

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;

how do i stretch the icon from the list to fit the size of Image1?


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;

Better Approach

Sometimes it is a bit awkward to use Delphi since the extent of cooperation between the VCL and the native Windows API is somewhat unclear. If the above code doesn't work (I get the feeling it is leaking icons), here is a pure native approach (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;
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜