delphivirtualtreeviewtvirtualstringtree

How to display an icon or image in a column of TVirtualStringTree?


In a Delphi VCL project, I have created a simple TVirtualStringTree with two columns. The first column will contain text identifying the Name of the data being represented. The data record also contains a status field. The second column is intended to represent the status of the record using an image (16x16 pixel) w/o text.

I have searched demos, but have not mastered the full process for how VTV displays a node, and have not been successful in getting an icon to display in the node of a specified column.

So I have three related questions:

  1. I see how the text is assigned in the OnGetText event, but where should I assign or change the image to reflect the current status in my record?

  2. How do I get the image to actually display in the column?

  3. Am I limited in size for the images, or can they be larger than icons? If so, do I need to change any settings to adjust the height of each row (if possible)?


Solution

  • You need to assign (16x16 in your case) TImageList to the TVirtualStringTree.Images property, then handle the event OnGetImageIndex e.g.:

    procedure TForm1.VirtualStringTree1GetImageIndex(Sender: TBaseVirtualTree;
      Node: PVirtualNode; Kind: TVTImageKind; Column: TColumnIndex;
      var Ghosted: Boolean; var ImageIndex: Integer);
    var
      NodeRec: PNodeRec;
    begin
      NodeRec := Sender.GetNodeData(Node);
      if Assigned(NodeRec) then 
      begin
        if (Column = 1) then
        begin 
          if Kind in [ikNormal, ikSelected] then
          begin
            case NodeRec.Status of // check the needed status(es)
              1: ImageIndex := 1; // whichever image you need
              2: ImageIndex := 2; // whichever image you need
              // ...
            end; 
          end;
        end;
      end;
    end;
    

    Am I limited in size for the images, or can they be larger than icons? If so, do I need to change any settings to adjust the height of each row (if possible)

    Not sure what you meant by that, because you stated you need a 16x16 images. You could use OnGetImageIndexEx if you need different image lists with possibly different dimensions. for variable height you could set toVariableNodeHeight in the TreeOptions.MiscOptions and handle the OnMeasureItem event. another way to draw graphics into the VTV canvas is to handle the OnBeforeItemPaint/OnAfterItemPaint for example.