• Linux屏幕控制(1)


    使用curses函数库管理基于文本的屏幕

    curses有很多个版本,有curses和ncurses版本,先查看自己的版本: ls -l /usr/include/*curses.h
    一般来讲,是不需要的查看的,现在的Linux都会自动集成最新的版本。


    1.curses术语和概念



    (1)屏幕,也就是你的显示屏,通常是你的终端设备,至少存在一个curses窗口,我们称之为stdscr,它与物理屏幕的尺寸一样,程序员可以创建一些尺寸小于此窗口的窗口。
    窗口可以相互重叠,子窗口必须包含在它的父窗口中。
    (2)函数库用两个数据结构来映射终端屏幕,分别是stdscr和curscr,stdscr对应标准屏幕,是默认的输出窗口。
        curscr对应的当前屏幕的状态,每次刷新屏幕时,系统会对比两个结构的不同之处,然后刷新屏幕
    (3)在一般的编程中,最好不要直接访问stdscr,调用curscr和refresh是最好的。
    (4)屏幕字符的显示使用的行号和列号,起点是左上角,这里的坐标是(0,0),任意一点的坐标可设置为(y,x),y代表行号,x代表列号
    (5)所有的curses程序设计,应该先初始化,然后再程序结束的时候恢复原状态,所以应该使用initscr和endwin完成

    下面这个程序会在终端15,15的位置输出hello world

    1. #include
    2. #include
    3. #include
    4. int main()
    5. {
    6. initscr();//初始化
    7. move(15,15);
    8. printw("hello world\n");
    9. refresh();
    10. sleep(2);
    11. endwin();//还原到默认
    12. return 0;
    13. }


    WINDOW* initscr(); 初始化,如果执行成功,则返回一个指向stdscr的指针,失败则发送一条错误信息,然后退出程序
    int endwin();成功返回OK,失败返回ERR,很明显是宏定义。



    2.输出到屏幕的函数集合



    chtype是unsigned long类型
    (1)int addch(const chtype ch); 添加一个字符
    (2)int addchstr(const chtype* str);添加一个字符串
    (3)printw(char* format);和printf的功能差不多,只不过这是针对当前屏幕的
    (4)refresh();刷新屏幕
    (5)box(WINDOW* win_ptr, chtype vertical_char, chtype horizontal_char);围绕窗口绘制一个方框
    (6)insch(chtype char_to_insert); 插入一个字符,把已有的字符右移
    (7)insertln();插入一个空白行,将现有行下移一行
    (8)delch();删除一个字符
    (9)delteln();删除一行
    (10)beep();蜂鸣
    (11)flash();屏幕闪烁


    3.从屏幕读取字符



    chtype inch();返回光标所在的字符
    int instr(char* string);返回光标所在的一行,写入到string中
    int innstr(char* string, int number_of_char);同上


    4.清除屏幕



    int erase();
    int clear()
    int clrtobot();清除光标位置直到结尾的字符
    clrtoeol();清除光标开头到一行的结尾


    5.移动光标



    int move(int new_y, int new_x);移动到对应的坐标
    int leaveok(WINDOW* window_ptr, bool flag);设置光标所在,flag取false,则光标在逻辑位置,否则随机位置。所以选择false


    6.字符属性



    int attron(chtype attribute);开启某属性
    int attroff(chtype attribute);关闭某属性
    int attrset(chtype attribute);设置curse属性
    int standout();开启更突出的模式
    int standend();关闭
    属性有好几种
    A_BLINK,A_BOLD,A_DIM,A_REVERSE,A_STANDOUT,A_UNDERLINE
    下面是Linux手册对各个属性的解释

                       A_NORMAL        Normal display (no highlight) 普通
                       A_STANDOUT      Best highlighting mode of the terminal. 高亮
                       A_UNDERLINE     Underlining 划线
                       A_REVERSE       Reverse video 倒放
                       A_BLINK         Blinking 闪烁
                       A_DIM           Half bright 半亮
                       A_BOLD          Extra bright or bold 跳出或者黑体
                       A_PROTECT       Protected mode 保护模式
                       A_INVIS         Invisible or blank mode
                       A_ALTCHARSET    Alternate character set
                       A_CHARTEXT      Bit-mask to extract a character
                       COLOR_PAIR(n)   Color-pair number n 颜色


     

                       
    移动,插入和属性的实验

    1. #include
    2. #include
    3. #include
    4. #include
    5. #include
    6. int main()
    7. {
    8. const char witch_one[] = " First Witch ";
    9. const char witch_two[] = " Second Witch ";
    10. const char* scan_ptr = NULL;
    11. initscr();
    12. printw("HERO");
    13. move(5, 15);
    14. attron(A_BOLD);
    15. refresh();
    16. sleep(1);
    17. move(8,15);
    18. attron(A_STANDOUT);
    19. printw("%s ","set it as A_STANDOUT");
    20. attroff(A_STANDOUT);
    21. refresh();
    22. sleep(1);
    23. move(10,10);
    24. printw("%s","curPosition(10,10)");
    25. move(11, 23);
    26. printw("%s","curPosition(11,23)");
    27. move(13,10);
    28. printw("%s","curPosition(13,10)");
    29. move(14,23);
    30. printw("%s","curPosition(14,23)");
    31. refresh();
    32. sleep(1);
    33. //插入字符到指定位置
    34. attron(A_DIM);
    35. scan_ptr = witch_one + strlen(witch_one) - 1;
    36. while(scan_ptr != witch_one)
    37. {
    38. move(10,10);
    39. insch(*scan_ptr--);//插入字符
    40. }
    41. scan_ptr = witch_two + strlen(witch_two) - 1;
    42. while(scan_ptr != witch_two)
    43. {
    44. move(13, 10);
    45. insch(*scan_ptr--);
    46. }
    47. attroff(A_DIM);
    48. refresh();
    49. sleep(1);
    50. move(LINES-1, COLS-1);

    输出在光标之后,所以先设置好属性,移动光标,最后输出

  • 相关阅读:
    Apache Doris 是什么
    IMMA~~
    Go入门系列:变量声明
    JSX 中使用 js 表达式
    RHCSA2
    阻止IP地址追踪的意义和编程实现
    极客日报:宿华不再担任快手CEO,程一笑接任;微软市值重登全球第一;Bootstrap 4.6.1发布
    Worthington酶促细胞收获&细胞粘附和收获
    day23_mysql
    Seata介绍
  • 原文地址:https://blog.csdn.net/weixin_42581560/article/details/127954711