Recent

Author Topic: icons or graphics for ttreeview component?  (Read 3235 times)

Gizmo

  • Hero Member
  • *****
  • Posts: 688
icons or graphics for ttreeview component?
« on: May 28, 2014, 01:27:12 pm »
Without using virtualtreeview, is there a way to add graphics or icons, like a folder icon or file icon, to ttreeview? Lazarus 1.2.2 and fpc 2.6.4
Lazarus 2.0.4 and fpc 3.0.4 - Linux Mint 19 LTS, Windows 10 64 and Mac OSX Catlina
Useful Page to remember : http://wiki.freepascal.org/Cross_compiling#From_Linux_x64_to_Linux_i386

six1

  • Jr. Member
  • **
  • Posts: 77
Re: icons or graphics for ttreeview component?
« Reply #1 on: May 28, 2014, 01:35:43 pm »
Hi,
yes, you have to draw all items in the OwnerDraw Event.

Activate OwnerDraw by Style := csOwnerDrawVariable;

That's a Sample from doing this with a Combobox (Flaggen is TImagelist):

Code: [Select]
procedure TForm1.ComboBox1DrawItem(Control: TWinControl; Index: integer;
  ARect: TRect; State: TOwnerDrawState);
var
  ComboBox: TComboBox;
  symBitmap: TBitmap;
begin
  ComboBox := (Control as TComboBox);
  symBitmap := TBitmap.Create;
  try
    Flaggen.GetBitmap(index, symBitmap);
    with ComboBox.Canvas do
    begin
      ARect := Bounds(ARect.Left + 10, Arect.Top + 2, Arect.Left + 26, Arect.Top + 13);
      FillRect(ARect);
      if symBitmap.Handle <> 0 then
        Draw(ARect.Left, ARect.Top, symBitmap);
    end;
  finally
    symBitmap.Free;
  end;
end;

best, Michael

taazz

  • Hero Member
  • *****
  • Posts: 5363
Re: icons or graphics for ttreeview component?
« Reply #2 on: May 28, 2014, 01:38:56 pm »
sure there is, add an image list on your form connect it to the images property of the treeview and when you populate it with data you do something along the lines of
Code: [Select]
var
  vNode:TTreeNode;
begin
  vNode := TreeView1.Items.AddChild(Nil, 'Node 1');
  vNode.ImageIndex := 0;
  vNode.SelectedIndex := 0;
  vNode := TreeView1.Items.AddChild(Nil, 'Node 2');
  vNode.ImageIndex := 1;
  vNode.SelectedIndex := 1;
{.....}
end;
for a complete working example of the above sample download my code librarian from http://sourceforge.net/projects/codelibrarian/
Good judgement is the result of experience … Experience is the result of bad judgement.

OS : Windows 7 64 bit
Laz: Lazarus 1.4.4 FPC 2.6.4 i386-win32-win32/win64