TVirtualStringTree - 设置列 cells/nodes 对齐

TVirtualStringTree - setting column cells/nodes Alignment

我有一个 VirtualStringTree,Header.Column 设置为 taLeftJustify(默认)。

有没有办法将该列的 cells/nodes 设置为 taRightJustify 以便节点向右对齐,但 header 列文本将向右对齐左边?

这是我想要的结果(在第 1 列):

我使用的是相当旧的 VT 版本 4.5.5

列标题对齐使用:

Header.Columns[x].CaptionAlignment := taLeftJustify;

以及节点对齐方式:

Header.Columns[x].Alignment := taRightJustify;

x = 您的专栏

在我的旧 VT 版本中,没有 TVirtualTreeColumn.CaptionAlignment,所以我设法使用 OnAdvancedHeaderDraw 绘制了我自己的列标题。我将第 1 列设置为 taRightJustify 并处理 header Text 为所需列绘制自己。

这段代码可能对其他人有帮助,所以我还是post:

type
  TVirtualTreeColumnsAccess = class(TVirtualTreeColumns);

procedure TForm1.FormCreate(Sender: TObject);
begin
  VST.Header.Options := VST.Header.Options + [hoOwnerDraw];
  VST.OnHeaderDrawQueryElements := VSTHeaderDrawQueryElements;
  VST.OnAdvancedHeaderDraw := VSTAdvancedHeaderDraw;
end;

procedure TForm1.VSTHeaderDrawQueryElements(Sender: TVTHeader; var PaintInfo: THeaderPaintInfo;
  var Elements: THeaderPaintElements);
begin
  { Use OwnerDraw only for desired column(s)     }
  { other columns drawing will be handled by VST }
  if Assigned(PaintInfo.Column) and (PaintInfo.Column.Index = 1) then
    Elements := [hpeText];
end;

procedure TForm1.VSTAdvancedHeaderDraw(Sender: TVTHeader;
  var PaintInfo: THeaderPaintInfo; const Elements: THeaderPaintElements);
var
  DrawFormat: Cardinal;
  R: TRect;
begin
  { The event will fire only for the desired column(s) }
  if (hpeText in Elements) and Assigned(PaintInfo.Column) then
  with PaintInfo do
  begin
    DrawFormat := DT_LEFT or DT_TOP or DT_NOPREFIX;
    if Column.UseRightToLeftReading then
      DrawFormat := DrawFormat or DT_RTLREADING;

    R := TextRectangle;
    R.Left := PaintRectangle.Left + Column.Margin;

    TVirtualTreeColumnsAccess(Column.Owner).DrawButtonText(
      TargetCanvas.Handle, Column.Text, R,
      IsEnabled,
      IsHoverIndex and (hoHotTrack in Sender.Options)
        and not (tsUseThemes in Sender.Treeview.TreeStates),
      DrawFormat);
  end;
end;