Drawing text of Delphi GDI object

Source: Internet
Author: User
Tags drawtext textout
Basic Drawing operations (Basic Drawing operations)

Now we know that the rectangle method is used to draw squares and rectangles, the ellipse method is used to draw circles and rectangles, And the moveTo and lineto methods are used to draw lines.

The arc method is also used to draw an arc. The pie method is used to draw a pie chart. Everything is very basic. There is no need to learn more about these methods of TCanvas. Next we will start more interesting graphic operations, these graphic operations may be encountered when compiling Delphi applications.

 

Draw text)

It doesn't sound too difficult to draw text, does it? In fact, there are some small mistakes that are easy to make. If you do not pay attention to, it may make it very difficult to draw the text. In addition, there are several good text rendering features that should be understood.

1. The textout and textrect methods (the textout and textrect methods)

The textout method is the most basic method for drawing text on the canvas. There is not much about textout. You only need to enter the position X, Y, and the text to be displayed-for example:

Canvas. textout (20, 20, 'simplest text output ');

The Code displays the specified string at 20 or 20 points on the form.

X and Y coordinates refer to the upper left corner of the text to be drawn rather than the bottom line. To illustrate this meaning, experiment with this Code:

  Canvas.TextOut(20, 20, 'This is a text');  Canvas.MoveTo(20, 20);  Canvas.LineTo(100, 20);

This code () displays some text and draws lines from this position. For example, the execution result of this Code is displayed. Pay attention to the Line Painting in the upper part of the text.

When you want to display text, using textout does not require many precise positioning.

The textrect method requires that you specify the text to be displayed and the cut rectangle. This method is used when the text needs to be restricted within a certain domain. The text that falls out of the line is cut off. The following Code ensures that the text cannot exceed 100 pixels.

Canvas. textrect (rect (20, 50,120, 70), 20, 50, 'This is a very long line of text and may be dropped ');

As shown below, the string is dropped because the length exceeds 100 pixels.

Both textout and textrect can only draw a single line of text, but cannot wrap text.

Tip

To draw text with tab stops, see the Windows API function tabbedtextout.

Output text with tabs. For more information, see the Windows API function tabbedtextout.

2. Text backgrounds)

It is very easy to change the text background color, so you can do this by using the following code:

Canvas.Brush.Color := clWhite;

The display effect is as follows:

When you set the background color, you should develop this habit, that is, store the previous brush type, and restore the text to the original type after processing. The Code is as follows:

VaR oldstyle: tbrushstyle; oldcolor: tcolor; begin clearcanvas; oldstyle: = canvas. brush. style; oldcolor: = canvas. brush. color; canvas. brush. color: = clred; canvas. textout (20, 20, 'current text background color is red'); canvas. brush. style: = oldstyle; canvas. brush. color: = oldcolor; canvas. textout (20, 50, 'Restore original style' again now); end;

The display effect is as follows:

To use a text transparent background, set the brush type to bsclear. The following code:

  Canvas.Brush.Style := bsClear;

There are other advantages of using a transparent background, such as displaying some text on the image background. In this case, it is ideal to use a transparent background to output text. The Code is as follows:

VaR oldstyle: tbrushstyle; bitmap: tbitmap; begin bitmap: = tbitmap. create; bitmap.loadfromfile('handshak.bmp '); canvas. draw (0, 0, bitmap); canvas. font. name: = 'arial bold '; canvas. font. size: = 13; oldstyle: = canvas. brush. style; canvas. brush. style: = bsclear; canvas. textout (20, 5, 'transparent background text'); canvas. brush. style: = oldstyle; canvas. textout (20, 30, 'non-transparent background text'); bitmap. free; end;

This Code draws a bitmap on the form, and then draws the text with a transparent background on the form bitmap. The effect is as follows:

3. The drawtext Function)

The drawtext function of Windows API provides greater control over the text on the canvas than textout. For some reason, the TCanvas class does not have the drawtext method. Using drawtext means that the API is used directly. First, let's take a look at a basic drawtext example, and then let's talk about the more use of this function.

var  R: TRect;begin  R := Rect(20, 20, 220, 80);  Canvas.Rectangle(20, 20, 220, 80);  DrawText(Canvas.Handle, 'An example of DrawText.', -1, R,    DT_SINGLELINE or DT_VCENTER or DT_CENTER);end;

The code result and the results of the following examples are displayed.

First, the trect record is initialized using the Windows API rect function. Then, a regular rectangle is drawn on the canvas (the rectangle is drawn on the canvas so that you can imagine the size of the rectangle to be drawn ). Finally, call the drawtext function to draw text.

// The drawtext function declaration is as follows: drawtext (HDC: HDC; {Device handle} lpstring: pchar; {text} ncount: integer; {number of characters to be drawn; -1 indicates all} var lprect: trect; {rectangle structure} uformat: uint {Option}): integer; {return text height}

The following describes the parameters of a function as follows:

  • The first parameter is used to specify the device description environment. The handle attribute of TCanvas is the HDC of the canvas. Therefore, enter it as the first parameter.
  • The second parameter is the string to be displayed.
  • The third parameter is used to specify the number of characters to be drawn. If the parameter is-1, all characters in the string are drawn.
  • The fourth parameter is Var trect, which is the VaR parameter because the drawtext operation modifies the rectangle.
  • The last parameter specifies the flag used for text painting. In this example, dt_singleline (single line text), dt_vcenter (vertical center), and dt_center (horizontal center) are used. Drawtext has nearly 20 labels to specify. Every flag is not intended here. For all the logos, see the Win32 API help.

The preceding example illustrates the most common use of the drawtext function, which center the text horizontally and vertically. This feature is useful when making components. In particular, self-made lists, Combo boxes, and menus often need to center text. We may not immediately realize the benefits of this function. However, if you start to make self-made components or write your own graphics components, you will realize this.

Another interesting sign of drawtext is the dt_end_ellipsis flag. If the text is too long to be included in the specified rectangle, Windows intercepts some strings and adds a ellipsis at the end to indicate that the strings are intercepted. For example, the following code:

var  R: TRect;begin  R := Rect(20, 20, 120, 70);  DrawText(Canvas.Handle, 'This text is too long to fit', -1, R, DT_END_ELLIPSIS);end;

After this execution, the result text is displayed as follows:

If the text in the rectangle can be too long, you can use this sign.

Dt_calcrect is another very useful sign used to calculate the height of the rectangle needed to hold the specified text. When this flag is used, Windows calculates the required height and returns the height without drawing the text. The user tells windows how wide the rectangle should be, and Windows will tell the user how high the rectangle is required for the text. In fact, windows also modifies the bottom and left values of the rectangle. This is especially important when drawing multiple lines of text.

The following program example shows how high a rectangle is required for Windows to contain all text. Then, draw a rectangle on the screen, and draw the last text in the rectangle. The Code is as follows:

var  R: TRect;  S: string;begin  R := Rect(20, 20, 150, 30);  S := 'This is a very long string which will' +    'run into multiple lines of text.';  DrawText(Canvas.Handle, PChar(S), -1, R, DT_CALCRECT or DT_WORDBREAK);  Canvas.Brush.Style := bsSolid;  Canvas.Rectangle(R);  Canvas.Brush.Style := bsClear;  DrawText(Canvas.Handle, PChar(S), -1, R, DT_WORDBREAK);end;

Finally, run the code. The effect is as follows:

Note that the second parameter of drawtext must be converted from the string type to the pchar type because the parameter required by drawtext is a pointer to the character array rather than the string type.

Execute the preceding code multiple times and modify the length of the displayed string each time. No matter how many characters are added, the rectangular box can always accurately enclose the text.

Note

If you write a Delphi program adapted to various versions, you cannot forcibly convert the string type to the pchar type as above. Because delphi1 cannot be compiled, you must use the strpcopy function. The Code is as follows:

Procedure tform1.btn10click (Sender: tobject); var R: trect; s: string; temp: array [0 .. 100] of char; {at least length (s) + 1 space size} Begin R: = rect (20, 20,150, 30); s: = 'this is a very long string which will '+ 'run into multiple lines of text. '; {use the strpcopy function to convert string type to pchar type} drawtext (canvas. handle, strpcopy (temp, S),-1, R, dt_calcrect or dt_wordbreak); canvas. brush. style: = bssolid; canvas. rectangle (r); canvas. brush. style: = bsclear; drawtext (canvas. handle, strpcopy (temp, S),-1, R, dt_wordbreak); end;

If you do not consider earlier versions of Delphi, you do not need to use the strpcopy function.

 

Note

Drawing text with drawtext is a little slower than using textout. If the Drawing operation is sensitive to speed, use textout instead of drawtext. Users must do more work on their own, but the execution speed may be faster. Of course, for most text painting, do not pay attention to the difference between textout and drawtext.

Specifically, I wrote a program for efficiency comparison, which is determined by calculating the time required to execute the same job. For more information about the code, download the sample code. For example:

 

The drawtext function is a very useful and powerful function. When writing your own components, there is no doubt that such functions will be frequently used.

The above code passes the test in Delphi7. Download the sample code: gdi .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.