Difference between revisions of "TImageList"

From Free Pascal wiki
Jump to navigationJump to search
Line 4: Line 4:
  
 
See [[doc:lcl//controls/timagelist.html|TImageList]] in the LCL reference for details.
 
See [[doc:lcl//controls/timagelist.html|TImageList]] in the LCL reference for details.
 +
 +
 +
== Example with ComboBox ==
 +
 +
 +
To use TImageList, drop an ImageList object onto the form.
 +
 +
All images must be the same size.
 +
 +
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.
 +
[[File:Example.jpg]]
 +
 +
For this example the images will be used in a ComboBox called cbSymbols, rather than text. (Makes translation unnecessary.)
 +
 +
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.
 +
 +
  
 
[[Category:LCL]]
 
[[Category:LCL]]

Revision as of 21:43, 25 April 2014

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.

All images must be the same size.

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. Example.jpg

For this example the images will be used in a ComboBox called cbSymbols, rather than text. (Makes translation unnecessary.)

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.