Device description table and TCanvas)
Windows uses the device context (DC) to describe the canvas on which users can draw pictures. DC can be used to draw on many surfaces:
- Window client area or frame (to a window's client area or frame)
- To the Desktop)
- Memory (to memory)
- Printer or other output device (to a printer or other output device)
These are just some columns, and there are many other less-known DC (such as menus), but the above list is the DC that users are very interested in.
It is more complicated to process DC at the API level. First, you must go from windows to the DC handler. Then select various objects to enter the DC (Pen, brush, font), and then draw a picture in the DC. After drawing, make sure that the objects that enter the DC are cleared before deleting the DC. If you forget to clear various objects in the DC, the application will cause memory overflow.
Fortunately, VCL provides a TCanvas class for easy DC processing. For example, the following program uses Windows APIs to draw circles on the screen. The incircle is red and the border is blue.
procedure TForm1.Button1Click(Sender: TObject);var DC: HDC; Brush, OldBrush: HBRUSH; Pen, OldPen: HPEN;begin DC := GetDC(Handle); Brush := CreateSolidBrush(RGB(255, 0, 0)); Pen := CreatePen(PS_SOLID, 1, RGB(0, 0, 255)); OldBrush := SelectObject(DC, Brush); OldPen := SelectObject(DC, Pen); Ellipse(DC, 20, 20, 120, 120); SelectObject(DC, OldBrush); SelectObject(dc, OldPen); ReleaseDC(Handle, DC);end;
The above code is not too bad, but after processing the DC, it is easy to forget to restore the object. If it is forgotten, its application will leak resources. The following code is equivalent to the above Code, but written in VCL:
procedure TForm1.Button2Click(Sender: TObject);begin Canvas.Brush.Color := clRed; Canvas.Pen.Color := clBlue; Canvas.Ellipse(20, 20, 120, 120);end;
This code is not only short and easy to read, but also robust. TCanvas is responsible for releasing resources as needed, so there is no need to worry about resource issues. TCanvas is a simpler and more effective method than using APIs.
The TCanvas inheritance relationships are as follows:
The TCanvas class has many attributes and methods. Some attributes and methods will be used in subsequent explanations. The following table lists the main attributes and methods of TCanvas.
Table 1 main attributes of TCanvas
| Attribute |
Description |
| Brush |
Paint Brush color or pattern for plotting. (Determines the color and pattern for filling graphical shapes and backgrounds ..) Delphi Syntax:Property brush: tbrush; |
| Cliprect |
The current cut rectangle of the canvas. Any picture is limited to this rectangle. This attribute is read-only. (Specifies the boundaries of the clipping rectangle .) Delphi Syntax:Property cliprect: trect; |
| Copymode |
Determines how a picture is displayed (positive or reverse) (specifies how a graphical image is copied onto the canvas .) Delphi Syntax:Property copymode: tcopymode; |
| Font |
Specifies the font to use when writing text on the image .) Delphi Syntax:Property Font: tfont; |
| Handle |
The canvas handler used for accessing windows APIs directly. (Specifies the handle for this canvas .) Delphi Syntax:Property handle: HDC; |
| Pen |
Determines the type and color of the lines drawn on the canvas. (Specifies the kind of pen the canvas uses for drawing lines and outlining shapes .) Delphi Syntax:Property pen: tpen; |
| Penpos |
The current position of the image in the X and Y coordinate systems. (Specifies the current drawing position of the pen .) Delphi Syntax:Property penpos: tpoint; |
| Pixels |
Pixel of the array canvas (specifies the color of the pixels within the current cliprect .) Delphi Syntax:Property pixels [X, Y: integer]: tcolor; |
Table 2 main TCanvas Methods
| Method |
Description |
| Arc |
Draw an arc on the canvas using the current pen. Draws an arc on the image along the perimeter of the ellipse bounded by the specified rectangle. Delphi Syntax:Procedure arc (x1, Y1, X2, Y2, X3, Y3, X4, Y4: integer ); |
| Brushcopy |
Display a bitmap with a transparent background. Copies a portion of a bitmap onto a rectangle on the canvas, replacing one of the colors of the bitmap with the brush of the canvas. Delphi Syntax:Procedure brushcopy (const DEST: trect; bitmap: tbitmap; const Source: trect; color: tcolor ); |
| Copyrect |
Copy a part of the image to the canvas. Copies part of an image from another canvas into the canvas. Delphi Syntax:Procedure copyrect (const DEST: trect; canvas: TCanvas; const Source: trect ); |
| Draw |
Copy the image from the memory to the canvas. Renders the graphic specified by the graphic parameter on the canvas at the location given by the coordinates (x, y ). Delphi Syntax:Procedure draw (X, Y: integer; graphic: tgraphic ); |
| Ellipse |
Use the current paint brush to draw an ellipse and fill it with the current brush on the canvas. Draws the ellipse defined by a bounding rectangle on the canvas. Delphi Syntax: Procedure ellipse (x1, Y1, X2, Y2: integer); overload; Procedure ellipse (const rect: trect); overload; |
| Floodfill |
Fill an area of the canvas with the current brush. Fills an area of the canvas using the current brush. Delphi Syntax:Procedure floodfill (X, Y: integer; color: tcolor; fillstyle: tfillstyle ); |
| Lineto |
Draw a line from the current image location to the position specified by the X and Y positions parameter. Draws a line on the canvas from penpos to the point specified by X and Y, and sets the pen position to (x, y ). Delphi Syntax:Procedure lineto (X, Y: integer ); |
| MoveTo |
Set the current image position. Changes the current drawing position to the point (x, y ). Delphi Syntax:Procedure moveTo (X, Y: integer ); |
| Pie |
Draw a pie chart on the canvas. Draws a pie-shaped the section of the ellipse bounded by the rectangle (x1, Y1) and (X2, Y2) on the canvas. Delphi Syntax:Procedure pie (x1, Y1, X2, Y2, X3, Y3, X4, Y4: integer ); |
| Polygon |
Draw a diamond on the canvas based on a set of points and fill it with the current brush. Draws a series of lines on the canvas connecting the points passed in and closing the shape by drawing a line from the last point to the first point. Delphi Syntax:Procedure polygon (points: array of tpoint ); |
| Polyline |
Use the current pen to draw lines based on a group of points on the canvas. These points are not automatically closed. Draws a series of lines on the canvas with the current pen, connecting each of the points passed to it in points. Delphi Syntax:Procedure polyline (points: array of tpoint ); |
| Rectangle |
Draw a rectangle on the canvas, use the current pen around it, and fill the rectangle with the current brush. Draws a rectangle on the canvas. Delphi Syntax: Procedure rectangle (x1, Y1, X2, Y2: integer); overload; Procedure rectangle (const rect: trect); overload; |
| Roundrect |
Rounded rectangle. Draws a rectangle with rounded corners on the canvas. Delphi Syntax:Procedure roundrect (x1, Y1, X2, Y2, X3, Y3: integer ); |
| Stretchdraw |
Copy the bitmap from the memory to the canvas. The bitmap is expanded or reduced according to the size of the target rectangle. Draws the graphic specified by the graphic parameter in the rectangle specified by the rect parameter. Delphi Syntax:Procedure stretchdraw (const rect: trect; graphic: tgraphic ); |
| Textextent |
Returns the width and height of the input string pixel in the text parameter. The width is calculated using the current canvas font. Returns the width and height, in pixels, of a string rendered in the current font. Delphi Syntax:Function textextent (const text: string): tsize; |
| Textheight |
Returns the height of the input string pixel in the text parameter. Its width is calculated using the current canvas font. Returns the height, in pixels, of a string rendered in the current font. Delphi Syntax:Function textheight (const text: string): integer; |
| Textout |
Use the current font to draw text at a specified position on the canvas. Writes a string on the canvas, starting at the point (x, y), and then updates the penpos to the end of the string. Delphi Syntax:Procedure textout (X, Y: integer; const text: string ); |
| Textrect |
Draw text in a rectangular box. Writes a string inside a clipping rectangle. Delphi Syntax:Procedure textrect (rect: trect; X, Y: integer; const text: string ); |
Believe it or not, these properties and methods only reflect a small part of the features that Windows devices describe the environment. Fortunately, these attributes and Methods cover 80% of the work that needs to be done when processing images. Then, before discussing the TCanvas class in detail, we need to discuss the graphic objects used in Windows program design.