C++:通过 clrscr() 使用背景颜色
C++ : Using background colors with clrscr()
我目前正在制作一个简单的游戏。初始屏幕是欢迎屏幕,颜色如下:
system("color f3")//background:white , text:aqua
然后当我从 main()
函数中调用以下内容时
void display()
{
Sleep(2000);
clrscr();
system("color f3");
cout<<"Levels:\n\n";
int d;
cout<<"1.Easy\n";
cout<<"2.Medium\n";
cout<<"3.Hard\n";
cout<<"4.Insane!\n";
cout<<"Choose your difficulty:";
cin>>d;
}
在我的 display()
中没有语句 system("color f3");
背景为黑色,文本以白色突出显示,文本颜色为浅绿色。
我想知道为什么会出现上述情况。
问题:
当 clrscr()
被调用时,使用语句 system("color f3");
,屏幕变黑几毫秒,然后变成白色和浅绿色。
那么如何防止黑屏那几毫秒呢?
感谢您的帮助:)
当您调用 system()
时,您在另一个进程中启动命令处理器,这会更改屏幕设置。
当您稍后调用 clrscr()
时,您的库清除会使用它在启动时存储的自己的颜色来清除屏幕。这就是您遇到问题的原因。
您可以直接使用 windows console API, for example the function SetConsoleTextAttribute()
:
#include <windows.h>
...
SetConsoleTextAttribute(GetStdHandle(STD_OUTPUT_HANDLE),
BACKGROUND_INTENSITY|FOREGROUND_BLUE);
注:颜色和强度可以根据需要与|
组合。在你的情况下你可以简单地写 0xf3
顺便说一句, 展示了其他本机 windows 控制台 API 可能感兴趣的功能。
调用 system() 函数会强制终端启动外部进程。 system() 调用可能不是您要找的。尝试 windows 上的 conio.h 库中的一些函数或 linux 中的 ncurses 包。它们具有更好的更改文本和颜色的功能。
由于您正在制作游戏,所以您希望在游戏中编写自己的函数,并且
不必打电话给 SYSTEM( )
。其实没那么难,下面是示例代码。
#include <windows.h>
#include <iostream>
using namespace std;
void gotoxy(int x, int y);
void setcolor(WORD color);
void setForeGroundAndBackGroundColor(int ForeGroundColor,int BackGroundColor);
void clrbox(unsigned char x1,unsigned char y1,unsigned char x2,unsigned char y2,unsigned char bkcol);
void putbox(unsigned x,unsigned y,unsigned sx,unsigned sy,unsigned char col, unsigned char col2,unsigned char bkcol,char text_[]);
void box(unsigned x,unsigned y,unsigned sx,unsigned sy,unsigned char col,unsigned char col2,char text_[]);
void clrscr();
int main()
{
setcolor(31);
clrbox(1,1,79,23,33);
gotoxy(10,12);
setForeGroundAndBackGroundColor(2,14);
cout<<" Hello world ";
setcolor(7);
gotoxy(1,23);
return 0;
}
void gotoxy(int x, int y)
{
COORD coord;
coord.X = x; coord.Y = y;
SetConsoleCursorPosition(GetStdHandle(STD_OUTPUT_HANDLE), coord);
return;
}
void setcolor(WORD color)
{
SetConsoleTextAttribute(GetStdHandle(STD_OUTPUT_HANDLE),color);
return;
}
//
// colors:
// 0 = Black
// 1 = Blue
// 2 = Green
// 3 = Cyan
// 4 = Red
// 5 = Magenta
// 6 = Yellow
// 7 = LightGray
// 8 = DarkGray
// 9 = LightBlue
// 10 = LightGreen
// 11 = LightCyan
// 12 = LightRed
// 13 = LightMagenta
// 14 = LightYellow
// 15 = White
void setForeGroundAndBackGroundColor(int ForeGroundColor,int BackGroundColor)
{
int color=16*BackGroundColor+ForeGroundColor;
setcolor(color);
}
void clrscr()
{
COORD coordScreen = { 0, 0 };
DWORD cCharsWritten;
CONSOLE_SCREEN_BUFFER_INFO csbi;
DWORD dwConSize;
HANDLE hConsole = GetStdHandle(STD_OUTPUT_HANDLE);
GetConsoleScreenBufferInfo(hConsole, &csbi);
dwConSize = csbi.dwSize.X * csbi.dwSize.Y;
FillConsoleOutputCharacter(hConsole, TEXT(' '), dwConSize, coordScreen, &cCharsWritten);
GetConsoleScreenBufferInfo(hConsole, &csbi);
FillConsoleOutputAttribute(hConsole, csbi.wAttributes, dwConSize, coordScreen, &cCharsWritten);
SetConsoleCursorPosition(hConsole, coordScreen);
return;
}
void clrbox(unsigned char x1,unsigned char y1,unsigned char x2,unsigned char y2,unsigned char bkcol)
{
int x,y;
setcolor(bkcol); //Set to color bkcol
for (y=y1;y<y2;y++) //Fill Y Region Loop
{
for (x=x1;x<x2;x++) //Fill X region Loop
{
gotoxy(x,y);cout<<" "; //Draw Solid space
}
}
}
void box(unsigned x,unsigned y,unsigned sx,unsigned sy,unsigned char col,unsigned char col2,char text_[])
{ unsigned i,j,m;
{
m=(sx-x); //differential
j=m/8; //adjust
j=j-1; //more adjustment
gotoxy(x,y);cout<<"É"; //Top left corner of box
gotoxy(sx,y);cout<<"»"; //Top right corner of box
gotoxy(x,sy);cout<<"È"; //Bottom left corner of box
gotoxy(sx,sy);cout<<"¼"; //Bottom right corner of box
for (i=x+1;i<sx;i++)
{
gotoxy(i,y);cout<<"Í"; // Top horizontol line
gotoxy(i,sy);cout<<"Í"; // Bottom Horizontal line
}
for (i=y+1;i<sy;i++)
{
gotoxy(x,i);cout<<"º"; //Left Vertical line
gotoxy(sx,i);cout<<"º"; //Right Vertical Line
}
gotoxy(x+j,y);cout<<text_; //put Title
gotoxy(1,24);
}
}
void putbox(unsigned x,unsigned y,unsigned sx,unsigned sy,
unsigned char col, unsigned char col2,unsigned char bkcol,char text_[])
{
clrbox(x,y,sx,sy,bkcol);
box(x,y,sx,sy,col,col2,text_);
}
我目前正在制作一个简单的游戏。初始屏幕是欢迎屏幕,颜色如下:
system("color f3")//background:white , text:aqua
然后当我从 main()
函数中调用以下内容时
void display()
{
Sleep(2000);
clrscr();
system("color f3");
cout<<"Levels:\n\n";
int d;
cout<<"1.Easy\n";
cout<<"2.Medium\n";
cout<<"3.Hard\n";
cout<<"4.Insane!\n";
cout<<"Choose your difficulty:";
cin>>d;
}
在我的 display()
中没有语句 system("color f3");
背景为黑色,文本以白色突出显示,文本颜色为浅绿色。
我想知道为什么会出现上述情况。
问题:
当 clrscr()
被调用时,使用语句 system("color f3");
,屏幕变黑几毫秒,然后变成白色和浅绿色。
那么如何防止黑屏那几毫秒呢?
感谢您的帮助:)
当您调用 system()
时,您在另一个进程中启动命令处理器,这会更改屏幕设置。
当您稍后调用 clrscr()
时,您的库清除会使用它在启动时存储的自己的颜色来清除屏幕。这就是您遇到问题的原因。
您可以直接使用 windows console API, for example the function SetConsoleTextAttribute()
:
#include <windows.h>
...
SetConsoleTextAttribute(GetStdHandle(STD_OUTPUT_HANDLE),
BACKGROUND_INTENSITY|FOREGROUND_BLUE);
注:颜色和强度可以根据需要与|
组合。在你的情况下你可以简单地写 0xf3
顺便说一句,
调用 system() 函数会强制终端启动外部进程。 system() 调用可能不是您要找的。尝试 windows 上的 conio.h 库中的一些函数或 linux 中的 ncurses 包。它们具有更好的更改文本和颜色的功能。
由于您正在制作游戏,所以您希望在游戏中编写自己的函数,并且
不必打电话给 SYSTEM( )
。其实没那么难,下面是示例代码。
#include <windows.h>
#include <iostream>
using namespace std;
void gotoxy(int x, int y);
void setcolor(WORD color);
void setForeGroundAndBackGroundColor(int ForeGroundColor,int BackGroundColor);
void clrbox(unsigned char x1,unsigned char y1,unsigned char x2,unsigned char y2,unsigned char bkcol);
void putbox(unsigned x,unsigned y,unsigned sx,unsigned sy,unsigned char col, unsigned char col2,unsigned char bkcol,char text_[]);
void box(unsigned x,unsigned y,unsigned sx,unsigned sy,unsigned char col,unsigned char col2,char text_[]);
void clrscr();
int main()
{
setcolor(31);
clrbox(1,1,79,23,33);
gotoxy(10,12);
setForeGroundAndBackGroundColor(2,14);
cout<<" Hello world ";
setcolor(7);
gotoxy(1,23);
return 0;
}
void gotoxy(int x, int y)
{
COORD coord;
coord.X = x; coord.Y = y;
SetConsoleCursorPosition(GetStdHandle(STD_OUTPUT_HANDLE), coord);
return;
}
void setcolor(WORD color)
{
SetConsoleTextAttribute(GetStdHandle(STD_OUTPUT_HANDLE),color);
return;
}
//
// colors:
// 0 = Black
// 1 = Blue
// 2 = Green
// 3 = Cyan
// 4 = Red
// 5 = Magenta
// 6 = Yellow
// 7 = LightGray
// 8 = DarkGray
// 9 = LightBlue
// 10 = LightGreen
// 11 = LightCyan
// 12 = LightRed
// 13 = LightMagenta
// 14 = LightYellow
// 15 = White
void setForeGroundAndBackGroundColor(int ForeGroundColor,int BackGroundColor)
{
int color=16*BackGroundColor+ForeGroundColor;
setcolor(color);
}
void clrscr()
{
COORD coordScreen = { 0, 0 };
DWORD cCharsWritten;
CONSOLE_SCREEN_BUFFER_INFO csbi;
DWORD dwConSize;
HANDLE hConsole = GetStdHandle(STD_OUTPUT_HANDLE);
GetConsoleScreenBufferInfo(hConsole, &csbi);
dwConSize = csbi.dwSize.X * csbi.dwSize.Y;
FillConsoleOutputCharacter(hConsole, TEXT(' '), dwConSize, coordScreen, &cCharsWritten);
GetConsoleScreenBufferInfo(hConsole, &csbi);
FillConsoleOutputAttribute(hConsole, csbi.wAttributes, dwConSize, coordScreen, &cCharsWritten);
SetConsoleCursorPosition(hConsole, coordScreen);
return;
}
void clrbox(unsigned char x1,unsigned char y1,unsigned char x2,unsigned char y2,unsigned char bkcol)
{
int x,y;
setcolor(bkcol); //Set to color bkcol
for (y=y1;y<y2;y++) //Fill Y Region Loop
{
for (x=x1;x<x2;x++) //Fill X region Loop
{
gotoxy(x,y);cout<<" "; //Draw Solid space
}
}
}
void box(unsigned x,unsigned y,unsigned sx,unsigned sy,unsigned char col,unsigned char col2,char text_[])
{ unsigned i,j,m;
{
m=(sx-x); //differential
j=m/8; //adjust
j=j-1; //more adjustment
gotoxy(x,y);cout<<"É"; //Top left corner of box
gotoxy(sx,y);cout<<"»"; //Top right corner of box
gotoxy(x,sy);cout<<"È"; //Bottom left corner of box
gotoxy(sx,sy);cout<<"¼"; //Bottom right corner of box
for (i=x+1;i<sx;i++)
{
gotoxy(i,y);cout<<"Í"; // Top horizontol line
gotoxy(i,sy);cout<<"Í"; // Bottom Horizontal line
}
for (i=y+1;i<sy;i++)
{
gotoxy(x,i);cout<<"º"; //Left Vertical line
gotoxy(sx,i);cout<<"º"; //Right Vertical Line
}
gotoxy(x+j,y);cout<<text_; //put Title
gotoxy(1,24);
}
}
void putbox(unsigned x,unsigned y,unsigned sx,unsigned sy,
unsigned char col, unsigned char col2,unsigned char bkcol,char text_[])
{
clrbox(x,y,sx,sy,bkcol);
box(x,y,sx,sy,col,col2,text_);
}