C language graphics programming (IV)-graphic text

Source: Internet
Author: User

I. Graphic Text Functions
85. The string function is displayed at the current position of outtext ().
86. outtextxy () shows the string function at (x, y ).
87. sprintf () format the output function
Ii. Graphic text settings
88. settextstyle () sets the text type Function
89. settextjustify () sets the text sorting Function
90. setusercharsize () sets the user character ratio function
91. gettextsettings () gets the text setting function
92. textheight () text height function
93. textwidth () text Width Function

If there is no text, the image will not come in handy. However, once the graphic mode is set, regular text cannot be displayed. Labels and text information can only be displayed in graphic text. The graphic text display is different from the conventional text display. For example, the row and column coordinates of a regular character screen are no longer applicable, and a single character can be displayed anywhere on the screen. In addition, the graphic text display can be either horizontal or vertical, the letter size can also be changed, and several different fonts can be provided. In short, regular text display is simple and easy; graphic text display is complex and not easy to operate. Therefore, the C language provides several functions to simplify operations and control graphic text display.

I. Graphic Text Functions
In graphic mode, strings are displayed on the screen. You can use outtext (), outtextxy (), and sprintf ().

85. The string function is displayed at the current position of outtext ().
Function: The outtext () function displays a string at the current position in graphic mode using the current text settings (font, character size, text display direction, and text arrangement.
Usage: This function is called by void outtext (char * string );
Note: The string parameter points to the string to be displayed.
You can call this function to set the current drawing color in advance, and select the font, character size, text display direction, and horizontal and vertical text sorting mode.
If the current display direction is horizontal, the current cursor position CP will move the length of the displayed characters; otherwise, the CP will remain unchanged; in graphic mode, the cursor is invisible, but even the invisible cursor, on the screen, the cursor position CP still exists.
The header file corresponding to this function is graphics. h.
For example, if you use the outtext () function, see the example of the 86. outtextxy () function.

86. outtextxy () shows the string function at (x, y ).
Function: The outtextxy () function displays a string at the coordinate pixel (x, y) in the graphic mode.
Usage: The call method of this function is void outtextxy (int x, int y, char * string );
Description: The Screen position of the string given by the parameter (x, y). The string points to the string. Calling this function does not affect the current cursor position. Of course, the cursor is invisible.
This function also uses text sorting in the current drawing color, Font, character size, text display direction, and horizontal and vertical directions.
The header file of outtextxy () is graphics. h.
Return Value: None
For example, the following program calls the outtext () and outtextxy () functions:
# Include <graphics. h>
Void main ()
{
Int driver, mode;
Int I;
Driver = detect;
Mode = 0;
Initgraph (& driver, & mode ,"");
Outtext ("this is an example ");
Outtext ("another line ");
For (I = 100; I <200; I + = 8)
Outtextxy (200, I, "hello ");
Getch ();
Restorecrtmode ();
}

87. sprintf () format the output function
Function: sprintf () is used for formatting output.
Usage: This function is called in int sprintf (char * string, char * format, arg_list );
Note: The usage of the sprintf () function is the same as that of the printf () function, except that the sprintf () function provides the first parameter string (generally a character array), and then calls outtextxy () the function displays the characters in the string on the screen. Arg_list is a parameter table, which can be an indefinite number. When outputting a number in plotting mode, you can call the sprintf () function to send the format to the first parameter and then display the output.
If there is no text, the image will not come in handy. However, once the graphic mode is set, regular text cannot be displayed. Labels and civilian information can only be displayed in graphic text. The graphic text display is different from the conventional text display. For example, the row and column coordinates of a regular character screen are no longer applicable, and a single character can be displayed anywhere on the screen. In addition, the graphic text display can be either horizontal or vertical, the letter size can also be changed, and several different fonts can be provided. In short, regular text display is simple and easy; graphic text display is complex and not easy to operate. Therefore, the C language provides several functions to simplify operations and control graphic text display.

Ii. Graphic text settings
Regular text mode display is equivalent to typing on paper, while graphic text mode display is closer to typographical printing. The key to this enhancement is to change the font size and character size. Select different horizontal positions for text sorting, and even display text in the vertical direction rather than the horizontal direction. These functions must be implemented by calling the text setting function.

88. settextstyle () sets the text type Function
Function: settextstyle () is used to set the current font, orientation (horizontal or vertical), and character size of the graphic text.
Usage: Call void settextstyle (INT font, int direction, int charsize );
Note: In function brackets, font is the text font parameter, direction is the text display direction, and charsize is the character size parameter. The headers are defined in graphics. Standard fonts listed in Table 1-14.

Table 1-14 standard font of graphic text
--------------------------------------------------------------------
Font, symbol name, and other values
--------------------------------------------------------------------
Default_font 0 8*8 lattice fonts (default font)
Triplex_font 1 three-fold stroke font (triple font)
Small_font 2 stroke font (small font)
Sans_serif_font 3 stroke font (sanserif font)
Gothic_font 4 strokes in bold (gothic_font)
--------------------------------------------------------------------

Before setting the font, The. CHR file of the selected font must be in the driverpath directory or subdirectory specified in initgraph.
The text display direction of the image is horizontal, but the text display direction of the image can be set to vertical (clockwise to 90 degrees ). Table 1-15 shows the text display directions of the two images.

Table 1-15 graphic text display direction
------------------------------------------------------------
Display direction symbol name and other value description
------------------------------------------------------------
Horiz_dir 0 is displayed from left to right (horizontal)
Vert_dir 1 is displayed from the bottom up (vertical)
------------------------------------------------------------

In vertical display, text strings are displayed from the bottom up. Currently, there are no pre-defined display rules from top to bottom or left to right, but can be designed as needed.
For dot matrix fonts, the character size can be between 0 and 10. For stroke font, charsize (character size parameter) = 0 indicates that the font is displayed in a default ratio of 4 times or the User-Defined character ratio is enlarged by the setusercharsize () function. The maximum valid charsize value is 0.
If an incorrect parameter value is passed to the settextstyle () function, the graphresult () function returns the value-11 (indicating a general image error), and the current text type remains unchanged.
The header file of the settextstyle () function is graphics. h.
Return Value: None
For example, the following program uses the settextstyle () function to display different text fonts and sizes:
# Include <graphics. h>
# Include <conio. h>
Void main ()
{
Int driver, mode;
Mode = 0;
Initgraph (& driver, & mode ,"");
Outtext ("normal ");
Settextstyle (gothic_font, horiz_dir, 2 );
Outtext ("Gothic ");
Settextstyle (triplex_font, horiz_dir, 2 );
Outtext ("triplex ");
Settextstyle (sans_serif_font, horiz_dir, 7 );
Outtext ("sans serif ");
Getch ();
Restorecrtmode ();
}

89. settextjustify () sets the text sorting Function
Function: settextjustify () is used to set the text arrangement in the horizontal and vertical directions.
Usage: This function is called by void settextjustify (INT horiz, int vert );
Note: The horiz and vert parameters are used to determine the horizontal and vertical text sorting mode. The value is shown in Table 1-16.

Table 1-16 graphical text Arrangement
---------------------------------------------------------------------------
Horizontal value, vertical value, and other values
Symbol name
---------------------------------------------------------------------------
Left_text 0 bottom_text 0
Center_text 1 center_text 1
Right_text 2 top_text 2
---------------------------------------------------------------------------

For horizontal sorting, if left_text is selected, the current position CP alignment is not dared on the left of the text, and the text string is displayed to the right. If center_text is used, the center of the text string is aligned with the current position CP. If right_text is used, the right of the text string is aligned with the current position CP and the text string is displayed to the left. The vertical line of bottom_text indicates that the bottom line of the text string is aligned with the current position CP. Center_text is the center line of the text string and the top line of the text string is CP. The default value is left_text in the horizontal direction and top_text in the vertical direction.
The header file corresponding to the settextjustify () function is graphics. h.
For example, the following program statement places the graphic text string to the right, that is, the right side of the string is arranged immediately next to the current position, and its top line is over CP:
Settextjustify (right_text, top_text );

90. setusercharsize () sets the user character ratio function
Function: The setusercharsize () function allows you to customize the font ratio of a stroke. However, this function does not apply to characters in the default font because the default font is a hardware-defined 8x8 lattice font.
Usage: This function is called by void setusercharsize (INT xmul, int xdiv, int ymul, int ydiv );
Note: The xmul/xdiv parameter sets the character width ratio. The ymul/ydiv parameter sets the character height ratio. After this function is called, the character width displayed on the screen is the default value of 8 pixels multiplied by the width ratio, and the height is the default value of 8 pixels multiplied by the height ratio. Note that the character adjustment parameter is valid only when the charsize parameter of settextstyle () is set to 0. To display characters with a height ratio of 3 and a width of 2 times higher, call the setuserchrsize () function using the following method ():
Xmul = 6; xdiv = 1;
Ymul = 3; ydiv = 1;
Setusercharsize (xmul, xdiv, ymul, ydiv );
The characters are 12 pixels wide and 48 pixels High.
The header file corresponding to the setusercharsize () function is graphics. h.
Return Value: None
For example, display the large character text:
# Include <graphics. h>
Void main ()
{
Int driver, mode;
Driver = detect;
Mode = 0;
Initgraph (& driver, & mode ,"");
Settextstyle (triplex_font, horiz_dir, 0 );
Setusercharsize (5, 1, 5, 1 );
Outtext ("big ");
Getch ();
Restorecrtmode ();
}

91. gettextsettings () gets the text setting function
Function: The gettextsettings () function fills in the textsettingstype structure with the set values of the current font, display direction, character size, and horizontal vertical direction text to obtain text information.
Usage: The call method of the gettextsettings () function is void gettextsettings (struct textsettingstype * info );
Note: The textsettingdtype structure in the header file graphics. H is defined as follows:
Strcut textsettingstype {
Int font; // font
Int direction; // horizontal or vertical direction
Int charsize; // character size
Int horiz; // horizontal sorting method
Int Vert; // vertical sorting method
};
The value of the font in the structure is one of the following values:

Font of Value
-------------------------------
0 default font
1 X Font
2 small fonts
3 sansserif font
4 Gothic
-------------------------------

The value of charsize is the coefficient used to determine the output character size. It is selected from 0 to 10.
The direction variable can be set to horiz_dir (default value) or vert_dir vertical. Horiz and vert are text-sorted variables with the following values:

Symbol Name Description
---------------------------------------------------------------
Left_text 0 CP (current position on the left)
Center_text 1 CP in the center
Right_text 2 CP on the right
Bottom_text 3 CP at the bottom
Top_text 4 cp on top
---------------------------------------------------------------

For more information, see the description section of the settextjustify () function.
The header file corresponding to this function is graphics. h.
Return Value: return the current text setting information and load it into the structure pointed to by info. Then, you can obtain the text setting values from this structure.
For example, read the set value of the current text.
Struct textsettingstype ts;
Gettextsettings (& TS );

92. textheight () text height function
Function: The textheight () function returns the string height.
Usage: The function call method is int textheight (char * string );
Note: The string parameter points to the string. The textheight () function calculates the number of bytes at the string height based on the current font, character size, proportional factor, and text direction. This can be the height of a single character or the height of the entire string.
The header file corresponding to this function is graphics. h.
Return Value: returns the number of bytes of the string height.
For example, the height of a numeric character is displayed:
# Include <graphics. h>
Void main ()
{
Int driver, mode;
Driver = 0;
Mode = 0;
Initgraph (& driver, & mode ,"");
Printf (Height: % d ", textheight (" 8 "));
Getch ();
Restorecrtmode ();
}

93. textwidth () text Width Function
Function: The textwidth () function returns the string width.
Usage: The function is called in int textwidth (char * string );
Description: The string parameter points to a string. The textwidth () function calculates the number of characters in the string width based on the current font, character size, proportional factor, and text direction. This can be the width of a single character, the width of the entire string, and usually the width of the entire string.
The header file corresponding to this function is graphics. h.
Return Value: returns the number of characters in the string width.
For example, display the width of the string Hello:
# Include <graphics. h>
Void main ()
{
Int driver, mode;
Driver = detect;
Mode = 0;
Initgraph (& driver, & mode ,"");
Printf ("width: % d", textwidth ("hello "));
Getch ();
Restorecrtmode ();
}

 

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.