如何设置 VirtualStringTree header 的颜色?

How to set the color of VirtualStringTree header?

VirtualStringTree header 有一个 'Background' 属性 但将其设置为不同的颜色不会改变颜色。我怀疑这棵树是使用 Windows 主题渲染的。

如何设置颜色?

您可以使用 属性 THeader.Background,但您必须从 TreeOptions.PaintOptions 中排除 toThemeAware。正如 TLama 在上面的评论中所说的那样,这将关闭主题。


我建议您使用事件 OnAdvancedHeaderDrawOnHeaderDrawQueryElementshoOwnerDraw 必须包含在 Header.Options 中才能生效。

OnHeaderDrawQueryElements 中,您将 Elements 设置为(至少)[hpeBackground],在 OnAdvancedHeaderDraw 中,您进行自定义绘图。

查看此示例 (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;
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;