Solved

Custom drawing in TTreeView

Posted on 2002-06-26
6
345 Views
Last Modified: 2010-09-11
Hi,
I'm using C++ Builder 6. I would like to implement custom item drawing. More specifically, I would like to use several fonts in a single node.

I use the following code on AdvancedCustomItemDraw:
void __fastcall TForm5::pTreeAdvancedCustomDrawItem(
      TCustomTreeView *Sender, TTreeNode *Node, TCustomDrawState State,
      TCustomDrawStage Stage, bool &PaintImages, bool &DefaultDraw)
{
        DefaultDraw = false;

        TRect Rect = Node->DisplayRect(true);

        Sender->Canvas->Font->Name = "MS Serif";
        Sender->Canvas->Font->Size = 10;
        Sender->Canvas->Font->Color = clRed;
        Sender->Canvas->TextOut(Rect.Left, Rect.Top, "ala");

        Sender->Canvas->Font->Name = "Tahoma";

        Sender->Canvas->Font->Color = clBlack;
        Sender->Canvas->TextOut(Rect.Left+30, Rect.Top, "ala");
}

But the font does not change.
Please help.
0
Comment
Question by:ia_ia_ia_1
[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
  • 3
  • 2
6 Comments
 
LVL 5

Expert Comment

by:alanwhincup
ID: 7111122
Have a look in BCB's examples. I think theres a project called CustomDraw or CustDraw which should be able to help you. When I get to a machine with BCB on it I will have a go at solving your problem.

Cheers,

Alan
0
 
LVL 7

Expert Comment

by:Cynna
ID: 7111142
ia_ia_ia_1,

I don't do C (only Delphi ;-), but I think this is what you're looking for:

http://groups.google.com/groups?hl=hr&lr=&ie=UTF-8&oe=UTF8&newwindow=1&selm=37030D24.5275F19B%40cornell.edu&rnum=7
0
 
LVL 2

Author Comment

by:ia_ia_ia_1
ID: 7111196
The point is that I need to use different fonts in a SINGLE node - and it seems that only the first font I've used to draw text is used - all subsequent changes to the font are disregarded.
0
Technology Partners: 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 7

Accepted Solution

by:
Cynna earned 300 total points
ID: 7113152
ia_ia_ia_1,

I'm sorry, I misunderstood you.
When ported to Delphi, your code really has unexpected
results, just like you said. This seems to be a bug
in Delphi (and, obviously, in BC++). You might try installing latest service pack.

But, in the meantime, I did a little investigation about
this behaviour. It seems that problem lies in the fact that
TCustomTreeView replaces the default Brush and Font
OnChange events with its own CanvasChanged . As a
result, Brush and Font are changed only once, before using it. All subsequent changes are ignored by Canvas.

This can be fixed by saving original OnChange events of the
TCustomTreeView.Canvas, and later calling them explicitly to notify TCustomTreeView.Canvas that font has changed.


I tested this with Delphi, and I assume it'll work in BC++ too. Below is a Delphi version that solves this problem.

1. Add this event handlers to the Form declaration:

  public
    { Public declarations }
    OriginalBrushOnChanged: TNotifyEvent;
    OriginalFontOnChanged: TNotifyEvent;

   (....)



2. Add this to Form's OnCreate event:

    procedure TForm5.FormCreate(Sender: TObject);
    begin
      // Save original Canvas OnChange events:
      OriginalBrushOnChanged :=  Tree.Canvas.Brush.OnChange;
       OriginalFontOnChanged := pTree.Canvas.Font.OnChange;

    end;

3. Finally, explicitly call event handler after font
   change in AdvancedCustomItemDraw.

   change this line in your example:
 
        Sender->Canvas->Font->Color = clBlack;

   ... to these two lines:

        Sender->Canvas->Font->Color = clBlack;
        // explicit call to original event handler:
        OriginalFontOnChanged(Sender->Canvas->Font);

4. If you change brush, same goes for it too, except
   you, of course, call OriginalBrushOnChanged()...


I'm sorry I didn't post C++ version completely, but
I was unsure about declaration of the events in C++, and presumed you know how to translate this to Builder C++, since you posted your question here.
0
 
LVL 2

Author Comment

by:ia_ia_ia_1
ID: 7113215
Cynna,

Thanks for the investigation. I'm going to give this a try and come later with the results (and points, of course)
0
 
LVL 2

Author Comment

by:ia_ia_ia_1
ID: 7113267
It works! Thanks :)
0

Featured Post

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!

Question has a verified solution.

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

In this tutorial I will show you how to use the Windows Speech API in Delphi. I will only cover basic functions such as text to speech and controlling the speed of the speech. SAPI Installation First you need to install the SAPI type library, th…
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…
If you're a developer or IT admin, you’re probably tasked with managing multiple websites, servers, applications, and levels of security on a daily basis. While this can be extremely time consuming, it can also be frustrating when systems aren't wor…
This is my first video review of Microsoft Bookings, I will be doing a part two with a bit more information, but wanted to get this out to you folks.

734 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