您的位置:首页 > 编程语言

利用curses库编程开始

2013-06-24 00:06 169 查看
curses库常用函数:



注意编译时要用这样的格式:gcc xxx.c -l curses -o xxx

第一个小例子:

include <stdio.h>
#include <curses.h>

int main()
{
initscr();

clear();
move(10,20);
addstr("Hello, world");
move(LINES-1, 0);
refresh();
getch();
endwin();
return 0;
}
运行效果如下:



第二个小例子:

#include <stdio.h>
#include <curses.h>

int main()
{
int i;

initscr();
clear();
for (i = 0; i < LINES; i++)
{
move(i, i+1);
if (i%2 == 1)
standout();     //反白显示
addstr("Hello, world");
if (i%2 == 1)
standend();     //关闭反白显示
}
refresh();
getch();
endwin();
return 0;
}
运行效果:



第三个小例子:

#include <stdio.h>
#include <curses.h>

int main()
{
int i;

initscr();
clear();
for (i = 0; i < LINES; i++)
{
move(i, i+1);
if (i%2 == 1)
standout();
addstr("Hello, world");
if (i%2 == 1)
standend();
refresh();
sleep(1);
move(i, i+1);                   //move back
addstr("             ");        //erase the line appear before
}
endwin();
return 0;
}
运行效果:字符串沿着对角线一行一下行的向下移动

第四个小例子:

#include <stdio.h>
#include <curses.h>

#define LEFTEDGE    10  /* 左边界 */
#define RIGHTEDGE   30  /* 右边界 */
#define ROW         10

int main()
{
char *message = "Hello";
char *blank = "     ";
int dir = 1;
int pos = LEFTEDGE;

initscr();
clear();
while (1)
{
move(ROW, pos);
addstr(message);        /* draw string */
//      move(LINES-1, COLS-1);
refresh();              /* show string */
sleep(1);
move(ROW, pos);         /* move back */
addstr(blank);          /* erase string */
pos += dir;             /* advance position */
if (pos >= RIGHTEDGE)   /* check for bounce */
dir = -1;
if (pos <= LEFTEDGE)
dir = 1;
}
return 0;
}
运行效果:在(ROW,LEFTEDGE)----(ROW,RIGHTEDGE)间来回移动字符串
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: