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;
}