码迷,mamicode.com
首页 > 其他好文 > 详细

学习 TTreeView [16] - 给 TTreeView 添加复选框 (回复 "丁永其" 的问题)

时间:2019-08-19 00:01:52      阅读:92      评论:0      收藏:0      [点我收藏+]

标签:code   技术   setw   The   gif   controls   选中   strong   mic   

问题来源: 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.

学习 TTreeView [16] - 给 TTreeView 添加复选框 (回复 "丁永其" 的问题)

标签:code   技术   setw   The   gif   controls   选中   strong   mic   

原文地址:https://www.cnblogs.com/jijm123/p/11374367.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!