ANSI转义码

windows 里如果不支持,可先使能

reg add "HKCU\Console" /v VirtualTerminalLevel /t REG_DWORD /d 1
#include <stdio.h>

int main() {
    // Move cursor to row 5, column 10
    printf("\033[50;10H");
    printf("Hello, World!");
}

在这里插
入图片描述
在这里插入图片描述
在这里插入图片描述
在这里插入图片描述
在这里插入图片描述

在这里插入图片描述
在这里插入图片描述
在这里插入图片描述
在这里插入图片描述

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

// Function to set background color
void setBackgroundColor(const char* color) {
    if (strcmp(color, "black") == 0) printf("\033[40m");
    else if (strcmp(color, "red") == 0) printf("\033[41m");
    else if (strcmp(color, "green") == 0) printf("\033[42m");
    else if (strcmp(color, "yellow") == 0) printf("\033[43m");
    else if (strcmp(color, "blue") == 0) printf("\033[44m");
    else if (strcmp(color, "magenta") == 0) printf("\033[45m");
    else if (strcmp(color, "cyan") == 0) printf("\033[46m");
    else if (strcmp(color, "white") == 0) printf("\033[47m");
    else if (strcmp(color, "reset") == 0) printf("\033[0m");
    else printf("\033[0m"); // Default to reset if unknown color
}

// Function to set foreground color (text color)
void setForegroundColor(const char* color) {
    if (strcmp(color, "black") == 0) printf("\033[30m");
    else if (strcmp(color, "red") == 0) printf("\033[31m");
    else if (strcmp(color, "green") == 0) printf("\033[32m");
    else if (strcmp(color, "yellow") == 0) printf("\033[33m");
    else if (strcmp(color, "blue") == 0) printf("\033[34m");
    else if (strcmp(color, "magenta") == 0) printf("\033[35m");
    else if (strcmp(color, "cyan") == 0) printf("\033[36m");
    else if (strcmp(color, "white") == 0) printf("\033[37m");
    else if (strcmp(color, "reset") == 0) printf("\033[0m");
    else printf("\033[0m"); // Default to reset if unknown color
}

// Function to set font color (alias for setForegroundColor)
void setFontColor(const char* color) {
    setForegroundColor(color);
}

// Function to set cursor position
void setCursorPos(int x, int y) {
    printf("\033[%d;%dH", y, x);
}

// Function to clear a specific area on the screen
void clearArea(int start_row, int start_col, int end_row, int end_col) {
    for (int row = start_row; row <= end_row; row++) {
        setCursorPos(start_col, row);
        for (int col = start_col; col <= end_col; col++) {
            printf(" ");
        }
    }
}

// Function to clear the whole screen
void cls() {
    printf("\033[2J"); // Clear screen
    printf("\033[H");  // Move cursor to home position (1,1)
}

// Function to show or hide the cursor
void showCursor(int show) {
    if (show) {
        printf("\033[?25h"); // Show cursor
    } else {
        printf("\033[?25l"); // Hide cursor
    }
}

int main() {
    // Example usage
    setForegroundColor("red");
    setBackgroundColor("yellow");
    printf("Hello, World!\n");

    setCursorPos(10, 5);
    printf("This is at position (10, 5)\n");

    clearArea(1, 1, 5, 20);
    cls();

    showCursor(0); // Hide cursor
    printf("Cursor is hidden.\n");
    showCursor(1); // Show cursor
    printf("Cursor is visible.\n");

    setFontColor("green");
    printf("This text is green.\n");

    setBackgroundColor("reset");
    setForegroundColor("reset");
    printf("Colors are reset to default.\n");

    return 0;
}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值