C ++:将背景色与clrscr()一起使用 [英] C++ : Using background colors with clrscr()

查看:57
本文介绍了C ++:将背景色与clrscr()一起使用的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我目前正在制作一个简单的游戏.初始屏幕是欢迎屏幕,其中包含以下颜色:

I am currently making a simple game. The initial screen is the welcome screen with the following for color:

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"); ,背景为黑色,文本突出显示为白色,文本颜色为浅绿色

without the statement system("color f3"); in my display() the background is black, the text gets highlighted in white and the text color is aqua.

我想知道为什么会发生这种情况.

I want to know why the above happens.

问题:

使用语句 system("color f3"); 时,调用 clrscr()时,屏幕会变黑几毫秒,然后变成白色,水色.

with the statement system("color f3"); when the clrscr() is invoked, the screen turns black for a few milliseconds and then turns to white and aqua.

那么如何在几毫秒内防止屏幕变黑?

So how to prevent the screen to turn black for those few milliseconds?

感谢您的所有帮助:)

推荐答案

调用

When you call system(), you lauch a command processor in another process, which changes the screen settings.

以后再调用 clrscr()时,库清除使用其在启动时存储的自己的颜色来清除屏幕.这就是您遇到问题的原因.

When you later call clrscr() your library clears uses its own colors that it stored at startup to clear the screen. THis is why you experience the problem.

您可以改为直接使用 SetConsoleTextAttribute() :

You could instead use directly windows console API, for example the function SetConsoleTextAttribute():

#include <windows.h>
...
   SetConsoleTextAttribute(GetStdHandle(STD_OUTPUT_HANDLE),
                        BACKGROUND_INTENSITY|FOREGROUND_BLUE); 

注意:颜色和强度可以根据需要与 | 组合.就您而言,您只需编写0xf3

Note: The colors and intensity can be combined as needed with |. In your case you could simply write 0xf3

顺便说一句,此SO问题显示可能感兴趣的其他本机Windows控制台API函数.

By the way, this SO question shows other native windows console API functions that could be of interest.

这篇关于C ++:将背景色与clrscr()一起使用的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆