Search code examples
delphidelphi-7vclvirtualtreeviewtvirtualstringtree

VirtualStringTree aligned text and gridlines


I have a virtualstringtree (Gridlines enabled) with X headers and x roots. I would like to add children (at least 1) to the roots that have only 1 text which is in the middle and goes through all the headers (independent). So no matter if I resize the headers, etc. the text is always aligned to the actual client size of the VirtualStringTree.

Is this possible? If so, how?

enter image description here


Solution

  • This sounds like cell merging. This is discussed here, for example, where the solution is to include toAutoSpanColumns in TreeOptions.AutoOptions.

    If you want center-aligned text, override OnDrawText accordingly. Something like:

    procedure TForm1.VirtualStringTree1DrawText(Sender: TBaseVirtualTree;
      TargetCanvas: TCanvas; Node: PVirtualNode; Column: TColumnIndex;
      const Text: WideString; const CellRect: TRect; var DefaultDraw: Boolean);
    var
      r: TRect;
    begin
      r := CellRect;
      Windows.DrawTextW(TargetCanvas.Handle, PWideChar(Text), Length(Text), r, DT_CENTER or DT_VCENTER);
      DefaultDraw := False;
    end;