RAD Studio |  |

Creating Drawing Spaces
See Also
Collapse All

The TCanvas class is defined in the Graphics unit, and encapsulates a Windows device context. This class handles all drawing for forms, visual containers (such as panels) and the printer object (see Printing). Using the canvas object, you need not worry about allocating pens, brushes, palettes, and so on—all the allocation and deallocation are handled for you.

TCanvas includes a large number of primitive graphics routines to draw lines, shapes, polygons, fonts, etc. onto any control that contains a canvas. For example, here is a button event handler that draws a line from the upper left corner to the middle of the form and outputs some raw text onto the form:

Copy Code

    procedure TForm1.Button1Click(Sender: TObject);
    begin
    Canvas.Pen.Color := clBlue;
    Canvas.MoveTo( 10, 10 );
    Canvas.LineTo( 100, 100 );
    Canvas.Brush.Color := clBtnFace;
    Canvas.Font.Name := 'Arial';
    Canvas.TextOut( Canvas.PenPos.x, Canvas.PenPos.y,'This is the end of the line' );
    end;

Copy Code

    void __fastcall TForm1::Button1Click(TObject *Sender)
    {
    Canvas->Pen->Color = clBlue;
    Canvas->MoveTo( 10, 10 );
    Canvas->LineTo( 100, 100 );
    Canvas->Brush->Color = clBtnFace;
    Canvas->Font->Name = "Arial";
    Canvas->TextOut( Canvas->PenPos.x, Canvas->PenPos.y,"This is the end of the line" );
    }

The TCanvas object defined in the Graphics unit also protects you against common Windows graphics errors, such as restoring device contexts, pens, brushes, and so on to the value they had before the drawing operation. TCanvas is used everywhere in the VCL that drawing is required or possible, and makes drawing graphics both fail-safe and easy.

See Also

Printing

Copyright(C) 2009 Embarcadero Technologies, Inc. All Rights Reserved.
What do you think about this topic? Send feedback!