簡體   English   中英

使用C和Visual Studio 2015的ANSI

[英]ANSI using C and Visual Studio 2015

我有一個學校項目,我們將使用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)。 它將打開一個控制台,該控制台已將當前目錄設置為您在文件資源管理器中打開的目錄,這非常方便。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM