Implement control screen display information and cursor status under Linux

Source: Internet
Author: User
Tags clear screen set background sprintf

Display.h

[CPP]View Plaincopy
  1. /*************************************************************
  2. Filename:display.h
  3. Filefunc: Control screen display information and cursor status header file
  4. version:v0.1
  5. Author:sunrier
  6. date:2012-06-09
  7. Control of the screen and cursor under Descp:linux
  8. *************************************************************/
  9. #ifndef _display_h_
  10. #define _display_h_
  11. #ifdef __cplusplus
  12. extern "C" {
  13. #endif
  14. #include <stdio.h>
  15. #include <string.h>
  16. #include <unistd.h>
  17. #define Max_display_item 25//25 Line
  18. #define MAX_DISPLAY_WIDTH 80//80 Column
  19. void Display_message (int x,int y,char *str);
  20. void Draw_box (int row,int col,int len,int wid);
  21. void Move_cursor (int x,int y);
  22. void Display_cursor ( void);
  23. void Hide_cursor ( void);
  24. void Clear_all_display ( void);
  25. void Clear_screen ( void);
  26. #ifdef __cplusplus
  27. }
  28. #endif
  29. #endif


Display.c

[CPP]View Plaincopy
  1. /*************************************************************
  2. Filename:display.c
  3. Filefunc: Control screen display information and cursor status implementation file
  4. version:v0.1
  5. Author:sunrier
  6. date:2012-06-09
  7. Control of the screen and cursor under Descp:linux
  8. *************************************************************/
  9. #include "Display.h"
  10. X-> line (starting from 1),y-> column (starting from 1)
  11. Display information in x row y column
  12. void Display_message (int x,int y,char *str)
  13. {
  14. unsigned int uilen=0;
  15. Char szmessage[512];
  16. memset (szmessage,0,sizeof (szmessage));
  17. sprintf (Szmessage,"\033[%d;%dh%s", x,y,str);
  18. Uilen = strlen (szmessage);
  19. Write (1,szmessage,uilen);
  20. }
  21. Draw Border
  22. void Draw_box (int row,int col,int len,int wid)
  23. {
  24. int i = 0,end = 0;
  25. Char sztop[100],szbottom[100];
  26. memset (sztop,0,sizeof (sztop));
  27. memset (szbottom,0,sizeof (Szbottom));
  28. strcpy (Sztop,"┏");
  29. strcpy (Szbottom,"┗");
  30. For (i=1;i<wid/2-1;i++)
  31. {
  32. strcat (Sztop,"-");
  33. strcat (Szbottom,"-");
  34. }
  35. strcat (Sztop,"┓\0");
  36. strcat (Szbottom,"┛\0");
  37. end=col+wid/2*2-2;
  38. Display_message (Row,col,sztop);
  39. For (i=1;i<len-1;i++)
  40. {
  41. Display_message (Row+i,col,"┃");
  42. Display_message (Row+i,end,"┃\0");
  43. }
  44. Display_message (Row+len-1,col,szbottom);
  45. }
  46. Move cursor to x row y column
  47. void Move_cursor (int x,int y)
  48. {
  49. unsigned int uilen = 0;
  50. Char szmessage[16];
  51. memset (szmessage,0,sizeof (szmessage));
  52. sprintf (Szmessage,"\033[%d;%dh", X, y);
  53. Uilen=strlen (szmessage);
  54. Write (1,szmessage,uilen);
  55. }
  56. Display cursor
  57. void Display_cursor ( void)
  58. {
  59. printf ("\033[?25h");
  60. }
  61. Hide cursor
  62. void Hide_cursor ( void)
  63. {
  64. printf ("\033[?25l");
  65. }
  66. Clears all display information (x:1 to 25 rows; Y:1 to 80 columns)
  67. void Clear_all_display ( void)
  68. {
  69. int i=0;
  70. Char Szzero[max_display_width];
  71. memset (Szzero, ", sizeof (Szzero));
  72. szzero[sizeof (Szzero)-1] = 0;
  73. For (i = 1; I <= max_display_item; i++)
  74. {
  75. Display_message (I,1,szzero);
  76. }
  77. }
  78. Clear Screen
  79. void Clear_screen ( void)
  80. {
  81. //printf ("\033[2j\033[1;1h\n");
  82. printf ("\033[2j\033[1;1h");
  83. }


[CPP]View Plaincopy
    1. Report:
    2. Output of colors under Linux terminal
    3. You can also create colorful fonts and patterns at the command line, just add some color codes
    4. Example 1:
    5. printf ("\033[44;31m sunrier\033[0m")
    6. 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
    7. Example 2:
    8. Echo-e "\033[41;36m Red Bottom Green character \033[0m"
    9. Where 41 represents the background color and 36 represents the font
    10. Word Background color range: 47----
    11. 40: Black
    12. 41: Crimson
    13. 42: Green
    14. 43: Yellow
    15. 44: Blue
    16. 45: Purple
    17. 46: Dark Green
    18. 47: White
    19. Word Color: 37-----------
    20. 30: Black
    21. 31: Red
    22. 32: Green
    23. 33: Yellow
    24. 34: Blue
    25. 35: Purple
    26. 36: Dark Green
    27. 37: White
    28. Description of the ANSI control code
    29. \33[0m Close all properties, set as default properties
    30. \33[1m Setting High brightness
    31. \33[4m Underline
    32. \33[5m Flashing
    33. \33[7M Reverse Display
    34. \33[8m blanking
    35. \33[30m--\33[37m setting foreground color
    36. \33[40m--\33[47m Set background color
    37. \33[na the cursor to move n rows
    38. \33[NB cursor down n rows
    39. \33[NC cursor right shifts n rows
    40. \33[nd cursor left n rows
    41. \33[Y;XH Setting the cursor position
    42. \33[2J Clear Screen
    43. \33[k clears the contents from the cursor to the end of the line
    44. \33[s Save Cursor position
    45. \33[u Restore cursor Position
    46. \33[?25l Hide Cursor
    47. \33[?25h Display cursor
    48. 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

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.