【发布时间】:2017-04-17 22:41:40
【问题描述】:
我需要制作一个类似于 Window 的 CMD 的 CLI。为了制作颜色命令,我在rlutil.h 中使用了rlutil::setColor 和rlutil::setBackgroundColor 函数。但是,要更改所有控制台中的颜色,我必须清除屏幕 (rlutil::cls()),否则只有新的输出才会出现这些更改,如图所示。
没有 cls:
这是我做的函数:
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>";
更改颜色后如何保持输出?
【问题讨论】: