Linux 下实现控制屏幕显示信息和光标的状态

 

//display.h

[cpp]  view plain copy
 
  1. /*************************************************************     
  2.     FileName : display.h 
  3.     FileFunc : 控制屏幕显示信息和光标的状态头文件   
  4.     Version  : V0.1     
  5.     Author   : Sunrier     
  6.     Date     : 2012-06-09 
  7.     Descp    : Linux下实现屏幕和光标的控制     
  8. *************************************************************/  
  9. #ifndef   _DISPLAY_H_      
  10. #define   _DISPLAY_H_   
  11.   
  12. #ifdef __cplusplus  
  13. extern "C" {  
  14. #endif  
  15.   
  16. #include <stdio.h>  
  17. #include <string.h>  
  18. #include <unistd.h>  
  19.   
  20. #define MAX_DISPLAY_ITEM    25  //25行  
  21. #define MAX_DISPLAY_WIDTH   80  //80列  
  22.   
  23. void Display_Message(int x,int y,char *str);  
  24. void Draw_Box(int row,int col,int len,int wid);  
  25. void Move_Cursor(int x,int y);  
  26. void Display_Cursor( void );  
  27. void Hide_Cursor( void );  
  28. void Clear_All_Display( void );  
  29. void Clear_Screen( void );  
  30.   
  31. #ifdef __cplusplus  
  32. }  
  33. #endif  
  34.   
  35. #endif   


 

 

 

//display.c

[cpp]  view plain copy
 
  1. /*************************************************************     
  2.     FileName : display.c 
  3.     FileFunc : 控制屏幕显示信息和光标的状态实现文件    
  4.     Version  : V0.1     
  5.     Author   : Sunrier     
  6.     Date     : 2012-06-09 
  7.     Descp    : Linux下实现屏幕和光标的控制     
  8. *************************************************************/  
  9. #include "display.h"  
  10.   
  11. //x->行(从1开始),y->列(从1开始)  
  12. //在X行Y列显示信息  
  13. void Display_Message(int x,int y,char *str)  
  14. {  
  15.     unsigned int uiLen=0;  
  16.     char szMessage[512];  
  17.     memset(szMessage,0,sizeof(szMessage));  
  18.   
  19.     sprintf(szMessage,"\033[%d;%dH%s",x,y,str);  
  20.     uiLen = strlen(szMessage);  
  21.     write(1,szMessage,uiLen);  
  22. }  
  23.   
  24. //画边框  
  25. void Draw_Box(int row,int col,int len,int wid)  
  26. {  
  27.     int i = 0,end = 0;  
  28.     char szTop[100],szBottom[100];  
  29.   
  30.     memset(szTop,0,sizeof(szTop));  
  31.     memset(szBottom,0,sizeof(szBottom));  
  32.     strcpy(szTop,"┏");  
  33.     strcpy(szBottom,"┗");  
  34.     for (i=1;i<wid/2-1;i++)  
  35.     {  
  36.         strcat(szTop,"━");  
  37.         strcat(szBottom,"━");  
  38.     }  
  39.     strcat(szTop,"┓\0");  
  40.     strcat(szBottom,"┛\0");  
  41.   
  42.     end=col+wid/2*2-2;  
  43.     Display_Message(row,col,szTop);  
  44.     for (i=1;i<len-1;i++)   
  45.     {  
  46.         Display_Message(row+i,col,"┃");  
  47.         Display_Message(row+i,end,"┃\0");  
  48.     }  
  49.     Display_Message(row+len-1,col,szBottom);  
  50. }  
  51.   
  52. //移动光标到X行Y列  
  53. void Move_Cursor(int x,int y)  
  54. {  
  55.     unsigned int uiLen = 0;  
  56.     char szMessage[16];  
  57.   
  58.     memset(szMessage,0,sizeof(szMessage));  
  59.     sprintf(szMessage,"\033[%d;%dH",x,y);  
  60.     uiLen=strlen(szMessage);  
  61.     write(1,szMessage,uiLen);  
  62. }  
  63.   
  64. //显示光标  
  65. void Display_Cursor( void )  
  66. {  
  67.     printf("\033[?25h");  
  68. }  
  69.   
  70. //隐藏光标  
  71. void Hide_Cursor( void )  
  72. {  
  73.     printf("\033[?25l");  
  74. }  
  75.   
  76. //清除所有的显示信息(X:1到25行;Y:1到80列)  
  77. void Clear_All_Display( void )  
  78. {  
  79.     int i=0;  
  80.     char  szZero[MAX_DISPLAY_WIDTH];  
  81.     memset(szZero, ' ', sizeof(szZero));  
  82.     szZero[sizeof(szZero) -1] = 0;  
  83.     for(i = 1; i <= MAX_DISPLAY_ITEM; i++)  
  84.     {  
  85.         Display_Message(i,1,szZero);  
  86.     }  
  87. }  
  88.   
  89. //清除屏幕  
  90. void Clear_Screen( void )  
  91. {  
  92.     //printf("\033[2J\033[1;1H\n");  
  93.     printf("\033[2J\033[1;1H");  
  94. }  


 

 

[cpp]  view plain copy
 
    1. 附:  
    2. Linux 终端下颜色的输出  
    3.   
    4. 在命令行下也能产生五颜六色的字体和图案,只需要加上一些颜色代码  
    5. 例1:  
    6.     printf("\033[44;31m Sunrier\033[0m")  
    7.     其中44代表字背景色, 31代表字体的颜色,Sunrier是字符串,后面的\033[0m是控制码,表示关闭所有属性,m意味着设置属性然后结束  
    8.       
    9.       
    10. 例2:  
    11.     echo -e "\033[41;36m 红底绿字\033[0m"  
    12.     其中41代表字背景色, 36代表字体的颜色  
    13.   
    14.     字背景颜色范围:40----47  
    15.     40:黑  
    16.     41:深红  
    17.     42:绿  
    18.     43:黄色  
    19.     44:蓝色  
    20.     45:紫色  
    21.     46:深绿  
    22.     47:白色  
    23.   
    24.     字颜色:30-----------37  
    25.     30:黑  
    26.     31:红  
    27.     32:绿  
    28.     33:黄  
    29.     34:蓝色  
    30.     35:紫色  
    31.     36:深绿  
    32.     37:白色  
    33.   
    34.     ANSI控制码的说明  
    35.     \33[0m 关闭所有属性,设置成默认属性  
    36.     \33[1m 设置高亮度  
    37.     \33[4m 下划线  
    38.     \33[5m 闪烁  
    39.     \33[7m 反显  
    40.     \33[8m 消隐  
    41.     \33[30m -- \33[37m 设置前景色  
    42.     \33[40m -- \33[47m 设置背景色  
    43.     \33[nA 光标上移n行  
    44.     \33[nB 光标下移n行  
    45.     \33[nC 光标右移n行  
    46.     \33[nD 光标左移n行  
    47.     \33[y;xH设置光标位置  
    48.     \33[2J 清屏  
    49.     \33[K 清除从光标到行尾的内容  
    50.     \33[s 保存光标位置  
    51.     \33[u 恢复光标位置  
    52.     \33[?25l 隐藏光标  
    53.     \33[?25h 显示光标  
    54.   
    55. 一般使用时习惯把\33写成\033,其中\nnn 插入nnn(注n为1到3位)(八进制)所代表的ASCII字符  

转载于:https://www.cnblogs.com/lvdongjie/p/4153156.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值