利用ESC Code实现光标控制

本文介绍了如何通过ESC代码来实现对光标的精确控制,提供了相关资源下载链接。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

(转自www.lameck.cn)
说明:此方法适用于各种基于终端的操作,比如也可以利用些方法实现Makefile 或 shell脚本在输出时对光标的控制。。。
以前在编写linux程序时需要使光标在屏幕上移动公位置的功能,当时不知道怎么写,前几天又有同学问了同样的问题,这里把方法贴出来。
主要利用了ESC code,可参考:
http://isthe.com/chongo/tech/comp/ansi_escapes.html
这里只利用了ESC code中的总会跟光标移动相关的代码,还有其他一些功能没有写出来,比如 显示颜色 设置。。。这里就不写了,感兴趣的可以自己看看怎么来样。。。
实现代码,包含两个文件:
 //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

//清屏
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_
另附test.c程序:

//cusor_ctrl.c
#include"cusor_ctrl.h"

int ESC = 27;

//清屏
void clear_screen(void)
{// ESC[2J
    printf("%c[2J",ESC);
    fflush(stdout);
}

//清除从光标位置到行末的内容
void clear_to_end(void)
{// ESC[K
    printf("%c[K",ESC); fflush(stdout);} //光标移动到(x,y)
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);
}

 

#include"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("/nsdfasdfasdf/n");
        sleep(1);
        set_bg_color(COLOR_YELLOW);
        set_fg_color(COLOR_GREEN);
        printf("sdfasdfasdf/n");
        cusor_set_pos();
        while(1);
    }
    return 0;
}
 
常用ANSI控制符
*[nA   光标上移n个位置!
*[nB   光标下移n个位置!
*[nC   光标左移n个位置!
*[nD   光标右移n个位置!
*[s    保存光标位置
*[u    恢复光标位置
*[m;nH 光标移动到绝对坐标(m,n)处
*[2J   清屏,光标移动到(0,0)处
*[K    删除从光标处开始到行末的所以字符
*[0m   恢复系统显示背景,前景色
*[1m   高亮显示字符 低亮高亮
*[4m   下划线
*[5m   闪烁字符!   闪烁
*[7m   反转显示   反转显示
*[30m ---- *[37m 各种不同的前景色
       30    Black        31    Red
      32    Green        33    Yellow
      34    Blue         35    Magenta
      36    Cyan         37    White
*[40m ---- *[47m 各种不同的背景色
       40    Black        41    Red
      42    Green        43    Yellow
      44    Blue         45    Magenta
      46    Cyan         47    White

CODE:  http://www.lameck.cn/upfile/2009427/20094276482015989.zip

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值