Board index » cppbuilder » Canvas of a TShape

Canvas of a TShape

I have drawn a TShape (rectangle) on a form, and wish to access the
Canvas property to add text with the Canvas->TextRect(....) function.

Compiler gives error 'Controls::TGraphicControl::Canvas' is not
accessible.

How to draw on the canvas of a control?

 

Re:Canvas of a TShape


Dave,
        Create a new ControlCanvas and set it's Control property to your
TShape...

//in header...
TControlCanvas *CCanvas;

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

Quote
}

//---------------------------------------------------------------------------
void __fastcall TForm1::Button1Click(TObject *Sender)
{
        CCanvas->TextOut(5, 5, "Hello World!");
Quote
}

//---------------------------------------------------------------------------
void __fastcall TForm1::FormClose(TObject *Sender, TCloseAction &Action)
{
        delete CCanvas;

Quote
}

//Damon

Re:Canvas of a TShape


Actually the text IS visible. Maybe you have set it to the same colour as the
shape:-))
Quote
Dave Kelly wrote:
> Tried this .... text not visible!!

> >Dave,
> >       Create a new ControlCanvas and set it's Control property to your
> >TShape...

> >//in header...
> >TControlCanvas *CCanvas;

> >//in source...
> >__fastcall TForm1::TForm1(TComponent* Owner)
> >       : TForm(Owner)
> >{
> >    CCanvas = new TControlCanvas();
> >    CCanvas->Control = Shape1;
> >}
> >//---------------------------------------------------------------------------
> >void __fastcall TForm1::Button1Click(TObject *Sender)
> >{
> >       CCanvas->TextOut(5, 5, "Hello World!");
> >}
> >//---------------------------------------------------------------------------
> >void __fastcall TForm1::FormClose(TObject *Sender, TCloseAction &Action)
> >{
> >       delete CCanvas;
> >}

> >//Damon

Re:Canvas of a TShape


You're right - your example does work.

However it needs the ButtonClick event to do it.

I have placed my writing in the Form->Activate event, in which case it
doesn't print the text!

Any other ideas?

Re:Canvas of a TShape


Damon,

My specific requirement is I have placed a Rectangle below the legend
in a TChart, which provide supplementary information.

Originally I used TLabels, and set the Caption property appropriately,
but when I placed the labels in the right position they were behind
the TShapes, and so not visible.

I was hoping to use the TextRect method so that I can make sure my
text fits within the box. (I know I can set the Label width)

Know why your example works on the button event, but not any other (in
my case the TChart->OnAfterDraw)??

Quote
>Dave,
>    At least for the initial text, why not just put a TLabel on the Shape?

>//Damon

Re:Canvas of a TShape


On Tue, 30 Mar 1999 10:47:13 GMT, dke...@memco.co.uk (Dave Kelly)
wrote:

Quote

>Originally I used TLabels, and set the Caption property appropriately,
>but when I placed the labels in the right position they were behind
>the TShapes, and so not visible.

Did you send the TShape to the back or bring the TLabels to the front?

Mike Henderson
Inventor, Patent-Holder and Wordwide Distributor of
The Original Lossless Floss System (c)(r)(tm)

Re:Canvas of a TShape


Quote
> I was hoping to use the TextRect method so that I can make sure my
> text fits within the box. (I know I can set the Label width)

Dave,
        If you are set on using TextRect, there are two options that you can
do.  The the EASIEST thing to do, would be to drop a TPaintBox on your
TShape, and do something like the following in the PaintBox's OnPaint
event handler...

void __fastcall TForm1::PaintBox1Paint(TObject *Sender)
{
    PaintBox1->Color = Shape1->Brush->Color;
    PaintBox1->Canvas->TextRect(PaintBox1->ClientRect, 3, 3, "My Text");

Quote
}
> Know why your example works on the button event, but not any other (in
> my case the TChart->OnAfterDraw)??

The Parent window of the TShape is being repainted after your writing
code (the call to TextOut or TextRect) in the other events such as
Form->OnPaint and Form->OnActivate, etc.  This brings us to the next
option... trapping the Parent Window's WM_PAINT message...

//in header file...
public:
       void virtual __fastcall OrderedRePaint(TMessage &Msg);

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

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

    // Finally draw your stuff
    CCanvas->TextRect(PaintBox1->ClientRect, 3, 3, "My Text");

Quote
}

Both methods work, and are relatively easy to implement.  Good luck.

//Damon

Other Threads