Pen, brush, and font of Delphi GDI objects

Source: Internet
Author: User

Windows Graphics Device Interface (GDI) has many objects that define a device's ability to describe the environment. The most common GDI objects are pens, brushes, and fonts. Other GDI objects include the color palette, bitmap, and region. First, understand the pen, brush, and font, and then turn to more complex objects.

Pen pens, brush brushes, and Fonts

Pens, brushes, and fonts are concise and easy to understand. First, we will introduce these objects and how to use them in the TCanvas class.

Pens

A pen is defined as an object used to draw a line. A line can be a simple straight line from one point to the next, or it can be a boundary drawn around a rectangle, an ellipse, or a diamond. The pen can be accessed through the pen attribute of the TCanvas class and is an instance of the tpen class.

The tpen class inheritance relationship is as follows:

The following table lists the attributes of tpen.

Table 1. tpen attributes
Attribute Description
Color Set the line color.
Handle Handle, used to access GDI.
Mode Specifies the drop (raster operation grating operation) mode of the canvas (forward, reverse, etc ).
Style Pen type. line of sight, dotted line, dot line, double dot line, dot line, inner frame, and solid line are not allowed.
Width Pen width (in pixels) of the paint brush)

Most of these attributes can be used by users. The following example shows a red dotted line.

Canvas. pen. color: = clred; {the paint brush is red} canvas. pen. style: = psdash; {the paint brush style is dotted;} canvas. moveTo (20, 20); canvas. lineto (120, 20 );

To check the code, place a button on the form and input the program into the onclick event of the button. Run the program and click the button. The line is drawn on the form, for example:

Note

All simple examples described later can be tested using the above method. However, if another application is used to overwrite the application and then return to the application, the pictures will disappear because the pictures are temporary. If you want to save a picture for a long time, place the picture code in the onpaint event of the form. Once the Windows window needs to be re-painted, the onpaint event is generated and the image code is executed.

Dotted and dashed only applies to brushes with a width of 1. The psclear paint brush style is used to clear the lines (such as rectangles, ovles, and filled diamond) that Windows GDI draws outside the object ).

Tip

By placing the shape component on the form, you can test various attributes of the tpen and modify the Pen attributes of the graphic. These are particularly useful for figuring out the role of the mode attribute of the tpen class.

Below we have compiled a simple test button (see the sample code for details) to simulate the effects of various paint styles, such:

 

Brush brushes

The canvas brush is used to fill areas and graphics on the canvas. Unlike tpen, a paint brush is used to draw a line on the canvas, while a paint brush fills the canvas area with different colors, styles, and shapes.

The TCanvas class has a brush attribute, which can be used to control various features of the brush. The brush attribute is an instance of the tbrush class. The inheritance relationship is as follows:

Table 2. tbrush attributes
Attribute Description
Bitmap Specifies a bitmap for the background of the brush. The bitmap cannot exceed 8x8 pixels.
Color Paint Brush color
Handle Handle for direct access to GDI.
Style Brush style, including 8 styles.

By default, the style attribute is set to bssolid, the color is clwhite, and there is no bitmap. To fill the pattern, set the style attribute to bshorizontal, bsvertical, bsfdiagonal, bsbdiagonal, bscross or bsdiagcross. The Effects of Various Style styles are as follows: (For details, refer to the sample code)

The following example draws a circle on the form using a 45-degree pattern. The Code is as follows:

  Canvas.Brush.Color := clBlue;  Canvas.Brush.Style := bsDiagCross;  Canvas.Ellipse(20, 20, 220, 220);

It is the result displayed after the above Code is run:

When a pattern brush is used, the color attribute of the brush is defined as the line color of the pattern. For some reason, when a pattern is filled, VCL automatically sets the background pattern to transparent, this means that the background color of the brush is the same as that of the Windows background where the image is located. (For example, the circle background color is the same as the form color.) If you specify a color, you need to avoid VCL and use the API. If you need to use a blue pattern on a white background, the Code is as follows:

  Canvas.Brush.Color := clBlue;  Canvas.Brush.Style := bsDiagCross;  SetBkMode(Canvas.Handle, OPAQUE);  SetBkColor(Canvas.Handle, clWhite);  Canvas.Ellipse(20, 20, 220, 220);

Now the background color of the brush is white, such as the circle displayed.

 

Another interesting feature of the brush is the selection of the bitmap background. First, let's look at the program code and then discuss the bitmap brush. The Code is as follows:

  Canvas.Brush.Bitmap := TBitmap.Create;  Canvas.Brush.Bitmap.LoadFromFile('bkgnd.bmp');  Canvas.Ellipse(20, 20, 220, 220);  Canvas.Brush.Bitmap.Free;

In this Code, the first line creates a tbitmap object and assigns it to the bitmap attribute of the brush. The missing bitmap attribute is not assigned a value. Therefore, you must create a tbitmap object and assign it to the bitmap attribute.

The second line is to load the bitmap from the file. The bitmap cannot exceed 8x8 pixels. A large bitmap can be used, but it will be reduced to 8x8. (Bitmap larger than 8x8 pixels can be used in win7 testing ).

The third row draws an ellipse. After the elliptical image is drawn, the brush attribute is deleted. VCL does not delete the brush attribute. Therefore, you must manually delete the attribute; otherwise, the memory will overflow.

Displays the circle drawn by the bitmap brush.

 

Sometimes a hollow brush is needed. To create a hollow brush, you only need to set the style attribute to bsclear, use a hollow brush to add another circle to the first circle. The Code is as follows:

  Canvas.Brush.Bitmap := TBitmap.Create;  Canvas.Brush.Bitmap.LoadFromFile('bkgnd.bmp');  Canvas.Ellipse(20, 20, 220, 220);  Canvas.Brush.Style := bsClear;  Canvas.Pen.Width := 5;  Canvas.Ellipse(70, 70, 170, 170);  Canvas.Brush.Bitmap.Free;

The display effect is as follows:

If you directly access the API and use a brush to do other things, of course, the VCL tbrush class can handle this kind of work in most cases.

 

Fonts)

Font is nothing new for everyone. It is always used throughout the learning process. The font used by the TCanvas class is the same as that used by the form or other components. To change the font of the canvas, enter the following code.

  Canvas.Font.Name := 'Courier New';  Canvas.Font.Size := 20;  Canvas.Font.Style := Canvas.Font.Style + [fsBold];  Canvas.TextOut(20, 20, 'Testing');

The display effect is as follows:

In the subsequent sections, we will also detail how to process fonts.

The above code is successfully tested in Delphi7. Download the sample code in this tutorial:Gdiobjects' pen. rar

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.