Navigation: TextEd > Extension and Scripts > Classes (non-visual) >

TCanvas

 

 

 

TCanvas provides an abstract drawing space for objects that must render their own images. Several visual components have a canvas, like the ListBox, Panel or Form controls.

 


Properties


 


property TBrush Brush

TBrush represents the color and pattern used to fill solid shapes. TBrush encapsulates the Windows brush object (HBRUSH) and is used to fill solid shapes, such as rectangles and ellipses, with a color or pattern.

 

Common properties available in the TBrush object are:

 

 

property TBitmap Bitmap

Specifies an external bitmap image that defines a pattern for the brush.

 

 

property TColor Color

Indicates the color of the brush.

 

 

property TBrushStyle Style

Specifies the pattern for the brush.

 

TBrushStyle = (

  bsSolid,

  bsClear,

  bsHorizontal,

  bsVertical,

  bsFDiagonal,

  bsBDiagonal,

  bsCross,

  bsDiagCross

);

 

Ex.

form.canvas.Brush.Color = clRed;

form.canvas.Brush.Style = bsSolid;

form.canvas.FillRect(rect);

 

 

property TFont Font;

TFont encapsulates a system font. TFont describes font characteristics used when displaying text. TFont defines a set of characters by specifying the height, font family (typeface), attributes (such as bold or italic) and so on.


Common properties available in the TFont object are:

 

 

property TFontName Name

Identifies the typeface of the font.

 

 

property TColor Color

Indicates the color of the brush.

 


property int Size;

Specifies the height of the font in points.

 


property TFontStyles Style

Determines whether the font is normal, italic, underlined, bold, and so on.


TFontStyle = (

  fsBold,

  fsItalic,

  fsUnderline,

  fsStrikeOut

);

 

Ex.

form.Canvas.Font.Name = "Courier New";

form.Canvas.Font.Size = 10;

form.Canvas.Font.Color = clBlack;

form.Canvas.Font.Style = fsBold + fsUnderline;

form.Canvas.TextOut(0,0,"Hello World!");


 

property TPen Pen;

TPen is used to draw lines or outline shapes on a canvas.

 

Common properties available in the TFont object are:

 

 

property TColor Color

Determines the color used to draw lines on the canvas.

 

 

property int Width;

Specifies the width of the pen in pixels.

 

 

property TPenStyle Style

Use Style to draw a dotted or dashed line, or to omit the line that appears as a frame around shapes.

 

TPenStyle = (

  psSolid,

  psDash,

  psDot,

  psDashDot,

  psDashDotDot,

  psClear,

  psInsideFrame,

  psUserStyle,

  psAlternate

);

 

Ex.

form.Canvas.Pen.Width = 1;

form.Canvas.Pen.Color = clGray;

form.Canvas.Pen.Style = psDot;

form.Canvas.MoveTo(10,20); // Move pen to x,y pos

form.Canvas.LineTo(190,20); // Draw line to x,y pos


 

property TColor Pixels[int X, Y];

Read Pixels to learn the color on the drawing surface at a specific pixel position within the current clipping region. If the position is outside the clipping rectangle, reading the value of Pixels returns -1.

 

Write Pixels to change the color of individual pixels on the drawing surface. Use Pixels for detailed effects on an image.

 


Methods


 

procedure Draw(int X, int Y, TGraphic Graphic);

Renders the graphic specified by the Graphic parameter on the canvas at the location given by the coordinates (X, Y).


Ex.

TBitmap bitmap = TBitmap.Create;

bitmap.LoadFromFile("MyBitmap.bmp");

form.Canvas.Draw(10,20,bitmap);

 


procedure Ellipse(int X1, int Y1, int X2, int Y2);

Call Ellipse to draw a circle or ellipse on the canvas. Specify the bounding rectangle by giving the top left point at pixel coordinates (X1, Y1) and the bottom right point at (X2, Y2).

 

 

procedure LineTo(int X, int Y);

Draws a line on the canvas from the current pen pos (set by MoveTo) to the point specified by X and Y, and sets the pen position to (X, Y).

 

 

procedure MoveTo(int X, int Y);

Move the current pen position to a point specified by X and Y.

 

 

procedure Rectangle(int X1, int Y1, int X2, int Y2);

Draws a rectangle on the canvas. Use Rectangle to draw a rectangle using Pen and fill it with Brush.

 

 

Giving four coordinates that define the upper left corner at the point (X1, Y1) and the lower right corner at the point (X2, Y2).

 


procedure RoundRect(int X1, int Y1, int X2, int Y2, int X3, int Y3);

Draws a rectangle with rounded corners on the canvas. Use RoundRect to draw a rounded rectangle using Pen and fill it with Brush. The curve of the rounded corners matches the curvature of an ellipse with width X3 and height Y3.

 


procedure StretchDraw(int X1, int Y1, int X2, int Y2, TGraphic Graphic);

Draws the graphic specified by the Graphic parameter in the rectangle specified by the X1, Y1 and X2, Y2 parameters.


Ex.

TBitmap bitmap = TBitmap.Create;

bitmap.LoadFromFile("MyBitmap.bmp");

form.Canvas.StretchDraw(10,20,100,200,bitmap);

 

 

function int TextHeight(string Text);

Returns the height, in pixels, of a string rendered in the current font.


Ex.

int textHeight = form.Canvas.TextHeight("Tg_");

 


procedure TextOut(int X, int Y, string Text);

Writes a string on the canvas, starting at the point (X,Y). Use TextOut to write a string onto the canvas. The string will be written using the current value of Font.


Ex.

form.Canvas.TextOut(10,20,"Hello World!");

 


function int TextWidth(string Text);

Returns the width, in pixels, of a string rendered in the current font.


Ex.

int textWidth = form.Canvas.TextWidth("W");


 


 


 


 


 


 




 

 

 

 

Copyright © 2022 Rickard Johansson