TImageList

From Free Pascal wiki
Revision as of 22:01, 25 April 2014 by Windsurferme (talk | contribs)

TImageList is a list of images, e.g. for creating a Toolbar.

Toolbars use ImageLists

See TImageList in the LCL reference for details.


Example with ComboBox

To use TImageList, drop an ImageList 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 ImageLists must be used.

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

Double-click on the ImageList icon to open the ImageList 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:

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:

Procedure TMyForm.cbSymbolsDrawItem(Control: TWinControl;
  Index: Integer; ARect: TRect; State: TOwnerDrawState);
var
  cnv: TCanvas;
begin
  if not (Control is TComboBox) then Exit;
  cnv:=TComboBox(Control).Canvas;
  Imagelist1.Draw(cnv, ARect.Left+2, Arect.Top+2, index);
End;

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