如何为尚不存在的节点显示虚拟树视图网格线?

Ben*_*iss 5 delphi gridlines virtualtreeview

我在Delphi 7中使用SoftGem的VirtualStringTree.

有没有办法启用完整的网格线(就像在TListView中一样)?我只能找到toShowHorzGridLines,它只显示当前节点的行,而不是下面空白空间中的任何内容,并且toShowVertGridLines只显示垂直线.

如何在添加项目之前在空白区域中显示它们?

TLa*_*ama 5

我认为没有一种简单的方法可以在不修改PaintTree方法的情况下实现它,因为没有任何节点事件不能被触发,因为那些应该简单绘制线的节点还不存在.

这是一种肮脏的方法,如何根据最低可见节点另外绘制水平线.实际上,它DefaultNodeHeight在此屏幕截图中绘制了由橙色填充的区域中的值的距离:

在此输入图像描述

这是代码:

type
  TVirtualStringTree = class(VirtualTrees.TVirtualStringTree)
  public
    procedure PaintTree(TargetCanvas: TCanvas; Window: TRect; Target: TPoint;
      PaintOptions: TVTInternalPaintOptions; PixelFormat: TPixelFormat = pfDevice); override;
  end;

implementation

{ TVirtualStringTree }

procedure TVirtualStringTree.PaintTree(TargetCanvas: TCanvas; Window: TRect;
  Target: TPoint; PaintOptions: TVTInternalPaintOptions;
  PixelFormat: TPixelFormat);
var
  I: Integer;
  EmptyRect: TRect;
  PaintInfo: TVTPaintInfo;
begin
  inherited;
  if (poGridLines in PaintOptions) and (toShowHorzGridLines in TreeOptions.PaintOptions) and
    (GetLastVisible <> nil) then
  begin
    EmptyRect := GetDisplayRect(GetLastVisible,
      Header.Columns[Header.Columns.GetLastVisibleColumn].Index, False);
    EmptyRect := Rect(ClientRect.Left, EmptyRect.Bottom + DefaultNodeHeight,
      EmptyRect.Right, ClientRect.Bottom);
    ZeroMemory(@PaintInfo, SizeOf(PaintInfo));
    PaintInfo.Canvas := TargetCanvas;
    for I := 0 to ((EmptyRect.Bottom - EmptyRect.Top) div DefaultNodeHeight) do
    begin
      PaintInfo.Canvas.Font.Color := Colors.GridLineColor;
      DrawDottedHLine(PaintInfo, EmptyRect.Left, EmptyRect.Right,
        EmptyRect.Top + (I * DefaultNodeHeight));
    end;
  end;
end;
Run Code Online (Sandbox Code Playgroud)

这里有恒定和变量节点高度的结果:

在此输入图像描述

上面屏幕截图中可见的毛刺(从左侧偏移的线条)只是虚线渲染的结果.如果LineStyle在虚拟树视图上设置属性,lsSolid则会看到正确的结果.

  • 现在看结果,矩形移动了.我稍后会解决它(我现在必须去).但它仍然是非常脏的解决方案. (2认同)
  • Phew,线位置是正确的.它是由虚线渲染引起的.但是有一个错误.为了确定行的右边界,我使用了索引为"Header.Columns.Count - 1"的列,这是错误的.如果您有例如2列并将第二列移动到第一个位置,我应该使用的索引(最右列的索引)是0,而不是1(什么是`Header.Columns.Count - 1`).现在我正在使用`GetLastVisibleColumn`应该是正确的方法. (2认同)