用C ++改变背景颜色

时间:2014-02-21 15:35:52

标签: c++ windows turbo-c++

如何在不进入turbo C ++的图形模式的情况下更改输出屏幕的背景颜色。我在windows xp中使用turbo C ++。任何帮助将不胜感激。

3 个答案:

答案 0 :(得分:2)

这是一些我以前没有用过的旧代码。欢迎你...

enum {White, Blue, Green, Red, Yellow, Cyan, Magenta, Black,
      Grey, DarkBlue, DarkGreen, DarkRed, DarkYellow, DarkCyan, DarkMagenta};

void ISC_Console::SetColor(int nForeground, int nBackground)
{
    ::SetConsoleTextAttribute(::GetStdHandle(STD_OUTPUT_HANDLE),
        MakeFColor(nForeground) | MakeBColor(nBackground));
}

int ISC_Console::MakeFColor(int nColor)
{
    int nVal = 0;
    switch (nColor) {
    case Grey:
    case White:
        nVal = FOREGROUND_RED | FOREGROUND_GREEN | FOREGROUND_BLUE;
        break;
    case Red:
    case DarkRed:
        nVal = FOREGROUND_RED;
        break;
    case Green:
    case DarkGreen:
        nVal = FOREGROUND_GREEN;
        break;
    case Blue:
    case DarkBlue:
        nVal = FOREGROUND_BLUE;
        break;
    case Yellow:
    case DarkYellow:
        nVal = FOREGROUND_RED | FOREGROUND_GREEN;
        break;
    case Cyan:
    case DarkCyan:
        nVal = FOREGROUND_GREEN | FOREGROUND_BLUE;
        break;
    case Magenta:
    case DarkMagenta:
        nVal = FOREGROUND_RED | FOREGROUND_BLUE;
        break;
    }
    if (nColor < Black) nVal |= FOREGROUND_INTENSITY;
    return nVal;
}

int ISC_Console::MakeBColor(int nColor)
{
        int nVal = 0;
    switch (nColor) {
    case Grey:
    case White:
        nVal = BACKGROUND_RED | BACKGROUND_GREEN | BACKGROUND_BLUE;
        break;
    case Red:
    case DarkRed:
        nVal = BACKGROUND_RED;
        break;
    case Green:
    case DarkGreen:
        nVal = BACKGROUND_GREEN;
        break;
    case Blue:
    case DarkBlue:
        nVal = BACKGROUND_BLUE;
        break;
    case Yellow:
    case DarkYellow:
        nVal = BACKGROUND_RED | BACKGROUND_GREEN;
        break;
    case Cyan:
    case DarkCyan:
        nVal = BACKGROUND_GREEN | BACKGROUND_BLUE;
        break;
    case Magenta:
    case DarkMagenta:
        nVal = BACKGROUND_RED | BACKGROUND_BLUE;
        break;
    }
    if (nColor < Black) nVal |= BACKGROUND_INTENSITY;
    return nVal;
}

答案 1 :(得分:0)

我认为您在输出文本时想要改变输出屏幕的背景。 您只需要包含conio.h头文件并使用内置函数textbackground(COLOR)。

答案 2 :(得分:0)

这是来自TC帮助...

#include <conio.h>

int main(void)
{
    int i, j;

    clrscr();
    for (i=0; i<9; i++)
    {
       for (j=0; j<80; j++)
          cprintf("C");
       cprintf("\r\n");
       textcolor(i+1);
       textbackground(i);
    }

    return 0;
}

textbackground()可以更改textmode控制台的背景。