Display.h
[CPP]View Plaincopy
- /*************************************************************
- Filename:display.h
- Filefunc: Control screen display information and cursor status header file
- version:v0.1
- Author:sunrier
- date:2012-06-09
- Control of the screen and cursor under Descp:linux
- *************************************************************/
- #ifndef _display_h_
- #define _display_h_
- #ifdef __cplusplus
- extern "C" {
- #endif
- #include <stdio.h>
- #include <string.h>
- #include <unistd.h>
- #define Max_display_item 25//25 Line
- #define MAX_DISPLAY_WIDTH 80//80 Column
- void Display_message (int x,int y,char *str);
- void Draw_box (int row,int col,int len,int wid);
- void Move_cursor (int x,int y);
- void Display_cursor ( void);
- void Hide_cursor ( void);
- void Clear_all_display ( void);
- void Clear_screen ( void);
- #ifdef __cplusplus
- }
- #endif
- #endif
Display.c
[CPP]View Plaincopy
- /*************************************************************
- Filename:display.c
- Filefunc: Control screen display information and cursor status implementation file
- version:v0.1
- Author:sunrier
- date:2012-06-09
- Control of the screen and cursor under Descp:linux
- *************************************************************/
- #include "Display.h"
- X-> line (starting from 1),y-> column (starting from 1)
- Display information in x row y column
- void Display_message (int x,int y,char *str)
- {
- unsigned int uilen=0;
- Char szmessage[512];
- memset (szmessage,0,sizeof (szmessage));
- sprintf (Szmessage,"\033[%d;%dh%s", x,y,str);
- Uilen = strlen (szmessage);
- Write (1,szmessage,uilen);
- }
- Draw Border
- void Draw_box (int row,int col,int len,int wid)
- {
- int i = 0,end = 0;
- Char sztop[100],szbottom[100];
- memset (sztop,0,sizeof (sztop));
- memset (szbottom,0,sizeof (Szbottom));
- strcpy (Sztop,"┏");
- strcpy (Szbottom,"┗");
- For (i=1;i<wid/2-1;i++)
- {
- strcat (Sztop,"-");
- strcat (Szbottom,"-");
- }
- strcat (Sztop,"┓\0");
- strcat (Szbottom,"┛\0");
- end=col+wid/2*2-2;
- Display_message (Row,col,sztop);
- For (i=1;i<len-1;i++)
- {
- Display_message (Row+i,col,"┃");
- Display_message (Row+i,end,"┃\0");
- }
- Display_message (Row+len-1,col,szbottom);
- }
- Move cursor to x row y column
- void Move_cursor (int x,int y)
- {
- unsigned int uilen = 0;
- Char szmessage[16];
- memset (szmessage,0,sizeof (szmessage));
- sprintf (Szmessage,"\033[%d;%dh", X, y);
- Uilen=strlen (szmessage);
- Write (1,szmessage,uilen);
- }
- Display cursor
- void Display_cursor ( void)
- {
- printf ("\033[?25h");
- }
- Hide cursor
- void Hide_cursor ( void)
- {
- printf ("\033[?25l");
- }
- Clears all display information (x:1 to 25 rows; Y:1 to 80 columns)
- void Clear_all_display ( void)
- {
- int i=0;
- Char Szzero[max_display_width];
- memset (Szzero, ", sizeof (Szzero));
- szzero[sizeof (Szzero)-1] = 0;
- For (i = 1; I <= max_display_item; i++)
- {
- Display_message (I,1,szzero);
- }
- }
- Clear Screen
- void Clear_screen ( void)
- {
- //printf ("\033[2j\033[1;1h\n");
- printf ("\033[2j\033[1;1h");
- }
[CPP]View Plaincopy
- Report:
- Output of colors under Linux terminal
- You can also create colorful fonts and patterns at the command line, just add some color codes
- Example 1:
- printf ("\033[44;31m sunrier\033[0m")
- Where 44 represents the background color, 31 represents the font colour, Sunrier is a string, and the following \033[0m is the control code, which indicates that all properties are closed, m means that the property is set and then ends
- Example 2:
- Echo-e "\033[41;36m Red Bottom Green character \033[0m"
- Where 41 represents the background color and 36 represents the font
- Word Background color range: 47----
- 40: Black
- 41: Crimson
- 42: Green
- 43: Yellow
- 44: Blue
- 45: Purple
- 46: Dark Green
- 47: White
- Word Color: 37-----------
- 30: Black
- 31: Red
- 32: Green
- 33: Yellow
- 34: Blue
- 35: Purple
- 36: Dark Green
- 37: White
- Description of the ANSI control code
- \33[0m Close all properties, set as default properties
- \33[1m Setting High brightness
- \33[4m Underline
- \33[5m Flashing
- \33[7M Reverse Display
- \33[8m blanking
- \33[30m--\33[37m setting foreground color
- \33[40m--\33[47m Set background color
- \33[na the cursor to move n rows
- \33[NB cursor down n rows
- \33[NC cursor right shifts n rows
- \33[nd cursor left n rows
- \33[Y;XH Setting the cursor position
- \33[2J Clear Screen
- \33[k clears the contents from the cursor to the end of the line
- \33[s Save Cursor position
- \33[u Restore cursor Position
- \33[?25l Hide Cursor
- \33[?25h Display cursor
- Generally used to write \33 \033, where \nnn insert nnn (note N is 1 to 3 bits) (octal) represents the ASCII character
Implement control screen display information and cursor status under Linux