使用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
2
3
4
5
6
7
8
9
10
11
12
13#include<curses.h> #include<stdio.h> #include<stdlib.h> int main() { initscr();//初始化 move(15,15); printw("hello worldn"); 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 颜色
移动,插入和属性的实验
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50#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);
输出在光标之后,所以先设置好属性,移动光标,最后输出
最后
以上就是喜悦苗条最近收集整理的关于Linux屏幕控制(1)的全部内容,更多相关Linux屏幕控制(1)内容请搜索靠谱客的其他文章。
发表评论 取消回复