免费注册 查看新帖 |

Chinaunix

  平台 论坛 博客 文库
最近访问板块 发新帖
查看: 865 | 回复: 0
打印 上一主题 下一主题

ANSI的Escape序列屏幕控制码 [复制链接]

论坛徽章:
0
跳转到指定楼层
1 [收藏(0)] [报告]
发表于 2009-10-04 01:56 |只看该作者 |倒序浏览

实为printf 函数的拓展应用
可设置字背景颜色;字体颜色m字符串
int ESC =27;
ptintf("%c[参数",ESC);

//cusor_ctrl.h
#ifndef __CUSOR_CTRL_H_
#define __CUSOR_CTRL_H_
#include
#include
#define     COLOR_RED              31
#define     COLOR_BLACK            30
#define     COLOR_GREEN            32
#define     COLOR_BLUE             34
#define     COLOR_YELLOW           33
#define     COLOR_WHITE            37
#define     COLOR_CYAN             36
#define     COLOR_MAGENTA          35
int ESC = 27;
//清屏
void clear_screen(void);
//清除从光标位置到行末的内容
void clear_to_end(void);
//光标移动到(x,y)
void cusor_moveto(int x, int y);
//保存光标位置
void cusor_get_pos(void);
//恢复光标位置
void cusor_set_pos(void);
//光标上移num行
void cusor_up(int num);
//光标下移num行
void cusor_down(int num);
//光标左移num个字符
void cusor_lift(int num);
//光标右移num个字符
void cusor_right(int num);
//设置前景颜色
void set_fg_color(int color);
//设置背景颜色
void set_bg_color(int color);
#endif //__CUSOR_CTRL_H_
int main()
{
    int x=0;int y = 0;
    int color=31;
    clear_screen();   
    cusor_moveto(20,10);
    while(1)
    {
        cusor_get_pos();
        printf("(20,10)");
        fflush(stdout);
        sleep(1);
        cusor_up(5);
        sleep(1);
        cusor_down(5);
        sleep(1);
        cusor_right(5);
        sleep(1);
        cusor_lift(5);
        sleep(1);
        cusor_moveto(50,10);
        printf("(50,10)\n");
        sleep(1);
        set_bg_color(COLOR_BLACK);
        set_fg_color(COLOR_RED);
        printf("\ngood morning\n");
        sleep(1);
        set_bg_color(COLOR_YELLOW);
        set_fg_color(COLOR_GREEN);
        printf("gaorenwei\n");
       cusor_set_pos();
    }
    return 0;
}
void clear_screen(void)
{    printf("%c[2J",ESC);
fflush(stdout);
}
void clear_to_end(void)
{   
printf("%c[K",ESC);
fflush(stdout);
}
void cusor_moveto(int x, int y)
{// ESC[y;xH   
printf("%c[%d;%dH",ESC,y,x);   
fflush(stdout);
}
void cusor_get_pos(void)
{// ESC[s    printf("%c[s",ESC);   
fflush(stdout);
}
//恢复光标位置
void cusor_set_pos(void)
{// ESC[u   
printf("%c[u",ESC);   
fflush(stdout);
}
//光标上移num行
void cusor_up(int num)
{   
while(num--)   
{ // up =  ESC[A         
printf("%c[A",ESC);   
}      
fflush(stdout);
}
//光标下移num行
void cusor_down(int num)
{   
while(num--)   
{// down = ESC[B      
printf("%c[B",ESC);                  
}    fflush(stdout);}
//光标左移num个字符
void cusor_lift(int num)
{   
while(num--)   
{// lift = ESC[D        
printf("%c[D",ESC);                  
  }   
fflush(stdout);
}
//光标右移num个字符
void cusor_right(int num)
{   
while(num--)   
{// right = ESC[C      
printf("%c[C",ESC);                 
   }   
fflush(stdout);
}
//设置前景颜色
void set_fg_color(int color)
{// ESC[#m   
printf("%c[%dm",ESC,color);   
fflush(stdout);
}
//设置背景颜色
void set_bg_color(int color)
{// ESC[#m   
printf("%c[%dm",ESC,(color+10));  
  fflush(stdout);
}




本文来自ChinaUnix博客,如果查看原文请点:http://blog.chinaunix.net/u3/104504/showart_2063924.html
您需要登录后才可以回帖 登录 | 注册

本版积分规则 发表回复

  

北京盛拓优讯信息技术有限公司. 版权所有 京ICP备16024965号-6 北京市公安局海淀分局网监中心备案编号:11010802020122 niuxiaotong@pcpop.com 17352615567
未成年举报专区
中国互联网协会会员  联系我们:huangweiwei@itpub.net
感谢所有关心和支持过ChinaUnix的朋友们 转载本站内容请注明原作者名及出处

清除 Cookies - ChinaUnix - Archiver - WAP - TOP