首先说一下安装curses库吧
sudo apt-get install libncurses5-dev
1. 包含头文件:curses.h
2. 编译时应加上链接语句-lcurses,如:gcc temp.c -o temp -lcurses
常用函数
initscr() |
初始化curses库和ttty
(在开始curses编程之前,必须使用initscr()这个函数来开启curses模式)
|
endwin() |
关闭curses并重置tty
(结束curses编程时,最后调用的一个函数)
|
move(y,x) | 将游标移动至 x,y 的位置 |
getyx(win,y,x) | 得到目前游标的位置(请注意!是 y,x 而不是&y,&x) |
clear() and erase() | 将整个萤幕清除(请注意配合refresh() 使用) |
echochar(ch) | 显示某个字元 |
addch(ch) | 在当前位置画字符ch |
mvaddch(y,x,ch) | 在(x,y) 上显示某个字元。相当於呼叫move(y,x);addch(ch); |
addstr(str) | 在当前位置画字符串str |
mvaddstr(y,x,str) | 在(x,y) 上显示一串字串。相当於呼叫move(y,x);addstr(str); |
printw(format,str) | 类似 printf() ,以一定的格式输出至萤幕 |
mvprintw(y,x,format,str) | 在(x,y) 位置上做 printw 的工作。相当於呼叫move(y,x);printw(format,str); |
getch() | 从键盘读取一个字元。(注意!传回的是整数值) |
getstr() | 从键盘读取一串字元。 |
scanw(format,&arg1,&arg2...) | 如同 scanf,从键盘读取一串字元。 |
beep() | 发出一声哔声 |
box(win,ch1,ch2) | 自动画方框 |
refresh() | 使屏幕按照你的意图显示。比较工作屏幕和真实屏幕的差异,然后refresh通过终端驱动送出那些能使真实屏幕于工作屏幕一致的字符和控制码。(工作屏幕就像磁盘缓存,curses中的大部分的函数都只对它进行修改) |
standout() | 启动standout模式(一般使屏幕发色) |
standend() | 关闭standout模式 |
initscr();// 制作标准窗口
标准窗口 stdscr
WINDOW *derwin(//定义一个自定义窗口
WINDOW *orig, //父窗口
int nlines,
int ncols,
int begin_y,
int begin_x);
WINDOW *orig, //父窗口
int nlines,
int ncols,
int begin_y,
int begin_x);
int box(
WINDOW *win,//要画的窗口
chtype verch,//垂直线用什么来画
chtype horch);//水平线用什么来画
WINDOW *win,//要画的窗口
chtype verch,//垂直线用什么来画
chtype horch);//水平线用什么来画
LINES stdscr有多少个光标高
COLS stdscr有多少个光标宽
在指定窗口指定位置以标准格式化方式打印字符串
int mvwprintw(
WINDOW *win, //指定窗口
int y,//指定位置
int x, //指定位置
const char *fmt, ...);//打印字符串
time();//可以获取当前系统时间,但这个时间是距离1970年1月1日00:00:00的秒数。
localtime
struct tm {
int tm_sec; /* second
s */
int tm_min; /* minute
s */
int tm_hour; /* hours
*/
。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。
例子
#include <curses.h>
#include <time.h>
#include <stdlib.h>
int main(){
int d;
struct tm* tt = NULL;
time_t t;
initscr();// 制作标准窗口
curs_set(0);//设置光标透明度为100
WINDOW* w = derwin(stdscr,3,10,1,COLS-11);//定义窗口
box(w,0,0);//画窗口
while(1){
t = time(0);
tt = localtime(&t);
//获取当前系统时间
mvwprintw(w,1,1,"%02d:%02d:%02d",tt->tm_hour,tt->tm_min,tt->tm_sec);
sleep(1);
wrefresh(w);
}
refresh();//刷新标准窗口
getch();//从窗口上获取一个字符
endwin();//销毁窗口
return 0;
}