Sign in Join
Wiki copytemporary

TImageList

English · Français · Русский · 中文(中国)

TImageList is a component that provides a list of images that can be shared between other components like TMainMenu/TMenuItem, TToolBar/TToolButton, TActionList, TBitBtn or TSpeedButton. All images have the same size as specified by the Width and Height property of TImageList. A component needing an image specifies this by means of its ImageIndex property.

Multiple-resolution TImageList in Lazarus 1.9 and newer

In Lazarus 1.9 the TImageList was rewritten to support multiple resolutions of one image.

Every LCL control that supports an ImageList has now a new ImagesWidth property to decide what custom width at 96 PPI (100% scale) is to be used. Example: TToolBar.Images/ImageWidth, TListView.LargeImages/LargeImagesWidth.

Set the Scaled property to True and the image list will automatically pick up the scaled image in your High-DPI aware LCL application (provided that the option "Use LCL Scaling" has been checked in the project options).

Demo applications

There are 2 demo applications in Lazarus sources:

Changes affecting backwards compatibility

Add method

How to create a multi-resolution ImageList in the application

pascal
procedure TForm1.ImageList1GetWidthForPPI(Sender: TCustomImageList;
  AImageWidth, APPI: Integer; var AResultWidth: Integer);
begin
  AResultWidth := AImageWidth * APPI div 96;
end;

High-DPI alternative for Lazarus 1.8 and older: resize all images in TImageList

This is useful for example for High DPI scaling to get icons with higher resolution for TActionList, TMainMenu and TToolBar. Note: This may not work correctly with all widgetsets on all platforms. Note also: This code is not required for Laz 1.9+.

pascal
procedure ScaleImageList(ImgList: TImageList; NewWidth, NewHeight: Integer);
var
  TempImgList: TImageList;
  TempBmp1: TBitmap;
  TempBmp2: TBitmap;
  I: Integer;
begin
  TempImgList := TImageList.Create(nil);
  TempBmp1 := TBitmap.Create;
  TempBmp1.PixelFormat := pf32bit;
  TempBmp2 := TBitmap.Create;
  TempBmp2.PixelFormat := pf32bit;
  TempBmp2.SetSize(NewWidth, NewHeight);
  try
    TempImgList.Width := NewWidth;
    TempImgList.Height := NewHeight;

    for I := 0 to ImgList.Count - 1 do begin
      // Load image for given index to temporary bitmap
      ImgList.GetBitmap(I, TempBmp1);

      // Clear transparent image background
      TempBmp2.Canvas.Brush.Style := bsSolid;
      TempBmp2.Canvas.Brush.Color := TempBmp2.TransparentColor;
      TempBmp2.Canvas.FillRect(0, 0, TempBmp2.Width, TempBmp2.Height);

      // Stretch image to new size
      TempBmp2.Canvas.StretchDraw(Rect(0, 0, TempBmp2.Width, TempBmp2.Height), TempBmp1);
      TempImgList.Add(TempBmp2, nil);
    end;

    ImgList.Assign(TempImgList);
  finally
    TempImgList.Free;
    TempBmp1.Free;
    TempBmp2.Free;
  end;
end;

Example with ComboBox

To use TImageList, drop an TImageList object onto the form. In this example we need six images.

All images must be the same size. If two sizes of image are to be used, then two TImageLists must be used.

Set the height and width of the images in the Object Inspector; In this case 50px wide by 18px high.

Set Style to csOwnerDrawFixed.

Double-click on the TImageList icon to open the TImageList editor. Make sure that the icons show correctly when selected. If they are smaller than expected, check the size in the Object Inspector <F11>. You may have to reload the images.

Place a ComboBox on the form and name it cbSymbols.

In the FormCreate event enter:

pascal
 cbSymbols.Items.Clear;
 for I := 0 to 5 do
   cbSymbols.Items.Add('');
 cbSymbols.ItemIndex := 0;

This will write 6 blank entries, and select the first.

In the OnDrawItem event for cbSymbols place the following code:

pascal
procedure TMyForm.cbSymbolsDrawItem( 
  aControl: TWinControl;
  iIndex: Integer; 
  aRect: TRect; 
  aState: TOwnerDrawState );
var
  cnv: TCanvas;
begin
  if not (aControl is TComboBox) then exit;
  cnv := TComboBox( aControl ).Canvas;
  Imagelist1.Draw( cnv, aRect.Left+2, aRect.Top+2, iIndex );
end;

The adjustments (+2) are to center the image in the item, if necessary.

Example with SpeedButton

Add images to the ImageList1 like in the previous example and use:

pascal
   ImageList1.GetBitmap(iIndex,SpeedButton1.Glyph);

See also


From the Free Pascal wiki, page "TImageList", as it stood on 2023-12-22 after a change by Wp, out of the wiki's dump of 2024-05-26. Copyright stays with the author who wrote it, according to the old wiki's Licensing page. If you have concerns about this material, message the admin.

Revision 2, last changed by WikiArchive .

Talk about this page

Nobody has opened a bench for this page yet. Sign in to open one.