delphitvirtualstringtree

How to display a 'delete' icon on hover for a node in TVirtualStringTree?


I am working with TVirtualStringTree, and I would like to display a 'delete' icon when the user hovers over a specific node or the node is selected.

Example in VSCode Explorer View:

VSCode Explorer view

Is GetImageIndex the correct method to achieve this?

How can I handle the click event?


Solution

  • I tried the OnHotChange Event, but I couldn't figure out what to do with it. My solution uses GetImageIndex:

    procedure TForm1.VstGetImageIndex(Sender : TBaseVirtualTree; Node : PVirtualNode; Kind : TVTImageKind; Column : TColumnIndex; var Ghosted : Boolean; var ImageIndex : TImageIndex);
    begin
        case Kind of
            ikNormal, ikSelected :
            case Column of
                COL_NUM: begin 
                    if (Vst.HotNode = Node) then begin
                        ImageIndex := DEL_IMAGE_IDX; // Vst.Images should be set
                    end else begin
                        ImageIndex := -1;
                    end;
                end;
            end;
        end;
    end;
    

    OnClick is also easy:

    procedure TForm1.VstNodeClick(Sender : TBaseVirtualTree; const HitInfo : THitInfo);
    begin
        if hiOnNormalIcon in HitInfo.HitPositions then begin
            Vst.DeleteNode(HitInfo.HitNode);
            Exit;
        end;
    // every other click handling ...
    end;