如何在TVirtualStringTree中自动调整跨越列的大小?

问题描述 投票:4回答:1

我有VirtualStringTree(VST)的简单示例,有4列,在第2或第3列,我可以有更多符合默认列宽的文本。我启用了hoAutoSpanColumns,因此大文本将与其他列重叠,如果它们为空。这很好用。问题是第二列或第三列中的文本应跨越VST的整个长度。在这种情况下,大列仅延伸到最后(第4列)的末尾。

以下是第3行中的第2列仅跨越第3列和第4列,然后在第4列的宽度处停止:

enter image description here

如果我使用AutoFitColumns自动调整第二列的大小:

VST1.Header.AutoFitColumns(false, smaUseColumnOption,1,1);

然后它推送第3和第4列,以便它们从第2列的末尾开始:

enter image description here

但是我需要第3和第4列才能保持相同的位置,如下所示:

enter image description here

有没有办法自动将第二列添加到文本中,但是将第3列和第4列保持在原来相同的位置?

delphi autosize virtualtreeview tvirtualstringtree
1个回答
3
投票

此时没有内置方法可以自动将标头自动调整到跨区列文本。要自动调整给定列以查看整个跨区文本,您可以编写这样的代码(对于单个节点)。请注意,此时VT中的autospan功能不支持RTL读取(因此此代码不一样):

type
  TVirtualStringTree = class(VirtualTrees.TVirtualStringTree)
  protected
    function GetSpanColumn(Node: PVirtualNode): TColumnIndex; virtual;
  public
    procedure AutoFitSpanColumn(DestColumn: TColumnIndex; Node: PVirtualNode);
  end;

implementation

{ TVirtualStringTree }

function TVirtualStringTree.GetSpanColumn(Node: PVirtualNode): TColumnIndex;
begin
  { this returns visible span column for the given node, InvalidColumn otherwise }
  Result := Header.Columns.GetLastVisibleColumn;
  while ColumnIsEmpty(Node, Result) and (Result <> InvalidColumn) do
    Result := Header.Columns.GetPreviousVisibleColumn(Result);
end;

procedure TVirtualStringTree.AutoFitSpanColumn(DestColumn: TColumnIndex; Node: PVirtualNode);
var
  ColsWidth: Integer;
  SpanWidth: Integer;
  SpanOffset: Integer;
  SpanColumn: TColumnIndex;
begin
  SpanColumn := GetSpanColumn(Node);

  if SpanColumn <> InvalidColumn then
  begin
    { get the total width of the header }
    ColsWidth := Header.Columns.TotalWidth;
    { get the width of the span text cell as it would be autosized }
    SpanWidth := DoGetNodeWidth(Node, SpanColumn) + DoGetNodeExtraWidth(Node, SpanColumn) +
      DoGetCellContentMargin(Node, SpanColumn).X + Margin;
    { and get the left position of the span cell column in header }
    SpanOffset := Header.Columns[SpanColumn].GetRect.Left;
    { now, the width of the autosized column we increase by the subtraction of the fully
      visible span cell width and all columns width increased by offset of the span cell
      column; in other words, we'll just increase or decrease width of the DestColumn to
      the difference of width needed for the span column to be fully visible, or "fit" }
    Header.Columns[DestColumn].Width := Header.Columns[DestColumn].Width +
      SpanWidth - ColsWidth + SpanOffset;
  end;
end;
© www.soinside.com 2019 - 2024. All rights reserved.