In Customdrawitem event
If something then
canvas.brush.color := somecolor
else
canvas.brush.color := somothercolor;
I can't get this to work.
Any help or other suggestions?
Canvas.Brush.Color := clRed
Canvas.Font.Color := clWhite
If there are conditions for specific nodes to be certain colors, you'll need
to do some checking of your own and set them accordingly. I've used a
couple of different TList's to store nodes that should be different colors,
and stored the actual node pointer in the Data element of the Tlist.
Something like this:
// these should be in the scope of the entire unit.
Var
tlRed,tlGreen: Tlist ;
Procedure MyProc ;
Var
TrRoot,trChild : TTreeNode ;
Begin
tlRed := Tlist.Create ;
tlGreen:= Tlist.Create;
// Root will be Red
TrRoot := TreeView.Items.Add(nil,'This is a Root Node');
tlRed.Add(TrRoot);
// Child will be Green
TrChild:=TreeView1.Item.AddChild(TrRoot,'ChildNode');
tlGreen.Add(TrChild);
End;
Then in the OnCustomDrawItem of treeview you can check the Tlist's for the
current Node and change it's color.
procedure OnCustomDrawItem(Sender: TCustomTreeView; Node: TTreeNode;
State: TCustomDrawState; var DefaultDraw: Boolean);
Begin
If FindNode(tlRed,Node) Then
Canvas.Font.Color := clRed
Else If FindNode(tlGreen,Node) Then
Canvas.Font.Color := clGreen ;
End;
Function FindNode(tr:TreeNode;tl:Tlist): Boolean;
Var i : Integer
Begin
Result := False ;
For i := 0 To tr.Count-1 Do Begin
If tl.Items[i] = tr Then Begin
Result := True ;
Break;
End;
End;
End;
Don't forget to Free any Tlist objects you create. Oh, and, this is
untested code...<g>
Hope this helps.
-Steve-
J Leavitt <john.l...@smed.com> wrote in message
news:7tdj3a$d2...@forums.borland.com...
J Leavitt <john.l...@smed.com> wrote in message
news:7tdj3a$d2...@forums.borland.com...
> I posted a message asking how to change the color of a node in TTreeView
> last week and received a reply something like:
>
> In Customdrawitem event
>
> If something then
> canvas.brush.color := somecolor
> else
> canvas.brush.color := somothercolor;
>
> I can't get this to work.
>
> Any help or other suggestions?
the code above, as it stands, would mess with the forms canvas not the
treeview. Are you doing a "with treeview do begin"?
--
Greg Lorriman
Handy, free utils at http://www.lorriman.demon.co.uk
procedure OnCustomDrawItem(Sender: TCustomTreeView; Node: TTreeNode;
State: TCustomDrawState; var DefaultDraw: Boolean);
Begin
WIth Sender Do Begin
If FindNode(tlRed,Node) Then
Canvas.Font.Color := clRed
Else If FindNode(tlGreen,Node) Then
Canvas.Font.Color := clGreen ;
End;
End;
>I just wanted to change the color of a couple of the nodes if they met
>certain requirements.
The FindNode function in this case is the criteria. You can certainly
change this to meet whatever needs you have. I agree with you, in some
cases colors are less crowed.
-Steve-
tmp := mTreeList.Add(mNode); // add the pointer to the list. mNode
is a structure.
tvFunctions.Items.AddChildObject(tnParent, ListItem.Caption,
mTreeList[tmp]);
I just wanted to change the color of a couple of the nodes if they met
certain requirements. Maybe I should do something else like stateImages? I
just thought a color would be less crowded.
Thanks.
You're right. I must have been a bit too tired...<g>
-Steve-