0

我使用的是 TurboC++,我已更改为适用于 Windows 桌面的 Visual Studio Express 2012,我什至尝试了 Dev C++。我在两个较新的编译器中都遇到错误,但在 turbo C++ 中没有

struct date d;
sleep(3);

我想知道这些功能的替代品

进一步改变背景和前景色,我被告知使用

SetConsoleTextAttribute(GetStdHandle(STD_OUTPUT_HANDLE), FOREGROUND_GREEN);
SetConsoleTextAttribute(GetStdHandle(STD_OUTPUT_HANDLE), BACKGROUND_RED);

这些工作非常好,但事实是它只有有限数量的颜色(我想每种 3 种)就像,我不能将 textcolor 更改为白色!怎么做??

4

1 回答 1

0

您可以定义新颜色并将其用作现有颜色

#define FOREGROUND_WHITE (FOREGROUND_RED | FOREGROUND_BLUE | FOREGROUND_GREEN)  

下面的代码将
根据结果打印前景色和背景色的不同组合,并根据需要传递值。

for(i=0; i<255; i++)
{
SetConsoleTextAttribute(GetStdHandle(STD_OUTPUT_HANDLE), i);
printf("NEW COLOUR=%d\n",i);
}  

编辑

#include<stdio.h>
#include<windows.h>
/*
//0-black
//1-blue
//2-green
//  ......
//15-white



//0-15-black back ground, text colour as above 0-black,15-white
//16-31blue back ground,  text colour as above 16+0 -black 16+15-white
//32-47green back ground, text colour as above 32+0 -black 32+15-white
// ......
//240-white back ground,  text colour as above 240+0 -black 240+15-white
*/


int main()
{
int i;

    for(i=0; i<256; i++) //loop to print text colours 0-15 total 16 colours
    {
        // Sleep( 3000 );   // sleep three seconds
        SetConsoleTextAttribute(GetStdHandle(STD_OUTPUT_HANDLE), i);
        printf("Text colour=%d\n",i);
    }
    system("pause");


    for(i=0; i<256; i=i+8) //background colours 0-15 total 16 back grounds
    {
        //    Sleep( 3000 );   // sleep three seconds
        SetConsoleTextAttribute(GetStdHandle(STD_OUTPUT_HANDLE), i);
        printf("Back ground=%d\n",i);
    }
    system("pause");
    SetConsoleTextAttribute(GetStdHandle(STD_OUTPUT_HANDLE), 0+15);  //white text +black background

    system("pause");

}
于 2013-10-01T13:57:22.107 回答