在上一篇的《Ubuntu中NetBeans下Curses的使用》http://blog.youkuaiyun.com/akof1314/archive/2010/10/18/5948505.aspx
Curses将终端屏幕看成是由字符单元组成的网格,每一个单元由(行、列)坐标对标示。坐标系的原点是屏幕的左上角,行坐标自上而下递增,列坐标自左向右递增。下面介绍其中的9个函数:
函数名 | 作用 |
initscr() | 初始化curses库和tty |
endwin() | 关闭curses并重置tty |
refresh() | 使屏幕按照你的意图显示 |
move(r,c) | 移动光标到屏幕的(r,c)位置 |
addstr(s) | 在当前位置画字符串s |
addch(c) | 在当前位置画字符c |
clear() | 清屏 |
standout() | 启动standout模式(一般使屏幕反色) |
standend() | 关闭standout模式 |
之后继续学习Curses库的使用,下面的代码把curses函数与循环,变量和其他函数组合在一起会产生更复杂的显示效果。
示例:
/*hello2.c
*2010年10日18号 在Ubuntu下的NetBeans IDE测试通过
*purpose show how to use curses functions with a loop
* outline initialize,draw stuff,wait up
*/
#include <stdio.h>
#include <curses.h>
main()
{
int i;
initscr();
clear();
for(i= 0;i<LINES;i++){
move(i,i+ 1);
if(i% 2== 1)
standout(); //启动standout模式,一般使屏幕反色
addstr( "Hello,world");
if(i% 2== 1)
standend(); //关闭standout模式
}
refresh();
getch();
endwin();
}
*2010年10日18号 在Ubuntu下的NetBeans IDE测试通过
*purpose show how to use curses functions with a loop
* outline initialize,draw stuff,wait up
*/
#include <stdio.h>
#include <curses.h>
main()
{
int i;
initscr();
clear();
for(i= 0;i<LINES;i++){
move(i,i+ 1);
if(i% 2== 1)
standout(); //启动standout模式,一般使屏幕反色
addstr( "Hello,world");
if(i% 2== 1)
standend(); //关闭standout模式
}
refresh();
getch();
endwin();
}
效果图如下:
时钟编程,学习与系统函数sleep结合。
示例:
/*hello3.c
*2010年10日18号 在Ubuntu下的NetBeans IDE测试通过
*purpose using refresh and sleep for animated effects
* outline initialize,draw stuff,wait up
*/
#include <stdio.h>
#include <curses.h>
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();
sleep( 1);
refresh();
}
endwin();
}
*2010年10日18号 在Ubuntu下的NetBeans IDE测试通过
*purpose using refresh and sleep for animated effects
* outline initialize,draw stuff,wait up
*/
#include <stdio.h>
#include <curses.h>
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();
sleep( 1);
refresh();
}
endwin();
}
效果图如下:
现在可以开始变换出动画效果了,只需要把之前显示的字符串给“擦除”就创造出移动的假象。
示例:
/*hello4.c
*2010年10日18号 在Ubuntu下的NetBeans IDE测试通过
*purpose show how to use erase,time,and draw for animation
*/
#include <stdio.h>
#include <curses.h>
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);
addstr( " ");
}
endwin();
}
*2010年10日18号 在Ubuntu下的NetBeans IDE测试通过
*purpose show how to use erase,time,and draw for animation
*/
#include <stdio.h>
#include <curses.h>
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);
addstr( " ");
}
endwin();
}