Google Groups no longer supports new Usenet posts or subscriptions. Historical content remains viewable.
Dismiss

Change color in TTreeView

666 views
Skip to first unread message

J Leavitt

unread,
Oct 5, 1999, 3:00:00 AM10/5/99
to
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?

Steve Zimmelman

unread,
Oct 5, 1999, 3:00:00 AM10/5/99
to
It's actually pretty easy. In the OnCustomDrawItem event you can change the
Brush (Background) color or the Font (Foreground) color like this:

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...

Greg Lorriman

unread,
Oct 6, 1999, 3:00:00 AM10/6/99
to

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


Steve Zimmelman

unread,
Oct 6, 1999, 3:00:00 AM10/6/99
to
Greg's correct. I left out the With statement in the OnCustomDrawItem. It
should be:

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-

J Leavitt

unread,
Oct 6, 1999, 3:00:00 AM10/6/99
to
No I'm not. Right now I'm not doing anything other than adding it to the
tree like this:

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.


Steve Zimmelman

unread,
Oct 6, 1999, 3:00:00 AM10/6/99
to
>the code above, as it stands, would mess with the forms canvas not the
>treeview. Are you doing a "with treeview do begin"?

You're right. I must have been a bit too tired...<g>

-Steve-


0 new messages