令
令狐小侠
Unregistered / Unconfirmed
GUEST, unregistred user!
最近想做一个treeview上带checkbox的控件,
基本思想 是从 treeview继承....
查了一些资料..已经解决了 大部分问题...
现在有一个问题解决不了,大家帮看看
就是当 checkbox属性设为 false的时候,那个框框 因该 消失掉....但实际上是没消失?为什么
unit CheckBoxTreeview;
interface
uses
SysUtils, Classes, Controls, ComCtrls,windows,commctrl;
const
TVIS_Checked = $00002000;
TVIS_NotChecked = $00000800;
type
TCheckBoxTreeview = class(TTreeView)
private
fCheckbox: boolean;
fCheckChildrens: boolean;
procedure SetCheckbox(const Value: boolean);
function Getcheckbox: boolean;
function GetChecked(const node: Ttreenode): boolean;
procedure SetChecked(const node: Ttreenode; const Value: boolean);
{ Private declarations }
protected
procedure CreateParams(var params : TCreateParams);override;
{ Protected declarations }
public
constructor Create(AOwner: TComponent);
property Checked[const node : Ttreenode] : boolean read GetChecked write SetChecked;
{ Public declarations }
published
property CheckBox : boolean read Getcheckbox write SetCheckbox;
property CheckChildrens : boolean read fCheckChildrens write fCheckChildrens;
{ Published declarations }
end;
procedure Register;
implementation
procedure Register;
begin
RegisterComponents('Samples', [TCheckBoxTreeview]);
end;
{ TCheckBoxTreeview }
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 //就是当 checkbox属性设为 false的时候,那个框框 因该 消失掉....但实际上是没消失?为什么
else
Style := Style or Value;
SetWindowLong(Ctl.Handle, GWL_STYLE, Style);
end;
end;
constructor TCheckBoxTreeview.Create;
begin
inherited;
SetComCtlStyle(self,$0100,fCheckbox);
end;
procedure TCheckBoxTreeview.CreateParams(var params: TCreateParams);
begin
inherited;
params.Style := params.Style or TVS_CHECKBOXES;
end;
function TCheckBoxTreeview.Getcheckbox: boolean;
begin
result := fcheckbox;
end;
function TCheckBoxTreeview.GetChecked(const node: Ttreenode): boolean;
var
ItemState:TTVITEM;
begin
result := false;
if node<>nil then
begin
itemstate.hItem := node.ItemId;
itemstate.state := TVIF_STATE;
treeview_getitem(node.GetHandle,itemState);
result := ((itemstate.state and TVIS_Checked )= TVIS_Checked);
end
else
raise exception.Create('node is nil');
end;
procedure TCheckBoxTreeview.SetCheckbox(const Value: boolean);
begin
fcheckbox := value;
SetComCtlStyle(self,TVS_CHECKBOXES,fcheckbox);
end;
procedure TCheckBoxTreeview.SetChecked(const node: Ttreenode;
const Value: boolean);
var
ItemState : TtvItem;
lsnode : Ttreenode;
begin
if node <> nil then
begin
fillchar(itemstate,sizeof(itemstate),0);
itemstate.mask := TVIF_STATE;
itemstate.hItem := node.ItemId;
itemstate.stateMask := TVIs_StateImageMask;
if value then
itemstate.state := TVIS_Checked
else
itemstate.state := TVIS_NotChecked;
treeview_Setitem(node.GetHandle,itemState);
if not fCheckChildrens then
exit
else
begin
if node.HasChildren then
begin
lsnode := node.GetNext;
if lsnode = nil then
exit;
while node.Level<lsnode.Level do
begin
fillchar(itemstate,sizeof(itemstate),0);
itemstate.mask := TVIF_STATE;
itemstate.hItem := lsnode.ItemId;
itemstate.stateMask := TVIs_StateImageMask;
if value then
itemstate.state := TVIS_Checked
else
itemstate.state := TVIS_NotChecked;
treeview_Setitem(lsnode.GetHandle,itemState);
lsnode := lsnode.GetNext;
if lsnode=nil then
exit;
end;
end
else
exit;
end;
end
else
raise Exception.Create('node is nil');
end;
end.
还有个问题,就是象 TVIS_Checked 这些 常量的 意思是什么...为什么这样定义.
望高手指点
基本思想 是从 treeview继承....
查了一些资料..已经解决了 大部分问题...
现在有一个问题解决不了,大家帮看看
就是当 checkbox属性设为 false的时候,那个框框 因该 消失掉....但实际上是没消失?为什么
unit CheckBoxTreeview;
interface
uses
SysUtils, Classes, Controls, ComCtrls,windows,commctrl;
const
TVIS_Checked = $00002000;
TVIS_NotChecked = $00000800;
type
TCheckBoxTreeview = class(TTreeView)
private
fCheckbox: boolean;
fCheckChildrens: boolean;
procedure SetCheckbox(const Value: boolean);
function Getcheckbox: boolean;
function GetChecked(const node: Ttreenode): boolean;
procedure SetChecked(const node: Ttreenode; const Value: boolean);
{ Private declarations }
protected
procedure CreateParams(var params : TCreateParams);override;
{ Protected declarations }
public
constructor Create(AOwner: TComponent);
property Checked[const node : Ttreenode] : boolean read GetChecked write SetChecked;
{ Public declarations }
published
property CheckBox : boolean read Getcheckbox write SetCheckbox;
property CheckChildrens : boolean read fCheckChildrens write fCheckChildrens;
{ Published declarations }
end;
procedure Register;
implementation
procedure Register;
begin
RegisterComponents('Samples', [TCheckBoxTreeview]);
end;
{ TCheckBoxTreeview }
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 //就是当 checkbox属性设为 false的时候,那个框框 因该 消失掉....但实际上是没消失?为什么
else
Style := Style or Value;
SetWindowLong(Ctl.Handle, GWL_STYLE, Style);
end;
end;
constructor TCheckBoxTreeview.Create;
begin
inherited;
SetComCtlStyle(self,$0100,fCheckbox);
end;
procedure TCheckBoxTreeview.CreateParams(var params: TCreateParams);
begin
inherited;
params.Style := params.Style or TVS_CHECKBOXES;
end;
function TCheckBoxTreeview.Getcheckbox: boolean;
begin
result := fcheckbox;
end;
function TCheckBoxTreeview.GetChecked(const node: Ttreenode): boolean;
var
ItemState:TTVITEM;
begin
result := false;
if node<>nil then
begin
itemstate.hItem := node.ItemId;
itemstate.state := TVIF_STATE;
treeview_getitem(node.GetHandle,itemState);
result := ((itemstate.state and TVIS_Checked )= TVIS_Checked);
end
else
raise exception.Create('node is nil');
end;
procedure TCheckBoxTreeview.SetCheckbox(const Value: boolean);
begin
fcheckbox := value;
SetComCtlStyle(self,TVS_CHECKBOXES,fcheckbox);
end;
procedure TCheckBoxTreeview.SetChecked(const node: Ttreenode;
const Value: boolean);
var
ItemState : TtvItem;
lsnode : Ttreenode;
begin
if node <> nil then
begin
fillchar(itemstate,sizeof(itemstate),0);
itemstate.mask := TVIF_STATE;
itemstate.hItem := node.ItemId;
itemstate.stateMask := TVIs_StateImageMask;
if value then
itemstate.state := TVIS_Checked
else
itemstate.state := TVIS_NotChecked;
treeview_Setitem(node.GetHandle,itemState);
if not fCheckChildrens then
exit
else
begin
if node.HasChildren then
begin
lsnode := node.GetNext;
if lsnode = nil then
exit;
while node.Level<lsnode.Level do
begin
fillchar(itemstate,sizeof(itemstate),0);
itemstate.mask := TVIF_STATE;
itemstate.hItem := lsnode.ItemId;
itemstate.stateMask := TVIs_StateImageMask;
if value then
itemstate.state := TVIS_Checked
else
itemstate.state := TVIS_NotChecked;
treeview_Setitem(lsnode.GetHandle,itemState);
lsnode := lsnode.GetNext;
if lsnode=nil then
exit;
end;
end
else
exit;
end;
end
else
raise Exception.Create('node is nil');
end;
end.
还有个问题,就是象 TVIS_Checked 这些 常量的 意思是什么...为什么这样定义.
望高手指点