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

#include<curses.h>
#include<stdio.h>
#include<stdlib.h>
int main()
{
        initscr();//初始化
        move(15,15);
        printw("hello world\n");
        refresh();
        sleep(2);
        endwin();//还原到默认
        return 0;
}


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 颜色


 

                   
移动,插入和属性的实验

#include<term.h>
#include<unistd.h>
#include<stdlib.h>
#include<string.h>
#include<curses.h>

int main()
{
        const char witch_one[] = " First Witch ";
        const char witch_two[] = " Second Witch ";
        const char* scan_ptr = NULL;
        initscr();
        printw("HERO");
        move(5, 15);
        attron(A_BOLD);
        refresh();
        sleep(1);

        move(8,15);
        attron(A_STANDOUT);
        printw("%s ","set it as A_STANDOUT");
        attroff(A_STANDOUT);
        refresh();
        sleep(1);

        move(10,10);
        printw("%s","curPosition(10,10)");
        move(11, 23);
        printw("%s","curPosition(11,23)");
        move(13,10);
        printw("%s","curPosition(13,10)");
        move(14,23);
        printw("%s","curPosition(14,23)");
        refresh();
        sleep(1);

        //插入字符到指定位置
        attron(A_DIM);
        scan_ptr = witch_one + strlen(witch_one) - 1;
        while(scan_ptr != witch_one)
        {
                move(10,10);
                insch(*scan_ptr--);//插入字符
        }
        scan_ptr = witch_two + strlen(witch_two) - 1;
        while(scan_ptr != witch_two)
        {
                move(13, 10);
                insch(*scan_ptr--);
        }
        attroff(A_DIM);
        refresh();
        sleep(1);

        move(LINES-1, COLS-1);

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

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值