c windows控制台输出颜色文字

#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");
        }
    }
}

  

猜你喜欢

转载自www.cnblogs.com/wangjian8888/p/9907897.html