How to set the color of VirtualStringTree header?

拜拜、爱过 提交于 2019-12-06 17:41:43

问题


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?


回答1:


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;



回答2:


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;


来源:https://stackoverflow.com/questions/32396875/how-to-set-the-color-of-virtualstringtree-header

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!