TreeView OnMouseMove "highlight" items and selected item rectangle.

TreeView OnMouseMove "highlight" items and selected item rectangle.

Post by Elin » Thu, 11 Dec 2003 08:46:27


Dear Builders,

...just can't get this working :-(. (I am trying)

I have posted a little example in borland.public.attachments.


Just want to get TTreeView to change the text color(or eg. style) when the
mouse is over an(that) item.

Also if you take a look at the simple example you'll see, when an item is
selected, a rectangle with a different inner color, appears. But it has a
'bug' in it. (is explained in the example) So it's almost what I want.



This is the CustomDrawItem part...

void __fastcall TForm1::TreeViewCustomDrawItem(TCustomTreeView *Sender,
TTreeNode *Node, TCustomDrawState State, bool &DefaultDraw)
{
if (State.Contains(cdsSelected))
{
// Selected...
TreeView->Canvas->Font->Name = "Tahoma";
TreeView->Canvas->Font->Color = (TColor)0x006A6A6A;
TreeView->Canvas->Font->Size = 10;
TreeView->Canvas->Font->Style = TFontStyles(); // << fsBold;
TreeView->Canvas->Brush->Color = (TColor)0x00FFD5AA;

RECT LineRect;
LineRect = Node->DisplayRect(false);
// HDC hDC = TreeView->Canvas->Handle;
// DrawFocusRect(hDC, &LineRect);
TreeView->Canvas->FillRect(LineRect);
TreeView->Canvas->DrawFocusRect(LineRect);
}
else
{
//Not Selected...
TreeView->Canvas->Font->Name = "Tahoma";
TreeView->Canvas->Font->Color = (TColor)0x006A6A6A;
TreeView->Canvas->Font->Size = 10;
TreeView->Canvas->Font->Style = TFontStyles(); // << fsBold;
}

// I refresh it on the onchange... but that seems a bit ugly?

}

Elina.
 
 
 

TreeView OnMouseMove "highlight" items and selected item rectangle.

Post by Team » Thu, 11 Dec 2003 09:13:53


Why can't you just use the HotTrack property? It already does that for you.
Or are you trying to customize it beyond what it already does?


Gambit

 
 
 

TreeView OnMouseMove "highlight" items and selected item rectangle.

Post by Elin » Thu, 11 Dec 2003 09:59:35


you.

It's just a default color with underline... in XP it's black.

Elina.
 
 
 

TreeView OnMouseMove "highlight" items and selected item rectangle.

Post by Team » Fri, 12 Dec 2003 06:51:51


The color is customizable in the Control Panel.


Gambit
 
 
 

TreeView OnMouseMove "highlight" items and selected item rectangle.

Post by Elin » Fri, 12 Dec 2003 06:56:11

> The color is customizable in the Control Panel.

Ok, so I should make a message to everyone that they have to change it
before using my program..? *ahem*

I'm searching for a solution on the web in the meantime...

Elina.
 
 
 

TreeView OnMouseMove "highlight" items and selected item rectangle.

Post by Team » Fri, 12 Dec 2003 07:23:57


No, you should use whatever colors the user has configured. They have
custom colors set up for a reason.


Gambit
 
 
 

TreeView OnMouseMove "highlight" items and selected item rectangle.

Post by Elin » Fri, 12 Dec 2003 07:37:17


*sigh*

That maybe just what I don't want? That may be just the reason of this post?

Hope that somebody does know the/some answer, that's what newsgroups
are for aren't they? If your stuck you can get help and you can take a look
around if you can help other people.

Elina.
 
 
 

TreeView OnMouseMove "highlight" items and selected item rectangle.

Post by Team » Fri, 12 Dec 2003 08:19:42


post?

Fine. Your code fails because you did not set the DefaultDraw parameter to
false, or make the Canvas transparent for when the text is drawn on top of
your custom drawing later. I would suggest changing your drawing code to
the following instead, it works fine for me:

// make sure to set the HotTrack property to true as well

void __fastcall TForm1::TreeViewCustomDrawItem(TCustomTreeView *Sender,
TTreeNode *Node, TCustomDrawState State, bool &DefaultDraw)
{
DefaultDraw = false;

TRect LineRect = Node->DisplayRect(false);
TRect TextRect = Node->DisplayRect(true);
TCanvas *pCanvas = TreeView->Canvas;

pCanvas->Font->Name = "Tahoma";
pCanvas->Font->Color = (TColor)0x006A6A6A;
pCanvas->Font->Size = 10;
pCanvas->Font->Style = TFontStyles();

if( State.Contains(cdsSelected) || State.Contains(cdsHot) )
{
// Selected...
pCanvas->Brush->Color = (TColor)0x00FFD5AA;
}

pCanvas->FillRect(LineRect);

TextRect.Right = LineRect.Right;
pCanvas->TextRect(TextRect, TextRect.Left, TextRect.Top,
Node->Text);

if( State.Contains(cdsFocused) )
pCanvas->DrawFocusRect(LineRect);
}

As for the refreshing being "ugly", you should not be calling calling
Refresh(), that forces a full redraw of the entire control. Use
Invalidate() instead. In fact, you don't need to refresh the TreeView
manually at all. When you change the selection to a different item, the
TreeView will automatically redraw itself accordingly anyway, so get rid of
your OnChange handler altogether.


Gambit

P.S. next time you post example projects, please make sure that they
actually compile first! I had to change your project to disable runtime
packages, it was trying to link to a package that does not exist on my
machine.
 
 
 

TreeView OnMouseMove "highlight" items and selected item rectangle.

Post by Elin » Fri, 12 Dec 2003 09:07:09


I want to apologize, maybe my reaction was a bit too much. I know you're
trying to help a lot of people, thank your for that.


Thx, now I also understand some things better, it works great! I'm not
a pro in programming, but I find it very interesting and I'm still learning
a lot.



You're right, I even didn't think of that.


Sorry again, Elina.

(Maybe it's that time of the month :p.)