c windows控制檯輸出顏色文字
阿新 • • 發佈:2018-11-05
#include <windows.h>
//設定文字顏色
void SetColor(int ForgC)
{
WORD wColor;
//We will need this handle to get the current background attribute
HANDLE hStdOut = GetStdHandle(STD_OUTPUT_HANDLE);
CONSOLE_SCREEN_BUFFER_INFO csbi;
//We use csbi for the wAttributes word.
if(GetConsoleScreenBufferInfo(hStdOut, &csbi))
{
//Mask out all but the background attribute, and add in the forgournd color
wColor = (csbi.wAttributes & 0xF0) + (ForgC & 0x0F);
SetConsoleTextAttribute(hStdOut, wColor);
}
}
0 - BLACK 黑 深色0 - 7
1 - BLUE 蘭 2 - GREEN 綠
3 - CYAN 青 4 - RED 紅
5 - MAGENTA 洋紅 6 - BROWN 棕
7 - LIGHTGRAY 淡灰 8 - DARKGRAY 深灰 淡色8 - 15
9 - LIGHTBLUE 淡蘭 10 - LIGHTGREEN 淡綠
11 - LIGHTCYAN 淡青 12 - LIGHTRED 淡紅
13 - LIGHTMAGENTA 淡洋紅 14 - YELLOW 黃
15 - WHITE 白
例子:
void print_char() { printf("Game\n"); printf("---------------------\n"); printf("\n|----|----|----|\n"); for(int i=0;i<3;i++) { printf("|"); for(int j=0;j<3;j++) { if (a[i][j]!=0) { if (randx == i && randy == j) { SetColor(4); printf("%3d",a[i][j]); SetColor(15); printf(" |"); } else { SetColor(15); printf("%3d |",a[i][j]); } } else { SetColor(15); printf(" |"); } } if(i<=3) { printf("\n|____|____|____|\n"); } } }