首页  编辑  

TreeView1的宽度和高度自动随结点的最大长度变化的函数

Tags: /超级猛料/VCL/TreeView/   Date Created:

unit Unit1;

interface

uses

  Windows, Messages, ComCtrls, StdCtrls, AxCtrls, Classes, Controls,forms,

 ImgList;

type

  TForm1 = class (TForm)

   TreeView1: TTreeView;

   Label1: TLabel;

   ImageList1: TImageList;

    procedure TreeView1Expanded(Sender: TObject; Node: TTreeNode);

    procedure FormCreate(Sender: TObject);

    procedure TreeView1Collapsed(Sender: TObject; Node: TTreeNode);

  private

    { Private declarations }

  public

    { Public declarations }

  end ;

var

  Form1: TForm1;

implementation

uses Math;

{$R *.dfm}

procedure SetAtiveWidth(TreeView:TTreeView);

var Maxvalue,mylen,tmp,i:integer;

begin

Maxvalue:=0;   //

  for i:=0 to TreeView.Items.Count-1 do

      if   TreeView.Items.Item[i].IsVisible then begin //

          tmp:=(TreeView.Items.Item[i].Level+1)* TreeView.Indent; //

          mylen:=length(TreeView.Items.Item[i].Text);

          if mylen mod 2 <>0 then   // mylen

            mylen:=mylen+1; //

          tmp:=tmp+(abs( TreeView.Font.Height)+1) * (mylen+1) div 2; //

          if Assigned( TreeView.images) and ( TreeView.Items.Item[i].ImageIndex>-1) then //

              tmp:=tmp+ TreeView.Images.Height; //

          if Maxvalue<tmp then Maxvalue:=tmp; //

      end ;

TreeView.Width:=Maxvalue; //

//

//abs(Font.Height)

end ;

procedure SetAtiveHeight(TreeView:TTreeView);

var Maxvalue,i:integer;

begin

  Maxvalue:=0;   //

  with TreeView do

     for i:=0 to Items.Count-1 do

        if   Items.Item[i].IsVisible then

          Maxvalue:=Maxvalue+abs(Font.Height)+7; //+7

  TreeView.Height:=Maxvalue;

end ;

procedure TForm1.TreeView1Expanded(Sender: TObject; Node: TTreeNode);

begin

SetAtiveWidth(TTreeView(Sender));   //

SetAtiveHeight(TTreeView(Sender));   //

end ;

procedure TForm1.FormCreate(Sender: TObject);

begin

SetAtiveWidth(TreeView1);   //

SetAtiveHeight(TreeView1);   //

end ;

procedure TForm1.TreeView1Collapsed(Sender: TObject; Node: TTreeNode);

begin

SetAtiveWidth(TTreeView(Sender));   //

SetAtiveHeight(TTreeView(Sender));   //

end ;

end .