NCURSES入门
Sunday, April 9, 2006 11:26:05 AM
NCURSES原来很容易使用,没有我想像中那么难,而且很好玩,哈哈。可以说学习使用ncurses的函数比学标准库里的函数还简单!而且也有C++ binding的版本。
下面用NCURSES-Programming-HOWTO里的例子说明一下ncurses是怎么回事。
不需要 stdio.h,因为 curses.h 里已经包含了它。
在debian里ncurses.h实际上是指向curses.h的symbolic link。
编译的时候注意要加上 -lncurses
当然,要装了ncurses开发包才能编译,这也很简单,因为有RPM包,可以在http://rpmfind.net/下载。
在debian下就更简单了,
aptitude install ncurses-dev
下面这个例子说明了ncurses有能力知道你的终端的分辨率(一般是80x25),并且说明了怎样在屏幕中间显示一行字。
下面用NCURSES-Programming-HOWTO里的例子说明一下ncurses是怎么回事。
#include <ncurses.h>
int main()
{
initscr(); /* 这个表示进入curses模式了 */
printw("Hello World !!!"); /* 这个是curses模式里的printf */
refresh(); /* 更新屏幕,没有这个的话hello world是不会出现的 */
getch(); /* 这不就是TC里的getch()吗? */
endwin(); /* 退出curses模式,在return或exit前不要忘了用这个 */
return 0;
}
不需要 stdio.h,因为 curses.h 里已经包含了它。
在debian里ncurses.h实际上是指向curses.h的symbolic link。
编译的时候注意要加上 -lncurses
gcc -Wall hello.c -lncurses
当然,要装了ncurses开发包才能编译,这也很简单,因为有RPM包,可以在http://rpmfind.net/下载。
在debian下就更简单了,
aptitude install ncurses-dev
下面这个例子说明了ncurses有能力知道你的终端的分辨率(一般是80x25),并且说明了怎样在屏幕中间显示一行字。
#include <ncurses.h> /* ncurses.h includes stdio.h */
#include <string.h>
int main()
{
char mesg[]="Just a string";
int row,col;
initscr();
keypad(stdscr, TRUE);
noecho();
getmaxyx(stdscr,row,col);
mvprintw(row/2,(col-strlen(mesg))/2,"%s",mesg);
mvprintw(row-2,0,"This screen has %d rows and %d columns\n",row,col);
printw("Try resizing your window(if possible) and then run this program again");
refresh();
getch();
endwin();
return 0;
}








How to use Quote function: