如何重新思考树形视图是否需要水平滚动条?

时间:2012-10-02 18:56:03

标签: delphi

考虑以下非常简单的单位:

Unit1.pas

unit Unit1;

interface

uses
  Windows, Classes, Controls, Forms, ComCtrls;

type
  TForm1 = class(TForm)
    TreeView1: TTreeView;
    procedure FormCreate(Sender: TObject);
  end;

var
  Form1: TForm1;

implementation

{$R *.dfm}

const
  SLongString = 'blah blah blah blah blah blah blah blah blah blah blah blah blah blah blah blah';

procedure TForm1.FormCreate(Sender: TObject);
var
  Node: TTreeNode;
begin
  TreeView1.Width := 200;
  Node := TreeView1.Items.Add(nil, SLongString);
  Node.Text := 'blah';
end;

end.

Unit1.dfm

object Form1: TForm1
  ClientHeight = 137
  ClientWidth = 216
  OnCreate = FormCreate
  PixelsPerInch = 96
  TextHeight = 13
  object TreeView1: TTreeView
    Left = 8
    Top = 8
    Width = 198
    Height = 121
  end
end

将其添加到VCL Forms应用程序并运行。结果如下:

enter image description here

我希望水平滚动条不显示。我怎样才能做到这一点?

现在我意识到我可以删除分配很长字符串的代码行。但就我的问题而言,这是一个减少的计划。在the real app the text of the nodes is changing中,我希望滚动条显示是否需要它们,并且不显示它们是否不需要。

我知道TVS_NOHSCROLL风格,但我无法使用它。有时树视图包含的文本比可用空间宽。有时候没有。

我还想使用TTreeView并且不想使用虚拟树视图。并非我对虚拟树视图有任何反对意见,只是我的应用程序当前正在使用TTreeView

1 个答案:

答案 0 :(得分:16)

非常简单,使用TreeView1.Items.BeginUpdate/EndUpdate方法,相应地计算滚动条。

像这样:

...
 TreeView1.Items.BeginUpdate;
 // change your nodes here
 TreeView1.Items.EndUpdate