Search code examples
delphivirtualtreeviewtvirtualstringtree

How to set the color of VirtualStringTree header?


The VirtualStringTree header has a 'Background' property but setting it to a different color does not change the color. I suspect the tree is rendered using Windows themes.

How can I set the color?


Solution

  • You can use property THeader.Background but you'll have to exclude toThemeAware from TreeOptions.PaintOptions. That would turn off themes, as TLama already said in his comment above.


    I recommend you to use the events OnAdvancedHeaderDraw and OnHeaderDrawQueryElements. hoOwnerDraw has to be included in Header.Options for them to take effect.

    In OnHeaderDrawQueryElements you set Elements to (at least) [hpeBackground] and in OnAdvancedHeaderDraw you do the custom drawing.

    See this example (source):

    procedure TfrmMain.MyVSTHeaderDrawQueryElements(Sender: TVTHeader;
      var PaintInfo: THeaderPaintInfo; var Elements: THeaderPaintElements);
    begin
      Elements := [hpeBackground];
    end;
    
    procedure TfrmMain.MyVSTAdvancedHeaderDraw(Sender: TVTHeader;
      var PaintInfo: THeaderPaintInfo; const Elements: THeaderPaintElements);
    begin
      if hpeBackground in Elements then
      begin
        PaintInfo.TargetCanvas.Brush.Color := clFuchsia; // <-- your color here
        if Assigned(PaintInfo.Column) then
          DrawFrameControl(PaintInfo.TargetCanvas.Handle, PaintInfo.PaintRectangle, DFC_BUTTON, DFCS_FLAT or DFCS_ADJUSTRECT); // <-- I think, that this keeps the style of the header background, but I'm not sure about that
        PaintInfo.TargetCanvas.FillRect(PaintInfo.PaintRectangle);
      end;
    end;