问题来源: http://www.cnblogs.com/del/archive/2008/05/15/1114450.html#1199402
本例效果图:
本例效果图:
unit Unit1; interface uses Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms, Dialogs, ComCtrls, StdCtrls; type TForm1 = class(TForm) TreeView1: TTreeView; Button1: TButton; Button2: TButton; Button3: TButton; procedure FormCreate(Sender: TObject); procedure Button1Click(Sender: TObject); procedure Button2Click(Sender: TObject); procedure Button3Click(Sender: TObject); end; var Form1: TForm1; implementation {$R *.dfm} uses CommCtrl; //让 TreeView 带有复选框 procedure TForm1.FormCreate(Sender: TObject); var H: HWND; begin H := TreeView1.Handle; SetWindowLong(H, GWL_STYLE, GetWindowLong(H, GWL_STYLE) or TVS_CHECKBOXES); {下面的代码只是给 TreeView 添加内容} with TreeView1 do begin Align := alLeft; Items.Add(nil, 'AAA'); Items.Add(nil, 'BBB'); Items.Add(nil, 'CCC'); Items.AddChild(Items[2], '111'); Items.AddChild(Items[2], '222'); Items.AddChild(Items[2], '333'); end; end; //列举带有复选框的项 procedure TForm1.Button1Click(Sender: TObject); var Node: TTreeNode; TVI: TTVItem; begin for Node in TreeView1.Items do begin TVI.mask := TVIF_STATE; TVI.hItem := Node.ItemId; TreeView_GetItem(TreeView1.Handle, TVI); if TVI.state and $2000 = $2000 then ShowMessage(Node.Text); end; end; //取消所有选中(这是后来补充的, 所以效果图上没有) procedure TForm1.Button2Click(Sender: TObject); var Node: TTreeNode; TVI: TTVItem; begin for Node in TreeView1.Items do begin TVI.mask := TVIF_STATE; TVI.hItem := Node.ItemId; TVI.stateMask := TVIS_STATEIMAGEMASK; TVI.state := $2000 shr 1; TreeView_SetItem(TreeView1.Handle, TVI); end; end; //选中所有(这是后来补充的, 所以效果图上没有) procedure TForm1.Button3Click(Sender: TObject); var Node: TTreeNode; TVI: TTVItem; begin for Node in TreeView1.Items do begin TVI.mask := TVIF_STATE; TVI.hItem := Node.ItemId; TVI.stateMask := TVIS_STATEIMAGEMASK; TVI.state := $2000; TreeView_SetItem(TreeView1.Handle, TVI); end; end; end.