+ -
当前位置:首页 → 问答吧 → C/C++控制台输出时设置字体和背景颜色

C/C++控制台输出时设置字体和背景颜色

时间:2011-12-21

来源:互联网

1.改变整个控制台的颜色
用 system("color 0A"); 
其中color后面的0是背景色代号,A是前景色代号。各颜色代码如下: 
0=黑色 
1=蓝色 
2=绿色 
3=湖蓝色 
4=红色 
5=紫色 
6=黄色 
7=白色 
8=灰色 
9=淡蓝色 
A=淡绿色 
B=淡浅绿色 
C=淡红色 
D=淡紫色 
E=淡黄色 
F=亮白色 
[b][/b][color=#FF0000][/color][size=24px][/size]

作者: satan__   发布时间: 2011-12-21

你不是都实现了啊
system("color 0A");

作者: agoago_2009   发布时间: 2011-12-21

试试以下C++程序:
C/C++ code

#include <iostream>
#include <windows.h>

int main()
{
    const WORD colors[] =
        {
        0x1A, 0x2B, 0x3C, 0x4D, 0x5E, 0x6F,
        0xA1, 0xB2, 0xC3, 0xD4, 0xE5, 0xF6
        };

    HANDLE hstdin  = GetStdHandle( STD_INPUT_HANDLE  );
    HANDLE hstdout = GetStdHandle( STD_OUTPUT_HANDLE );
    WORD   index   = 0;

    // Remember how things were when we started
    CONSOLE_SCREEN_BUFFER_INFO csbi;
    GetConsoleScreenBufferInfo( hstdout, &csbi );

    // Tell the user how to stop
    SetConsoleTextAttribute( hstdout, 0xEC );
    std::cout << "Press any key to quit.\n";

    // Draw pretty colors until the user presses any key
    while (WaitForSingleObject( hstdin, 100 ) == WAIT_TIMEOUT)
    {
        SetConsoleTextAttribute( hstdout, colors[ index ] );
        std::cout << "\t\t\t\t Hello World \t\t\t\t" << std::endl;
        if (++index > sizeof(colors)/sizeof(colors[0]))
            index = 0;
    }
    FlushConsoleInputBuffer( hstdin );

    // Keep users happy
    SetConsoleTextAttribute( hstdout, csbi.wAttributes );
    return 0;
}


作者: keiy   发布时间: 2011-12-21