In Tree view , the check box can be checked or unchecked . i want to change the color of check box , make it grey or some other color . is there any way to do it .
unit CheckTreeview;interfaceuses Windows, Messages, SysUtils, Classes, Controls,Commctrl,ComCtrls; const TVIS_CHECKED = $2000; type TCheckTreeview = class(TTreeView) private { Private declarations } protected { Protected declarations } procedure CreateParams(var Params: TCreateParams); override; public { Public declarations } function IsChecked(Node :TTreeNode) :Boolean; procedure SetChecked(Node :TTreeNode; Checked :Boolean); published { Published declarations } end;procedure Register;implementationprocedure Register; begin RegisterComponents('syz_component', [TCheckTreeview]); end;{ TCheckTreeview }procedure TCheckTreeview.CreateParams(var Params: TCreateParams); begin inherited; Params.Style:=Params.Style or TVS_CHECKBOXES; end;function TCheckTreeview.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 TCheckTreeview.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.
where to implement above code ?
cg1120's codes are delphi? I can give you vb code. I can email to you
下面是我做的VB源代码演示,其中选中框分为三种,全选,部分选,不选,是用自绘图片实现的(我想,也只有这样,第三方的控件肯定也是把图片包进去了) Private Const SEL_ALL = 3 Private Const SEL_NONE = 2 Private Const SEL_PART = 1Private Sub Form_Load() Dim oNode As Node
Set TreeView1.ImageList = ImageList1 TreeView1.LineStyle = tvwRootLines TreeView1.Indentation = 100 Set oNode = TreeView1.Nodes.Add(, , "ROOT", "ROOT", SEL_NONE) Call TreeView1.Nodes.Add(oNode, tvwChild, "SUB1", "SUB1", SEL_NONE) Call TreeView1.Nodes.Add(oNode, tvwChild, "SUB2", "SUB2", SEL_NONE) Call TreeView1.Nodes.Add(oNode, tvwChild, "SUB3", "SUB3", SEL_NONE) oNode.Expanded = True End SubPrivate Sub TreeView1_NodeClick(ByVal Node As MSComctlLib.Node) On Error GoTo err1 Dim bAllOn As Boolean Dim bAllOff As Boolean
If Node.Image = SEL_NONE Or Node.Image = SEL_PART Then Node.Image = SEL_ALL ElseIf Node.Image = SEL_ALL Then Node.Image = SEL_NONE End If
If Node.Children > 0 Then Call pRecurseSetChildren(Node, Node.Image) End If
If Node.Image = SEL_ALL Then bAllOn = pbCheckForAll(Node, SEL_ALL) If (bAllOn) Then pRecurseSetParents Node, SEL_ALL, True Else pRecurseSetParents Node, SEL_PART End If ElseIf Node.Image = SEL_NONE Then If (Node.Children > 0) Then pRecurseSetChildren Node, SEL_NONE End If bAllOff = pbCheckForAll(Node, SEL_NONE) If (bAllOff) Then pRecurseSetParents Node, SEL_NONE, False Else pRecurseSetParents Node, SEL_PART & sPf End If
End If
Exit Sub err1:
End SubPrivate Sub SetParentNode(Node As MSComctlLib.Node) Dim iCnt As Long Dim i As Long Dim iSel As Long Dim oNode As Node
iSel = 0 If Node.Children > 0 Then Set oNode = Node.Child For i = 1 To Node.Children If oNode.Image = SEL_ALL Then iSel = iSel + 1 End If Set oNode = oNode.Next Next End If If iSel > 0 Then If iSel < Node.Children Then Node.Image = SEL_PART Else Node.Image = SEL_ALL End If Else Node.Image = SEL_NONE End If
If Node.Parent Is Nothing Then Call SetParentNode(Node) End If
End SubPrivate Sub pRecurseSetChildren( _ ByRef nod As Node, _ ByVal vIconKey As Variant _ ) Dim nodS As Node Dim lS As Long Set nodS = nod.Child For lS = nodS.FirstSibling.Index To nodS.LastSibling.Index TreeView1.Nodes(lS).Image = vIconKey If (TreeView1.Nodes(lS).Children > 0) Then pRecurseSetChildren TreeView1.Nodes(lS), vIconKey End If Next lS End Sub Private Function pbCheckForAll( _ ByRef nod As Node, _ ByVal vIcon As Variant _ ) As Boolean Dim lS As Long pbCheckForAll = True For lS = nod.FirstSibling.Index To nod.LastSibling.Index If (TreeView1.Nodes(lS).Image <> vIcon) Then pbCheckForAll = False Exit For End If Next lSEnd Function Private Sub pRecurseSetParents(ByVal Node As Node, _ ByVal vIconKey As Variant, _ Optional ByVal bOn As Boolean = False _ ) Dim nodP As Node Dim lS As Long Dim vCheck As Variant Dim bCheck As Boolean If (Node.Parent Is Nothing) Then ' finished Else Set nodP = Node.Parent nodP.Image = vIconKey If (vIconKey = SEL_PART) Then pRecurseSetParents nodP, SEL_PART Else If (bOn) Then vCheck = SEL_ALL Else vCheck = SEL_NONE End If bCheck = pbCheckForAll(nodP, vCheck) If (bCheck) Then pRecurseSetParents nodP, vCheck Else pRecurseSetParents nodP, SEL_PART End If End If
Windows, Messages, SysUtils, Classes, Controls,Commctrl,ComCtrls;
const
TVIS_CHECKED = $2000;
type
TCheckTreeview = class(TTreeView)
private
{ Private declarations }
protected
{ Protected declarations }
procedure CreateParams(var Params: TCreateParams); override;
public
{ Public declarations }
function IsChecked(Node :TTreeNode) :Boolean;
procedure SetChecked(Node :TTreeNode; Checked :Boolean);
published
{ Published declarations }
end;procedure Register;implementationprocedure Register;
begin
RegisterComponents('syz_component', [TCheckTreeview]);
end;{ TCheckTreeview }procedure TCheckTreeview.CreateParams(var Params: TCreateParams);
begin
inherited;
Params.Style:=Params.Style or TVS_CHECKBOXES;
end;function TCheckTreeview.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 TCheckTreeview.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.
I can give you vb code.
I can email to you
Private Const SEL_ALL = 3
Private Const SEL_NONE = 2
Private Const SEL_PART = 1Private Sub Form_Load()
Dim oNode As Node
Set TreeView1.ImageList = ImageList1
TreeView1.LineStyle = tvwRootLines
TreeView1.Indentation = 100
Set oNode = TreeView1.Nodes.Add(, , "ROOT", "ROOT", SEL_NONE)
Call TreeView1.Nodes.Add(oNode, tvwChild, "SUB1", "SUB1", SEL_NONE)
Call TreeView1.Nodes.Add(oNode, tvwChild, "SUB2", "SUB2", SEL_NONE)
Call TreeView1.Nodes.Add(oNode, tvwChild, "SUB3", "SUB3", SEL_NONE)
oNode.Expanded = True
End SubPrivate Sub TreeView1_NodeClick(ByVal Node As MSComctlLib.Node)
On Error GoTo err1
Dim bAllOn As Boolean
Dim bAllOff As Boolean
If Node.Image = SEL_NONE Or Node.Image = SEL_PART Then
Node.Image = SEL_ALL
ElseIf Node.Image = SEL_ALL Then
Node.Image = SEL_NONE
End If
If Node.Children > 0 Then
Call pRecurseSetChildren(Node, Node.Image)
End If
If Node.Image = SEL_ALL Then
bAllOn = pbCheckForAll(Node, SEL_ALL)
If (bAllOn) Then
pRecurseSetParents Node, SEL_ALL, True
Else
pRecurseSetParents Node, SEL_PART
End If
ElseIf Node.Image = SEL_NONE Then
If (Node.Children > 0) Then
pRecurseSetChildren Node, SEL_NONE
End If
bAllOff = pbCheckForAll(Node, SEL_NONE)
If (bAllOff) Then
pRecurseSetParents Node, SEL_NONE, False
Else
pRecurseSetParents Node, SEL_PART & sPf
End If
End If
Exit Sub
err1:
End SubPrivate Sub SetParentNode(Node As MSComctlLib.Node)
Dim iCnt As Long
Dim i As Long
Dim iSel As Long
Dim oNode As Node
iSel = 0
If Node.Children > 0 Then
Set oNode = Node.Child
For i = 1 To Node.Children
If oNode.Image = SEL_ALL Then
iSel = iSel + 1
End If
Set oNode = oNode.Next
Next
End If
If iSel > 0 Then
If iSel < Node.Children Then
Node.Image = SEL_PART
Else
Node.Image = SEL_ALL
End If
Else
Node.Image = SEL_NONE
End If
If Node.Parent Is Nothing Then
Call SetParentNode(Node)
End If
End SubPrivate Sub pRecurseSetChildren( _
ByRef nod As Node, _
ByVal vIconKey As Variant _
)
Dim nodS As Node
Dim lS As Long
Set nodS = nod.Child
For lS = nodS.FirstSibling.Index To nodS.LastSibling.Index
TreeView1.Nodes(lS).Image = vIconKey
If (TreeView1.Nodes(lS).Children > 0) Then
pRecurseSetChildren TreeView1.Nodes(lS), vIconKey
End If
Next lS
End Sub
Private Function pbCheckForAll( _
ByRef nod As Node, _
ByVal vIcon As Variant _
) As Boolean
Dim lS As Long
pbCheckForAll = True
For lS = nod.FirstSibling.Index To nod.LastSibling.Index
If (TreeView1.Nodes(lS).Image <> vIcon) Then
pbCheckForAll = False
Exit For
End If
Next lSEnd Function
Private Sub pRecurseSetParents(ByVal Node As Node, _
ByVal vIconKey As Variant, _
Optional ByVal bOn As Boolean = False _
)
Dim nodP As Node
Dim lS As Long
Dim vCheck As Variant
Dim bCheck As Boolean If (Node.Parent Is Nothing) Then
' finished
Else
Set nodP = Node.Parent
nodP.Image = vIconKey
If (vIconKey = SEL_PART) Then
pRecurseSetParents nodP, SEL_PART
Else
If (bOn) Then
vCheck = SEL_ALL
Else
vCheck = SEL_NONE
End If
bCheck = pbCheckForAll(nodP, vCheck)
If (bCheck) Then
pRecurseSetParents nodP, vCheck
Else
pRecurseSetParents nodP, SEL_PART
End If
End If
End If
End Sub