Google grupe više ne podržavaju nove postove ni pretplate na Usenetu. Stari sadržaj ostaje vidljiv.

Font background color problem in TTreeView

5 prikaza
Preskoči na prvu nepročitanu poruku

laurent delayen

nepročitano,
27. ožu 1999. 03:00:0027. 03. 1999.
u
Hi!

i'm using a TTreeView component, and i changed the background color using
the Color property. But when i add nodes to that TTreeView component, they
all have white background. How can i change their background color???

Thanks,

-Shag

Will Green

nepročitano,
29. ožu 1999. 03:00:0029. 03. 1999.
u
The background colour will always be clWindow I'm afraid (you could change the
clWindow colour of course but that would affect all applications). If anyone
knows any different I'm happy to be corrected. I think the background colour of
the text should be transparent, that way whatever the colour it still looks
right.

Will Green - Bits Editor
--
"Injustice anywhere is a threat to justice everywhere." - King
The Bits C++Builder Site: http://www.richplum.co.uk/cbuilder
Delphi Developer's Survey: http://www.richplum.co.uk/survey

laurent delayen

nepročitano,
29. ožu 1999. 03:00:0029. 03. 1999.
u
Well Background color of the TTreeView can be changed via the Color
property.
But the TTreeNode->Text is always black on white, and not black on
transparent. And this is where is the problem. Cannot find a way to make it
transparent, or at least the same color as the TTreeView background.
This sucks big time!


Will Green <pha...@sheffield.ac.uk> a écrit dans le message :
36FEBDA4...@sheffield.ac.uk...


> The background colour will always be clWindow I'm afraid (you could change
the
> clWindow colour of course but that would affect all applications). If
anyone
> knows any different I'm happy to be corrected. I think the background
colour of
> the text should be transparent, that way whatever the colour it still
looks
> right.
>

Will Green

nepročitano,
29. ožu 1999. 03:00:0029. 03. 1999.
u
I was voicing an opinion, not stating a fact, it's black on the clWindow colour
but I THINK it should be transparent!

Will Green - Bits Editor
--
"Injustice anywhere is a threat to justice everywhere." - King
The Bits C++Builder Site: http://www.richplum.co.uk/cbuilder
Delphi Developer's Survey: http://www.richplum.co.uk/survey

laurent delayen

nepročitano,
30. ožu 1999. 03:00:0030. 03. 1999.
u
;) Anyway i still have that problem......

Will Green <pha...@sheffield.ac.uk> a écrit dans le message :

36FFD99E...@sheffield.ac.uk...

Damon Chandler

nepročitano,
31. ožu 1999. 03:00:0031. 03. 1999.
u
Looks like you're going to have to use CustomDraw. Search the MSDN
website for "Custom Draw," it's pretty detailed.

//Damon

Damon Chandler

nepročitano,
1. tra 1999. 03:00:0001. 04. 1999.
u
laurent,
Ok, here's a method to change the background color (and/or Font color)
of individual items of a TTreeView. The goal is to create a new
ControlCanvas and set it's Control property to the TreeView. This will
give you a Canvas to draw on. Then every time the TreeView is painted,
you have to update the TreeView by painting on this Canvas. To do this,
you'll have to trap the WM_PAINT message sent to the Form.

//in header...
TControlCanvas *TVCanvas;

virtual void __fastcall PaintHandler(TMessage &Msg);

BEGIN_MESSAGE_MAP
MESSAGE_HANDLER(WM_PAINT, TMessage, PaintHandler)
END_MESSAGE_MAP(TForm)


//in source...
__fastcall TForm1::TForm1(TComponent* Owner)
: TForm(Owner)
{
TVCanvas = new TControlCanvas();
TVCanvas->Control = TreeView1;

//Draw text transparently!
TVCanvas->Brush->Style = bsClear;

TVCanvas->Font = TreeView1->Font;
}

void __fastcall TForm1::FormClose(TObject *Sender, TCloseAction &Action)
{
delete TVCanvas;
}

void __fastcall TForm1::PaintHandler(TMessage &Msg)
{
//let everything else draw first
TForm::Dispatch(&Msg);

//manage drawing the TreeView
for (int index = 0; index < TreeView1->Items->Count; index++)
{
if (index != TreeView1->Selected->Index)
{
TRect NodeRect =
TreeView1->Items->Item[index]->DisplayRect(true);
TVCanvas->Brush->Color = TreeView1->Color;
TVCanvas->FillRect(NodeRect);

//Change the font color of individual items
switch(index)
{
case 0: TVCanvas->Font->Color = clRed;
break;
case 1: TVCanvas->Font->Color = clGreen;
break;
case 2: TVCanvas->Font->Color = clBlue;
break;
default: TVCanvas->Font->Color = clBlack;
}

TVCanvas->TextOut(NodeRect.Left + 2, NodeRect.Top + 1,
TreeView1->Items->Item[index]->Text);
}
}
}

void __fastcall TForm1::TreeView1Change(TObject *Sender, TTreeNode
*Node)
{
Refresh();
}

void __fastcall TForm1::FormPaint(TObject *Sender)
{
TreeView1->Refresh();
}


Ok, that's one way to do it. The other, more robust way is to derive
your own TreeView from TTreeView, and trap the WM_PAINT message in your
derived class...


//in derived MyTreeView header...
private:
TControlCanvas *TVCanvas;
public:
__fastcall TMyTreeView(TComponent* Owner);
__fastcall TMyTreeView::~TMyTreeView();
virtual void __fastcall PaintIt(TMessage &Message);

BEGIN_MESSAGE_MAP
MESSAGE_HANDLER(WM_PAINT, TMessage, PaintIt);
END_MESSAGE_MAP(TTreeView)

//in derived MyTreeView source...
__fastcall TMyTreeView::TMyTreeView(TComponent* Owner)
: TTreeView(Owner)
{
TVCanvas = new TControlCanvas();
TVCanvas->Control = this;
TVCanvas->Brush->Style = bsClear;
}

__fastcall TMyTreeView::~TMyTreeView()
{
delete TVCanvas;
}

void __fastcall TMyTreeView::PaintIt(TMessage &Msg)
{
TTreeView::Dispatch(&Msg);
TVCanvas->Brush->Color = Color;

for (int index = 0; index < Items->Count; index++)
{
if (index != Selected->Index)
{
TRect NodeRect = Items->Item[index]->DisplayRect(true);

//Change the font color of individual items
switch(index)
{
case 0: TVCanvas->Font->Color = clRed;
break;
case 1: TVCanvas->Font->Color = clGreen;
break;
case 2: TVCanvas->Font->Color = clBlue;
break;
default: TVCanvas->Font->Color = clBlack;
}

TVCanvas->FillRect(NodeRect);
TVCanvas->TextOut(NodeRect.Left + 2, NodeRect.Top + 1,
Items->Item[index]->Text);
}
}
}


There are some things to note, specifically in the line...

TRect NodeRect = Items->Item[index]->DisplayRect(true);

If you set the parameter to the DisplayRect method to false, then
NodeRect will be the entire line of the TreeNode, including the buttons
and images (if present). This is in fact one way to draw your own
TreeView buttons, and indeed, adds a greater degree of control. The
problem is that it will be difficult to tell how it is connected. It
can be done by testing it's previous node and next node, but it is
somewhat of a hassle. Also, if you do have an imagelist set to
MyTreeView, you'll have to offset the text appropriately (if
"DisplayRect(false)" is specified). Either way, it can be done without
CustomDraw, and the second method allows you nearly complete control
over the look of your TreeView. Good luck.

//Damon

0 novih poruka