How to set the color of VirtualStringTree header?

2020-08-01 06:26发布

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?

2条回答
成全新的幸福
2楼-- · 2020-08-01 06:51

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;
查看更多
啃猪蹄的小仙女
3楼-- · 2020-08-01 07:08
procedure TfrmDepositDefrayalSingly.vstItemsManuallyHeaderDrawQueryElements(Sender: TVTHeader;
  var PaintInfo: THeaderPaintInfo; var Elements: THeaderPaintElements);
begin
  Elements := [hpeBackground];
end;


procedure TfrmDepositDefrayalSingly.vstItemsManuallyAdvancedHeaderDraw(Sender: TVTHeader;
  var PaintInfo: THeaderPaintInfo; const Elements: THeaderPaintElements);
begin
  if hpeBackground in Elements then
  begin
    PaintInfo.TargetCanvas.Brush.Color := cGlobalVar.BasicColor;
    PaintInfo.TargetCanvas.FillRect(PaintInfo.PaintRectangle);

    if Assigned(PaintInfo.Column) then
    begin
      PaintInfo.TargetCanvas.Brush.Color := clGray;
      PaintInfo.TargetCanvas.FrameRect(PaintInfo.PaintRectangle);
    end;
  end;
end;
查看更多
登录 后发表回答