C language Graphic programming [1]: character screen

Source: Internet
Author: User

I. screen operation functions
1. clrscr () clears the character Window Function
2. window () character Window Function
3. gotoxy () cursor locating Function
4. clreol () clears the character function at the end of the cursor line
5. insline () inserts an empty row Function
6. delline () deletes a row of functions.
7. gettext () is copied to the text function.
8. puttext (): Specifies the function for copying text.
9. movetext () mobile text function
Ii. Character attribute functions
10. textmode () text mode function
11. highvideo () High Brightness Function
12. lowvideo () low brightness Function
13. normvideo (void );
14. textcolor () text color Function
15. textattr () text attribute function
16. textbackground () text background Function
3. Screen Display Function
17. wherex () x coordinate function at the cursor
18. wherey () y coordinate function at the cursor
19. gettextinfo () function for obtaining text window information

Borland C ++ provides character screens and graphic functions. The core of the character screen is the window, which is the activity part of the screen. The character output or display is carried out in the activity window. When the window is missing, it is the whole screen. You can specify the window size as needed. Similarly, operations on graphical functions are also provided (viewport ). That is to say, all operations on graphical functions are performed on the viewport. The graphical and character windows have the same features. You can define different sizes of the visual and character windows on the screen. If you do not define the size of the view, it is the whole screen.
A window is a concept on the character screen. Only characters can be displayed in the window. The minimum unit that users can access is one character. The view is a concept in the graphics screen state. Both text and graphics can be displayed in the view, the smallest unit that users can access is a pixel (the term pixel was originally used to indicate the smallest, individual luminous point unit on the display. However, the concept is extended to the minimum accessible point on the graphic display ).
In character and graph shapes, the positions on the screen are determined by their rows and columns. Note that the coordinates in the upper left corner of the character state are (), but those in the upper left corner are ).
It is important to understand the relationship between the font screen and graphical functions and the window and the view. For example, the character screen cursor position function gotoxy () moves the cursor to the X and Y positions of the window, which is not necessarily relative to the entire screen. The following describes the functions, operations, and sequence of common types of character screen functions.

I. screen operation functions
Character screen operations are often used to write program plotting. For example, you must clear the screen before writing characters to the screen. Another example is that sometimes you need to write a string of flesh-like characters in multiple places on the screen. In this case, you 'd better use Screen Copy to efficiently complete this task. Borland C ++ provides a series of character screen operation functions for these operations.

1. clrscr () clears the character Window Function
Function: The clrscr () function clears the current character window and marks the light at (1, 1) in the upper left corner.
Usage: This function is called by void clrscr (void );
Note: void in brackets indicates no parameter.
The header file of the function is conio. h.
Return Value: None
For example, see the example of clreol () function 4.

2. window () character Window Function
Function: function window () is used to create a character window at a specified position.
Usage: This function is called by void window (INT left, int top, int right, int bottom );
Note: The left and TOP Parameters in the function are the coordinates in the upper left corner of the window, and right and bottom are the coordinates in the lower right corner of the window.
If a coordinate is invalid, the window () function does not work. Once this function is successfully called, all positioning coordinates are relative to the window, not to the entire screen. However, the coordinates used to create a window are always relative to the absolute coordinates of the screen, rather than relative coordinates of the current window. In this way, you can create multiple non-nested windows as needed.
The header file of this function is conio. h.
Return Value: None
Example: for the implementation of calling this function, see Example 3. gotoxy () function.

3. gotoxy () cursor locating Function
Function: moves the cursor on the word screen to the position specified in the current window.
Usage: This function is called by void gotoxy (int x, int y );
Note: x and y in parentheses are coordinates for cursor positioning. If one of the coordinates is invalid (for example, the cursor is not moved.
This function is often used in character states (sometimes called text states). Its header file is conio. h.
Return Value: None
For example, the following program creates two windows and then displays characters in the window. The character position is determined by calling this function.
# Include "conio. h>
Void border (INT startx, int starty, int endx, int Endy)
{
Register int I;
Gotoxy (1, 1 );
For (I = 0; I <= endx-startx; I ++)
Putch ('-');
Gotoxy (1, Endy-starty );
For (I = 0; I <= endx-startx; I ++)
Putch ('-');
For (I = 2; I <= Endy-starty; I ++ ){
Gotoxy (1, I );
Putch ('1 ');
Gotoxy (endx-startx + 1, I );
Putch ('1 ');
}
}

Main ()
{
Void border (INT, Int, Int, INT );
Clrscr ();
Window (6, 8, 38, 12 );
Border (6, 8, 38, 12 );
Gotoxy (2, 2 );
Printf ("Window 1 ");
Window (8, 16, 40, 24 );
Border (8, 16, 40, 24 );
Gotoxy (3, 2 );
Printf ("window 2 ");
Getch ();
}

4. clreol () clears the character function at the end of the cursor line
Function: The clreol () function clears all characters from the cursor position to the end of the row in the current character window, while the cursor position remains unchanged.
Usage: The call method is void clreol (void );
Note: The header file of this function is conio. h.
Return Value: None
For example, the clreol () and clrscr () functions are used in the program ()
# Include "conio. h>
Void main ()
{
Register int I;
Gotoxy (6, 8 );
Printf ("this is a test of the clreol () function .");
Getch ();
Gotoxy (6, 8 );
Clreol ();
For (I = 0; I <20; I ++)
Printf ("Hello/N ");
Getch ();
Clrscr ();
}

5. insline () inserts an empty row Function
Function: The insline () function inserts an empty row into the row where the current cursor is located, and all rows below the cursor are moved down.
Usage: The call method of this function is void insline (void );
Note: This function is only used in text mode and is valid only when a character window is used.
The header file of this function is conio. h.
Return Value: None
For example, the program provides the insline () function usage.
# Include "conio. H"
Void main ()
{
Registser int I;
Clrscr ();
For (I = 1; I <24; I ++)
{
Gotoxy (1, I );
Printf ("this is line % d/N", I );
}
Getch ();
Gotoxy (1, 10 );
Insline ();
Getch ();
}

6. delline () deletes a row of functions.
Function: Delete the row where the cursor is located in the current window using the delline () function, and move all rows under this row up one row.
Usage: This function is called by void delline (void );
Note: If the current window is smaller than the entire screen, this function only affects characters in the window.
The corresponding header file of this function is conio. h.
Return Value: None
Example: first display 16 lines of text on the screen, and then delete 4th lines
# Include "conio. H"
Void main ()
{
Register int I;
Clrscr ();
For (I = 0; I <16; I ++) printf ("line % d/N", I );
Getch ();
Gotoxy (1, 4 );
Delline ();
Getch ();
}

7. gettext () is copied to the text function.
Function: The gettext () function is used to copy text in the rectangular area of the screen to the memory in the text state.
Usage: This function is called in int gettext (INT left, int top, int right, int bottom, void * buffer );
Note: The left and TOP Parameters in the function are the coordinates in the upper left corner of the rectangle area, right, and bottom are the coordinates in the lower right corner. These coordinates are the absolute coordinates of the screen, not the relative coordinates of the window. The buffer pointer must point to a memory that is sufficient to store the text in this rectangle. The memory used is calculated as follows:
Nod with number of knots = number of rows in the rectangle × Number of columns in the rectangle × 2
In this case, the number of rows multiplied by the number of columns and then multiplied by 2 is used to save each character on the screen. one byte storage unit stores the character itself, and the other stores its attributes.
The corresponding header file of this function is conio. h.
Return Value: if the function is successfully called, 1 is returned; otherwise, 0 is returned.
For example, copy the area at () and (10, 10) in the upper left corner of the screen to the memory pointed by the Buf.
Buf = (char *) malloc (10*10*2 );
If (! Buf) gettext (1, 1, 10, 10, Buf );

8. puttext (): Specifies the function for copying text.
Function: puttext () copies the text previously saved by gettext () to the memory pointed by the buffer to a rectangular area on the screen.
Usage: This function is called in int puttext (INT left, int top, int right, int bottom, void * buffer );
Note: In the function, left and top are the upper-left points, right, and bottom points in the upper-right corner of the rectangle area on the screen. The coordinates are the absolute coordinates of the screen, instead of using the relative coordinates of the window.
The header file of the function is conio. h.
Return Value: if the function is successfully called, the return value is 1. Otherwise, the return value is 0.
For example, content in a certain area of the screen is copied to the memory pointed by the Buf, and the text is copied to a new position on the screen.
Buf = (char *) malloc (10*10*2 );
Gettext (1, 1, 10, 10, Buf );
Puttext (16,16, 30,30, Buf );

9. movetext () mobile text function
Function: The movetext () function moves the text in a rectangle area on the screen to another area.
Usage: This function is called in int movetext (INT left, int top, int right, int bottom, int newleft, int newtop );
Note: Top and left are the coordinates of the rectangle area, right, and bottom in the lower right corner, newleft, and newright in the upper left corner of the area. These coordinates are the absolute coordinates of the screen, not the relative coordinates of the window.
To move a piece of text on the screen to another position on the screen, it is more efficient to use the movetext () function than to use gettext () and then puttext.
The header file of this function is conio. h.
Return Value: if more than one coordinate is invalid, the return value of the function is 0. Otherwise, 1 is returned.
Column: Move the text in the rectangular area at (8, 8) and (20, 20) in the lower right corner of the screen to the position at (10, 10) in the upper left corner:
Movetext (8, 20, 20, 10, 10 );

10. textmode () text mode function
Function: sets the function textmode () to text mode (or character state ).
Usage: The function call method is void textmode (INT mode );
Note: The mode parameter must be one of the modes shown in Table 1-2. (1) Use the mode name (symbol value) or an equivalent integer.

Table 1-2 text mode
----------------------------------------------------------------
Expression name (symbol value) equivalent integer description

Bw40 0 40 columns black and white
C 40 1 40 columns color
Bw80 2 80 columns black and white
C 80 3 80 columns color
Mono 7 80 columns monochrome
Lastmode-1 last Mode
----------------------------------------------------------------

After the function is called, the screen is reset, and the attributes of all characters are restored to their default values.
The header file corresponding to this function is conio. h.
Return Value: None
For example, set the screen to 80 columns in color mode.
Textmode (C80 );

Ii. Character attribute functions
You can set the High Brightness or low brightness of the character display, such as flashing and background color. Functions with these operations are called character attribute functions. In addition to single-mode and single-color display cards, the character attribute function applies to all other display cards.

11. highvideo () High Brightness Function
Function: sets the screen to high brightness.
Usage: This function is called by void highvideo (void );
Note: after calling this function, the characters written to the screen are highlighted. This function is only applicable to text mode (or character screen status ).
The header file corresponding to this function is conio. h.
Return Value: None
For example, set the screen to high brightness.
Highvideo ();

12. lowvideo () low brightness Function
Function: The lowvideo () function sets the screen to low-Brightness Display.
Usage: This function is called by void lowvideo (void );
Note: after calling this function, the characters written to the screen are low-brightness. This function is only applicable to text mode (or character screen status ).
The header file of this function is conio. h.
Return Value: None
For example, set the screen to low-Brightness
Lowvideo ();

13. normvideo (void );
Function: set the screen brightness to normal by using the normvideo () function.
Usage: This function is called by void normvideo (void );
Note: after calling this function, the characters written to the screen are normal brightness. This function is only applicable to text mode (or character-like screen status ).
The header file of this function is conio. h.
Return Value: None
For example, set the screen brightness to normal.
Normvideo ();

14. textcolor () text color Function
Function: textcolor () is used to set the text color (or character color) on the character screen. It can also be used to flash characters.
Method: Call void textcolor (INT color );
Description: The valid values of the color parameter in the function can be the color name (macro name) or other values in Table 1-3.

Table 1-3 color names and other values
---------------------------------------------------
Name and other values
Black 0 black
Blue 1 blue
Green 2 green
Cyan 3 Qing
Red 4 red
Magenta 5 foreign red
Brown 6 Brown
Lightgray 7 light gray
Drakgray 8 deep gray
Lightblue 9 light blue
Lightgreen 10 light green
Lightcyan 11 light blue
Lightred 12 light red
Lightmagenta 13
Yellow 14 yellow
White 15 White
Blink 128 flash drive
------------------------------------------------------------

After the textcolor () function is executed, only the color of the adventure character is output after the function is executed, without changing the color of other characters on the current screen. Obviously, if you want to output the flashing characters, you only need to take the color parameter in the function as blink. If you want to make the characters flash in color, the selected color value and 128 must be calculated as "or.
The header file of this function is conio. h.
Returned value: None
For example, the first statement in the following section causes the output character to flash, and the third statement causes the output to flash simultaneously with the color:
Textcolor (blink );
Printf ("hello ");
Textcolor (Red | blink );

15. textattr () text attribute function
Function: textattr () is used to set text attributes, that is, the background color, the color of the character itself, and whether the character flashes.
Usage: The function call method is void textattr (INT attribute );
Note: The simplest way to set the character background color attribute byte is to multiply the selected background color value by 16 and then use the character color value for bitwise OR (OR) operations. For example, if you need a red background or a green character, set it to red * 16 | green. If you want to flash the character, set the background color value, character color value, and flashing value together.
Bitwise OR operation. For example, if the background is blue or yellow and the characters are flashing, set it:
Yello | 128 | blue * 16
0th characters are used to set the character color.
The header file of this function is conio. h.
Return Value: None
For example, set the background to blue, the characters to red, and the characters to flash:
Textattr (Red | 128 | blue * 16 );

16. textbackground () text background Function
Function: sets the text background color (or character background color) on the character screen ).
Usage: This function is called by void textbackground (INT bcolor );
Description: The valid values of the bcolor parameter are 1-4 Background colors (macro names) or other values.
Table 1-4 Background Color and other values
-------------------------------------------------
Background Color and other values
-------------------------------------------------
Black 0 black
Blue 1 blue
Green 2 green
Cyan 3 Qing
Red 4 red
Magenta 5 foreign red
Brown 6 Brown
-------------------------------------------------

This function affects only the background color of subsequent characters, but does not change the background color of the characters currently displayed on the screen.
The header file corresponding to this function is conio. h.
Return Value: None
For example, set the text background color to Blue:
Textbackground (blue ));

3. Screen Display Function
Three functions are provided to display the status of the screen in text mode.

17. wherex () x coordinate function at the cursor
Function: wherex () returns the horizontal coordinates of the cursor in the current window.
Usage: This function is called in int wherex (void );
Note: This function call has no parameters. Its header file is conio. h.
Return Value: if the function is successfully called, the X coordinate value at the cursor is returned.
For example, see the example of the 18. wherey () function to call this function.

18. wherey () y coordinate function at the cursor
Function: wherey () returns the vertical coordinates of the cursor in the current window.
Usage: The function is called in int wherey (void );
Note: This function has no parameters. The corresponding header file is conio. h.
Return Value: if the function is successfully called, the Y coordinate value at the cursor is returned.
For example, call the wherex () and wherey () functions to return the X and Y coordinates of the current cursor and assign them to the Integer Variables xpos and ypos.
Int xpos, ypos;
Xpos = wherex ();
Ypos = wherey ();

19. gettextinfo () function for obtaining text window information
Function: The gettextinfo () function obtains information about the current text window and stores it in the real parameter structure.
Method: The function call method is void gettextinfo (struct text-Info * info );
Note: The parameter info in the function is a struct text-Info structured pointer. The struct text-Info structure is defined in the corresponding header file of this function:
Struct text-Info {
Unsigned char winleft; // X coordinate in the upper left corner of the window
Unsigned char wintop; // y coordinate in the upper left corner of the window
Unsigned char winright; // X coordinate in the lower right corner of the window
Unsigned char winbottom; // y coordinate in the lower right corner of the window
Unsigned char attribute; // text attribute
Unsigned char normattr; // normal attribute
Unsigned char currmode; // current Screen Display Mode
Unsigned char screenhight // text window height (in rows)
Unsigned char screenwidth; // text window width (measured by the number of characters)
Unsigned char curx; // X coordinate at the cursor
Unsigned char Cury; // y coordinate at the cursor
};
Remember, when calling the gettextinfo () function, you must pass the struct text-Info structure pointer or the first address of the structure. Do not pass the structure variable itself.
The header file corresponding to this function is conio. h.
Return Value: returns the coordinate of corner points in the text window, the height and width of the text, and the equivalent of the text attribute. The values are stored in the Structure Variable pointed to by info.
For example, the program statement describes how to correctly call the gettextinfo () function:
Struct text-Info win-status;
Gettextinfo (& Win-status );

Related Article

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.