繁体   English   中英

更改控制台颜色并将输出保留为C ++

[英]Change console colors and keep the output in C++

我需要制作类似于Window的CMD的CLI。 为了执行颜色命令,我使用了rlutil::setColorrlutil::setBackgroundColor 函数 但是,要更改所有控制台中的颜色,我必须清除屏幕( rlutil::cls() ),否则只有新的输出会与图像中的更改一起出现。

没有cls:

没有cls

与cls: 命令之前 在第一个命令之后-颜色af

在cmd中(我使用@echo off来不显示当前目录): cmd中使用的相同代码

这是我做的功能:

void colors(string value) {//I recive the user's input (like in the cmd)
    char foo[3];//I save each character in this array
    int c_text = 0, c_bg = 0;//Variables to get the numeric value of each character
    if(value.length() == 2) {//This is to only accept 2 characters as parameter for the command
        strcpy(foo, value.c_str());//Copy the values of the string in the array
        c_bg= chartoHEX(foo[0]);//Take the int value of each character 
        //(if the parameter in chartoHEX is '0', returns 0, if it's 'A', returns 10, and so on)
        c_text = chartoHEX(foo[1]);
        //If the function returns -1 means that the parameter wasn't an HEX number
        if(c_text != -1 && c_bg != -1) {
            rlutil::setColor(c_text);//Changes the text color
            rlutil::setBackgroundColor(c_bg);//Changes the background color
        }
    }
}

当我调用该函数时:

colors("0a");
rlutil::cls();
cout << "C:\\Users\\Raven>";

更改颜色后如何保持输出?

如果使用低级本机Windows控制台功能,则可以更改颜色而不影响文本。 获取与控制台柄GetStdHandle_get_osfhandle ,然后调用WriteConsoleOutputAttribute

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM