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 - 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
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 - 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
Will Green <pha...@sheffield.ac.uk> a écrit dans le message :
36FFD99E...@sheffield.ac.uk...
//Damon
//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