谁知道有CheckBox的树型目录控件, 像诺顿杀毒软件一样可以打勾的, (200分) ( 积分: 200 )

  • 主题发起人 主题发起人 gdgztt
  • 开始时间 开始时间
G

gdgztt

Unregistered / Unconfirmed
GUEST, unregistred user!
谁知道有CheckBox的树型目录控件, 像诺顿杀毒软件一样可以打勾的, (200分)
 
谁知道有CheckBox的树型目录控件, 像诺顿杀毒软件一样可以打勾的, (200分)
 
raize 控件里有
 
delphi自己就可以做,就用一般的treeview就可以。
调用api就实现了,你如果需要,我晚上给你代码。
 
在delphi2005中的winform中的treeview 就有这个checkbox,
自己做也可以啊,直接将treenode.text传给radiogroup(或者车checkbox) 组件就可以啊
 
一个简单的办法,用treeview的节点的图标索引的变换来实现。
如索引为0的图片是一个小方框的图片,而索引为1的图片是一个带对号的图片
当你点击某一个节点的时候先判断这个节点的图标索引,如果为0那么置为1,如果为1那么置为0。
 
你可以用Raize中的RzCheckTree,它挺不错的,当子节点没有全部选择时,父节点不是打勾,而是点,全部选择才是√,下载地址:
http://www.2ccc.com/go.asp?id=2234&url=http://downloads.2ccc.com/vcl/component_packs/Raize_v3.0.13.rar

另外你可以这样给TreeView中的每一项加上可复选框的CheckBox
TreeView不能像ListView中的CheckBox一样多选,只能用StateImage,效果不错,但不是CheckBox,而是画一个打勾的图形(Index=3)。当某个Item被选中后,前面就出现这个打勾的图标,当再次点击时,打勾的图标就消失。
在TreeView的OnClick事件中添加如下代码:
if TreeView2.Selected.StateIndex <> 3 then
TreeView2.Selected.StateIndex :=3
else
TreeView2.Selected.StateIndex := -1;

再另:cnpack的共享代码包中也有一个CnCheckTreeView
再再另:我有个经过修改的以前用过的一个CheckTreeView,我可以发给你,不过在现在的版本7、8、9能不能用。
 
有这样ActiveX控件,google一下就行了!
 
1stClass控件组里有。

也可以自己写一个。
// ***********************************************************************
//
// TCheckTreeView
//
// l940801@yahoo.com
//
// ***********************************************************************

unit CheckTreeViews;

interface

uses
Windows, Messages, Classes, Controls, ComCtrls, CommCtrl;

type

TTVCheckChangingEvent = procedure(Sender: TObject; Node: TTreeNode; var CanCheck: boolean) of Object;
TTVCheckChangedEvent = procedure(Sender: TObject; Node: TTreeNode) of Object;
TTVCheckClick = procedure(Sender: TObject; Node: TTreeNode) of Object;
{ TCustomCheckTreeView }

TCustomCheckTreeView = class(TCustomTreeView)
private
FCheckBoxes: boolean;
FCheckStream: TMemoryStream;

FOnCheckChanged: TTVCheckChangedEvent;
FOnCheckChanging: TTVCheckChangingEvent;
FOnCheckClick:TTVCheckClick;
procedure SetCheckBoxes(const Value: boolean);
procedure SaveChecks;
procedure RestoreChecks;

function GetChecked(Index: integer): boolean;
procedure SetChecked(Index: integer; const Value: boolean);

procedure TVMSetItem(var Msg: TMessage); message TVM_SETITEM;
procedure WMLButtonDown(var Msg: TWMLButtonDown); message WM_LBUTTONDOWN;
protected
procedure CreateWnd; override;
procedure DestroyWnd; override;
public
constructor Create(AOwner: TComponent); override;

property Checked[Index: integer]: boolean read GetChecked write SetChecked;
property CheckBoxes: boolean read FCheckBoxes write SetCheckBoxes;
property OnCheckChanging: TTVCheckChangingEvent read FOnCheckChanging write FOnCheckChanging;
property OnCheckChanged: TTVCheckChangedEvent read FOnCheckChanged write FOnCheckChanged;
property OnCheckClick:TTVCheckClick read FOnCheckClick write FOnCheckClick;
end;

{ TCheckTreeView }

TCheckTreeView = class(TCustomCheckTreeView)
published
property CheckBoxes;
property OnCheckChanging;
property OnCheckChanged;
property OnCheckClick;
property Align;
property Anchors;
property AutoExpand;
property BevelEdges;
property BevelInner;
property BevelOuter;
property BevelKind default bkNone;
property BevelWidth;
property BiDiMode;
property BorderStyle;
property BorderWidth;
property ChangeDelay;
property Color;
property Ctl3D;
property Constraints;
property DragKind;
property DragCursor;
property DragMode;
property Enabled;
property Font;
property HideSelection;
property HotTrack;
property Images;
property Indent;
property MultiSelect;
property MultiSelectStyle;
property ParentBiDiMode;
property ParentColor default False;
property ParentCtl3D;
property ParentFont;
property ParentShowHint;
property PopupMenu;
property ReadOnly;
property RightClickSelect;
property RowSelect;
property ShowButtons;
property ShowHint;
property ShowLines;
property ShowRoot;
property SortType;
property StateImages;
property TabOrder;
property TabStop default True;
property ToolTips;
property Visible;
property OnAddition;
property OnAdvancedCustomDraw;
property OnAdvancedCustomDrawItem;
property OnChange;
property OnChanging;
property OnClick;
property OnCollapsed;
property OnCollapsing;
property OnCompare;
property OnContextPopup;
property OnCreateNodeClass;
property OnCustomDraw;
property OnCustomDrawItem;
property OnDblClick;
property OnDeletion;
property OnDragDrop;
property OnDragOver;
property OnEdited;
property OnEditing;
property OnEndDock;
property OnEndDrag;
property OnEnter;
property OnExit;
property OnExpanding;
property OnExpanded;
property OnGetImageIndex;
property OnGetSelectedIndex;
property OnKeyDown;
property OnKeyPress;
property OnKeyUp;
property OnMouseDown;
property OnMouseMove;
property OnMouseUp;
property OnStartDock;
property OnStartDrag;
{ Items must be published after OnGetImageIndex and OnGetSelectedIndex }
property Items;
end;

procedure Register;

implementation

procedure Register;
begin
RegisterComponents('MyVcl', [TCheckTreeView]);
end;

procedure SetComCtlStyle(Ctl: TWinControl; Value: Integer; UseStyle: Boolean);
var
Style: Integer;
begin
if Ctl.HandleAllocated then
begin
Style:= GetWindowLong(Ctl.Handle, GWL_STYLE);
if not UseStyle then
Style:= Style and not Value
else
Style:= Style or Value;
SetWindowLong(Ctl.Handle, GWL_STYLE, Style);
end;
end;

{ TCustomCheckTreeView }

constructor TCustomCheckTreeView.Create(AOwner: TComponent);
begin
inherited Create(AOwner);
FCheckBoxes:= false;
end;

procedure TCustomCheckTreeView.CreateWnd;
begin
inherited CreateWnd;
if FCheckBoxes then SetCheckBoxes(true);
RestoreChecks;
end;

procedure TCustomCheckTreeView.DestroyWnd;
begin
SaveChecks;
inherited DestroyWnd;
end;

function TCustomCheckTreeView.GetChecked(Index: integer): boolean;
var
TVItem: TTVItem;
begin
// Prepare to receive the desired information.
TVItem.mask:= TVIF_HANDLE or TVIF_STATE;
TVItem.hItem:= Items[Index].ItemId;
TVItem.stateMask:= TVIS_STATEIMAGEMASK;

// Request the information.
TreeView_GetItem(Handle, TVItem);

// Return false if it's not checked, or true otherwise.
Result:= ((TVItem.state shr 12) - 1) > 0;
end;

procedure TCustomCheckTreeView.RestoreChecks;
var
I: Integer;
Value: Boolean;
begin
if not Assigned(FCheckStream) then Exit;
for I:= 0 to Items.Count - 1 do
begin
FCheckStream.Read(Value, SizeOf(Value));
Checked:= Value;
end;
FCheckStream.Free;
FCheckStream:= nil;
end;

procedure TCustomCheckTreeView.SaveChecks;
var
I: Integer;
Value: Boolean;
begin
if FCheckStream = nil then
FCheckStream:= TMemoryStream.Create
else
FCheckStream.Size:= 0;
for I:= 0 to Items.Count - 1 do
begin
Value:= Checked;
FCheckStream.Write(Value, SizeOf(Value));
end;
FCheckStream.Position:= 0;
end;

procedure TCustomCheckTreeView.SetCheckBoxes(const Value: boolean);
begin
FCheckBoxes:= Value;
SetComCtlStyle(Self, TVS_CHECKBOXES, Value);
end;

procedure TCustomCheckTreeView.SetChecked(Index: integer;
const Value: boolean);
var
TVItem: TTVItem;
StateIndex: integer;
begin
// Prepare to receive the desired information.
TVItem.mask:= TVIF_HANDLE or TVIF_STATE;
TVItem.hItem:= Items[Index].ItemId;
TVItem.stateMask:= TVIS_STATEIMAGEMASK;

//Image 1 in the tree view check box image list is the unchecked box. Image 2 is the checked box.
if Value then
StateIndex:= 2
else
StateIndex:= 1;

TVItem.state:= INDEXTOSTATEIMAGEMASK(StateIndex);

TreeView_SetItem(Handle, TVItem);
end;

procedure TCustomCheckTreeView.TVMSetItem(var Msg: TMessage);
var
Node: TTreeNode;
CanCheck: boolean;
begin
with PTVItem(Msg.lParam)^ do
if (Mask and (TVIF_STATE or TVIF_HANDLE) > 0) and Assigned(FOnCheckChanging) then
begin
Node:= Items.GetNode(hItem);
CanCheck:=True;
FOnCheckChanging(Self, Node, CanCheck);
if CanCheck then
begin
inherited;
if Assigned(FOnCheckChanged) then FOnCheckChanged(Self, Node);
end;
end
else
inherited;
end;

procedure TCustomCheckTreeView.WMLButtonDown(var Msg: TWMLButtonDown);
var
HitTests: THitTests;
Node: TTreeNode;
CanCheck: boolean;
begin
if FCheckBoxes then
begin
HitTests:= GetHitTestInfoAt(Msg.XPos, Msg.YPos);
if (htOnStateIcon in HitTests) and Assigned(FOnCheckChanging) then
begin
Node:= GetNodeAt(Msg.XPos, Msg.YPos);
CanCheck:=True;
FOnCheckChanging(Self, Node, CanCheck);
if CanCheck then
begin
inherited;
if Assigned(FOnCheckChanged) then FOnCheckChanged(Self, Node);
if Assigned(FOnCheckClick) then FOnCheckClick(Self,Node);
end;
end
else
inherited;
end
else
inherited;
end;

end.
 
virtualtreeview
http://www.2ccc.com/article.asp?articleid=292
 
这个是简单一些的。
unit dxDBCheckTreeView;

interface

uses
Windows, Messages,SysUtils, Classes, Controls, ComCtrls, dxtree, dxdbtree,Commctrl;

const
TVIS_CHECKED = $2000;
type
TdxDBCheckTreeView = class(TdxDBTreeView)
private

protected
procedure CreateParams(var Params: TCreateParams); override;
public
function IsChecked(Node :TTreeNode) :Boolean;
procedure SetChecked(Node :TTreeNode; Checked :Boolean);
published
{ Published declarations }
end;

procedure Register;

implementation

procedure Register;
begin
RegisterComponents('sun', [TdxDBCheckTreeView]);
end;

{ TdxDBCheckTreeView }

procedure TdxDBCheckTreeView.CreateParams(var Params: TCreateParams);
begin
inherited;
Params.Style:=Params.Style or TVS_CHECKBOXES;
end;

function TdxDBCheckTreeView.IsChecked(Node: TTreeNode): Boolean;
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 TdxDBCheckTreeView.SetChecked(Node: TTreeNode; Checked: Boolean);
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;


end.
 
谢谢各位,每人都有分,特别感谢"还是朋友",很详细
 
gdgztt:
因为前段时间我也想做这个东西,
所以在网上搜索了好多资料。

自己写可以,但是功能不行,呵呵。
后来干脆用1stClass里的控件算了,
 
后退
顶部