Solved

Multiple colors in a treeview node's Text

Posted on 2003-11-05
7
1,284 Views
Last Modified: 2007-12-19
Hi everyone.

I'm using a TTreeView to display a list of item which have subitems... so far so good. Problem is that for each parent item I have more information that I'd like to display Next to the item between parenthesis, the problem I have is that I'd like the text between parenthesis to be of another color. I wrote a nice little procedure in the AdvancedCustomDraw of my treeview (see below) but for some reasons I can't get the 2 colors... Anybody has an idea?

//////////////////// BeginCode

      if cdsSelected in state then
        Font.Color := clHighlightText
      else
        Font.Color := clWindowText;

      TextOut(NodeRect.Left, NodeRect.Top, 'blah');
      li_TextWidth := TextWidth('blah');

      if cdsSelected in state then
        Font.Color := clInactiveCaptionText
      else
        Font.Color := clGray;

      TextOut(NodeRect.Left + 1 + li_TextWidth, NodeRect.Top, '(ExtraText)');
   
//////////////////// EndCode

The whole text for the Node ends up begin of clInactiveCaptionText color... Anybody has an idea?
0
Comment
Question by:A1ternity
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 4
  • 3
7 Comments
 
LVL 26

Accepted Solution

by:
EddieShipman earned 500 total points
ID: 9690286
Try this example from Serge Gubenko. I've tweeked it a bit to move the frame over a little.

procedure TForm1.TreeView1CustomDrawItem(Sender: TCustomTreeView;
  Node: TTreeNode; State: TCustomDrawState; var DefaultDraw: Boolean);
var
  XNodeRect :trect;
  XDC: HDC;
  XBrush, XFrame: HBrush;
  XCaption: string;
  XFormat: longint;
  XFont: TFont;
begin
  DefaultDraw:=true;
  XNodeRect:=Node.DisplayRect(true);
  if UpperCase(node.Text)='TWO (3)' then
  begin
    XNodeRect.Left:=XNodeRect.Right + 25;
    XNodeRect.Right:=XNodeRect.Right + Canvas.TextWidth(node.Text) + 25;
    XCaption:='$10';
    XFormat:=DT_VCENTER + DT_SINGLELINE + DT_CENTER;

    XDC:=GetDC(Sender.Handle);
    XBrush:=CreateSolidBrush(ColorToRGB(clYellow));
    XFrame:=CreateSolidBrush(ColorToRGB(clBlack));
    XFont:=TFont.Create;
    try
      XFont.Style:=[fsItalic, fsBold];
      XFont.Name:='Arial';
      XFont.Size:=10;

      FillRect(XDC, XNodeRect, XBrush);
      SelectObject(XDC, XFont.Handle);
      SetTextColor(XDC, clBlue);
      SetBkColor(XDC, clYellow);
      DrawText(XDC, PChar(XCaption),
               length(XCaption), XNodeRect, XFormat);
      FrameRect(XDC, XNodeRect, XFrame);
    finally
      DeleteObject(XBrush);
      DeleteObject(XFrame);
      ReleaseDC(Sender.Handle, XDC);
      XFont.Free;
    end;
  end;
end;
0
 

Author Comment

by:A1ternity
ID: 9694335
Well... this method works as long as you don't have to put more than 1 color on the text of a single particular node.

Thanks for the help but sadly it didn't work for what I'm trying to do...
0
 
LVL 26

Expert Comment

by:EddieShipman
ID: 9695879
Wanna bet??
0
Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
LVL 26

Expert Comment

by:EddieShipman
ID: 9696241
I do not have time to follow up on this bet but maybe you should look into
the HTMLTreeview from TMS. It allows you to format your node text with
a limited set of HTML tags, including font tags and can have multiple colors
on one line.

http://www.tmssoftware.com/htmltv.htm
0
 
LVL 26

Expert Comment

by:EddieShipman
ID: 9696267
In HTMLTreeview, this sets the text Blah to Blue and the Text (Extra Text) to red with two space between:
(Set the Node's text property when you edit them)
<font face="MS Sans Serif" size="8" color="clblue">Blah</font>&nbsp; &nbsp;<font face="MS Sans Serif" size="8" color="clred">(Extra Text)</font>
0
 

Author Comment

by:A1ternity
ID: 9696281
I will take a deeper look at the code sample you provided... I only had time to try it quickly this morning and can't return to it right now. As for using the HTML Treeview I'd rather avoid using another component...

I'll keep you informed...
0
 

Author Comment

by:A1ternity
ID: 9832188
Still havn't had the time to try the solution and that part of the project is put aside for now...

Still here are your points! (won't say I'm not a good sports! Didn't even try your solution and I still pay! ;)
0

Featured Post

[Webinar] Learn How Hackers Steal Your Credentials

Do You Know How Hackers Steal Your Credentials? Join us and Skyport Systems to learn how hackers steal your credentials and why Active Directory must be secure to stop them. Thursday, July 13, 2017 10:00 A.M. PDT

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Hello everybody This Article will show you how to validate number with TEdit control, What's the TEdit control? TEdit is a standard Windows edit control on a form, it allows to user to write, read and copy/paste single line of text. Usua…
Introduction I have seen many questions in this Delphi topic area where queries in threads are needed or suggested. I know bumped into a similar need. This article will address some of the concepts when dealing with a multithreaded delphi database…
In this video, viewers are given an introduction to using the Windows 10 Snipping Tool, how to quickly locate it when it's needed and also how make it always available with a single click of a mouse button, by pinning it to the Desktop Task Bar. Int…
Add bar graphs to Access queries using Unicode block characters. Graphs appear on every record in the color you want. Give life to numbers. Hopes this gives you ideas on visualizing your data in new ways ~ Create a calculated field in a query: …

695 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question