//这里是创建一个一个类
type TCheckBoxTreeView=Class(TTreeView)
protected
procedure CreateParams(var Params: TCreateParams); override;
end;
//
具体只要加一句话
procedure TCheckBoxTreeView.CreateParams(var Params: TCreateParams);
begin
inherited;
Params.Style:=Params.Style or TVS_CHECKBOXES;
end;
或者在窗体上加一个TTreeView 控件,然后设置
SetWindowLong(TreeView.Handle,GWL_STYLE,GetWindowLong(TreeView.Handle,GWL_STYLE) or TVS_CHECKBOXES);
可以定义const TVS_CHECKBOXES =$0100 或者在单元里添加CommCtrl单元
两个函数:分别得到复选框状态
function IsNodeChecked(Node :TTreeNode) :Boolean;
const
TVIS_CHECKED = $2000;
var
TvItem :TTVItem;
begin
TvItem.Mask := TVIF_STATE;
TvItem.hItem := Node.ItemId;
TreeView_GetItem(Node.TreeView.Handle, TvItem);
Result := (TvItem.State and TVIS_CHECKED) = TVIS_CHECKED;
end;
procedure SetNodeChecked(Node :TTreeNode; Checked :Boolean);
const
TVIS_CHECKED = $2000;
var
TvItem :TTVItem;
begin
FillChar(TvItem, SizeOf(TvItem), 0);
with TvItem do begin
hItem := Node.ItemId;
Mask := TVIF_STATE;
StateMask := TVIS_STATEIMAGEMASK;
if Checked then
TvItem.State :=TVIS_CHECKED
else
TvItem.State :=TVIS_CHECKED shr 1;
TreeView_SetItem(Node.TreeView.Handle, TvItem);
end;
end;
窗体上添加一个TListBox,在按钮里添加代码
procedure TForm1.Button1Click(Sender: TObject);
var I:Integer;
begin
ListBox1.Clear;
for I:=0 to MyTree.Items.Count-1 do begin
if isNodeChecked(MyTree.Items.Item[i]) then
ListBox1.Items.Add(MyTree.Items.Item[i].Text);
end;
end;