台湾谢廷凯:给TreeView添加复选框

来源:百度文库 编辑:中财网 时间:2024/05/10 07:25:43

给TreeView添加复选框

分类: Delphi 2009-03-03 16:57 295人阅读 评论(0) 收藏 举报

[delphi] view plaincopy?

  1. unit Unit1;  
  2.   
  3. interface  
  4.   
  5. uses  
  6.   Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,  
  7.   Dialogs, StdCtrls, ComCtrls;  
  8.   
  9. type  
  10.   TForm1 = class(TForm)  
  11.     TreeView1: TTreeView;  
  12.     Button1: TButton;  
  13.     procedure Button1Click(Sender: TObject);  
  14.   private  
  15.     { Private declarations }  
  16.     procedure SetComCtlStyle(Ctl:  TWinControl;  Value:  Integer;  UseStyle:  Boolean);  
  17.   public  
  18.     { Public declarations }  
  19.   end;  
  20. const  
  21.   TVS_CHECKBOXES=$0100;  
  22. var  
  23.   Form1: TForm1;  
  24.   
  25. implementation  
  26.   
  27. {$R *.dfm}  
  28. procedure TForm1.SetComCtlStyle(Ctl:  TWinControl;  Value:  Integer;  UseStyle:  Boolean);  
  29. var  
  30.   Style:  Integer;  
  31. begin   
  32.   if  Ctl.HandleAllocated  then   
  33.   begin   
  34.     Style  :=  GetWindowLong(Ctl.Handle,  GWL_STYLE);   
  35.     if  not  UseStyle  then  Style  :=  Style  and  not  Value   
  36.     else  Style  :=  Style  or  Value;   
  37.     SetWindowLong(Ctl.Handle,  GWL_STYLE,  Style);   
  38.   end;   
  39. end;   
  40.   
  41. procedure TForm1.Button1Click(Sender: TObject);  
  42. begin  
  43.   SetComCtlStyle(TreeView1,TVS_CHECKBOXES,True);  
  44. end;  
  45.   
  46. end.