使用C和Visual Studio 2015的ANSI

没什么

我有一个学校项目,我们将使用c编写游戏代码到Zilog Z8 Encore板上。这是使用ANSI编写的,该板上具有控制台输出,该输出通过串行接口上​​的腻子进行读取。但是,使用Windows 10计算机,我希望能够模拟该程序,因此无需硬件即可测试代码。问题是,我尝试使用VS 2015,在此我使用stdio而不是硬件控制器,然后将其发送到控制台。但是Windows命令提示符不显示ANSI转义序列。因此,我尝试安装可以处理ANSI的conemu,但它不会更改背景颜色,只能更改前景。这是我要模拟的代码:

#include <string.h>
#include <stdio.h>

#define ESC 0x1B

void fgcolor(int foreground) {
    /*  Value      foreground     Value     foreground
    ------------------------------------------------
    0        Black            8       Dark Gray
    1        Red              9       Light Red
    2        Green           10       Light Green
    3        Brown           11       Yellow
    4        Blue            12       Light Blue
    5        Purple          13       Light Purple
    6        Cyan            14       Light Cyan
    7        Light Gray      15       White
    */
    int type = 22;             // normal text
    if (foreground > 7) {
        type = 1;                // bold text
        foreground -= 8;
    }
    printf("%c[%d;%dm", ESC, type, foreground + 30);
}

void bgcolor(int background) {
    /* IMPORTANT:   When you first use this function you cannot get back to true white background in HyperTerminal.
    Why is that? Because ANSI does not support true white background (ANSI white is gray to most human eyes).
    The designers of HyperTerminal, however, preferred black text on white background, which is why
    the colors are initially like that, but when the background color is first changed there is no
    way comming back.
    Hint:        Use resetbgcolor(); clrscr(); to force HyperTerminal into gray text on black background.

    Value      Color
    ------------------
    0        Black
    1        Red
    2        Green
    3        Brown
    4        Blue
    5        Purple
    6        Cyan
    7        Gray
    */
    printf("%c[%dm", ESC, background + 40);
}

void color(int foreground, int background) {
    // combination of fgcolor() and bgcolor() - uses less bandwidth
    int type = 22;             // normal text
    if (foreground > 7) {
        type = 1;                // bold text
        foreground -= 8;
    }
    printf("%c[%d;%d;%dm", ESC, type, foreground + 30, background + 40);
}

void resetbgcolor() {
    // gray on black text, no underline, no blink, no reverse
    printf("%c[m", ESC);
}

void clrscr() {
    printf("%c[2J", ESC);
}

void clreol() {
    printf("%c[K", ESC);
}
void gotoxy(int x, int y) {
    printf("%c[%d;%dH", ESC, y, x);
}

void underline(char on) {
    if (on == 'y') {
        printf("%c[4m", ESC);
    }
    else if (on == 'n') {
        printf("%c[24m", ESC);
    }
}

void blink(char on) {
    if (on == 'y') {
        printf("%c[5m", ESC);
    }
    else if (on == 'n') {
        printf("%c[25m", ESC);
    }
}

void reverse(char on) {
    if (on == 'y') {
        printf("%c[7m", ESC);
    }
    else if (on == 'n') {
        printf("%c[27m", ESC);
    }
}
void normal() {
    printf("%c[0;22m", ESC);
}


void window(int x1, int y1, int x2, int y2, char * c, int stil) {
    int length = strlen(c);
    int i = 0;
    char kanter[2][9] = { { 218,180,195,191,179,192,196,217 },{ 201,185,204,187,186,200,205,188 } };

    if (stil != 1) {
        stil = 0;
    }


    //color(2,5);
    gotoxy(x1, y1);
    printf("%c%c", kanter[stil][0], kanter[stil][1]);
    reverse('y');
    printf("  %s", c);
    gotoxy(x1 + 4 + length, y1);
    for (i = 0; i < x2 - (x1 + 5 + length); i++) {
        printf(" ");
    }
    reverse('n');
    printf("%c%c", kanter[stil][2], kanter[stil][3]);

    for (i = y1 + 1; i < y2; i++) {
        gotoxy(x1, i);
        printf("%c", kanter[stil][4]);
        gotoxy(x2, i);
        printf("%c", kanter[stil][4]);
    }

    gotoxy(x1, y2);
    printf("%c", kanter[stil][5]);
    for (i = 0; i < x2 - x1 - 1; i++) {
        printf("%c", kanter[stil][6]);
    }
    printf("%c\n", kanter[stil][7]);

    normal();
}
void up(int x) {
    printf("%c[%dA", ESC, x);
}

void down(int x) {
    printf("%c[%dB", ESC, x);
}

void right(int x) {
    printf("%c[%dC", ESC, x);
}
void left(int x) {
    printf("%c[%dD", ESC, x);
}
void main() {
    printf("hej");
    color(2, 0);
    clrscr();
    printf("\n");
    window(3, 4, 20, 15, "hej", 1);

    up(5);
    right(5);

    //  window(21, 12, 35, 30, "Farvel", 0);


    while (1 != 2) {};

}

此代码在控制台内部创建一个具有不同背景和前景色的窗口。

任何帮助表示赞赏。

注意到

Windows 10确实支持ANSI序列!只需从单独的命令提示符而不是从Visual Studio中启动.exe!Visual Studio打开的控制台窗口不支持ANSI,但是普通cmd.exe窗口(标准命令提示符)支持。

这样做的一个有用技巧是导航到您的.exe,而不是cmd在文件资源管理器窗口的地址栏中键入(然后按Enter)。它将打开一个控制台,该控制台已将当前目录设置为您在文件资源管理器中打开的目录,这非常方便。

本文收集自互联网,转载请注明来源。

如有侵权,请联系[email protected] 删除。

编辑于
0

我来说两句

0条评论
登录后参与评论

相关文章

来自分类Dev

如何在ANSI C中使用Visual Studio 2015编程(作为编辑器和编译器)?

来自分类Dev

Visual Studio 2015的C ++标准

来自分类Dev

Visual Studio 2015 C ++和程序集未生成

来自分类Dev

无法使用Entity Framework和Visual Studio 2015添加迁移

来自分类Dev

比较Visual Studio 2015和Blend for Visual Studio

来自分类Dev

Visual Studio 2015 和 Visual Studio 2017(社区版)+ Unity

来自分类Dev

Visual Studio 2015的AllMargins

来自分类Dev

与Visual Studio 2015的果酱

来自分类Dev

Visual Studio 2015

来自分类Dev

卸载Visual Studio 2015

来自分类Dev

Visual Studio 2015类库(我正在使用C#)

来自分类Dev

使用 Visual Studio 2015 的 C 预处理器

来自分类Dev

Visual Studio 2015和Android本机编程

来自分类Dev

Visual Studio 2015查找和替换

来自分类Dev

Visual Studio 2013和2015保存失败

来自分类Dev

如何使Visual Studio 2015 C ++项目与Visual Studio 2010兼容?

来自分类Dev

如何在Windows 10上使用Visual Studio 2015 x64配置和构建Tesseract OCR C ++

来自分类Dev

C ++错误C3646,C2059和C2238 Visual Studio 2015(社区)

来自分类Dev

Visual Studio 2015 C ++的助手

来自分类Dev

Visual Studio 2015中的C代码错误

来自分类Dev

从Visual Studio 2015外部使用gulp

来自分类Dev

使用TypeScript的Visual Studio 2015 Apache Cordova

来自分类Dev

使用 Visual Studio Shell (Isolated) 2015 编译

来自分类Dev

Visual Studio 2015 Express和Visual SourceSafe 2005

来自分类Dev

visual studio 2015和visual studio 2015实验实例之间有什么区别

来自分类常见问题

Visual Studio 2015非常慢

来自分类常见问题

Visual Studio 2015非常慢

来自分类Dev

Visual Studio 2015的项目模板

来自分类Dev

Visual Studio 2015的Git问题

Related 相关文章

热门标签

归档