利用curses库编程开始

来源:互联网 发布:2016大学生网购数据 编辑:程序博客网 时间:2024/06/05 01:06

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)间来回移动字符串

原创粉丝点击