我知道如何设置它们(SetConsoleTextAttribute),但是没有GetConsoleTextAttribute来检索这些信息。 在不受影响的控制台上应该是int 7。
问题是,从设置文本颜色的程序退出时,窗口运行的时间保持不变,我不能假定用户没有将颜色设置为自定义喜好。
一个快速的wincon.h
grep显示, CONSOLE_SCREEN_BUFFER_INFO
有一个wAttributes
成员, wAttributes
成员被记录为 “由WriteFile和WriteConsole函数写入屏幕缓冲区的字符的属性,或者由ReadFile和ReadConsole函数回显到屏幕缓冲区。 这与SetConsoleTextAttribute
的描述相匹配:“通过WriteFile或WriteConsole函数设置写入控制台屏幕缓冲区的字符的属性,或由ReadFile或ReadConsole函数响应。 该结构由GetConsoleScreenBufferInfo
返回。
感谢Talent25我做了这个功能:
#include <Windows.h> bool GetColor(short &ret){ CONSOLE_SCREEN_BUFFER_INFO info; if (!GetConsoleScreenBufferInfo(GetStdHandle(STD_OUTPUT_HANDLE), &info)) return false; ret = info.wAttributes; return true; }
使用它:
GetColor(CurrentColor);
CurrentColor – 颜色输出数量的变量(背景* 16 +主色)。 如果操作成功,则返回值通知。
这是代码片段。
HANDLE m_hConsole; WORD m_currentConsoleAttr; CONSOLE_SCREEN_BUFFER_INFO csbi; //retrieve and save the current attributes m_hConsole=GetStdHandle(STD_OUTPUT_HANDLE); if(GetConsoleScreenBufferInfo(m_hConsole, &csbi)) m_currentConsoleAttr = csbi.wAttributes; //change the attribute to what you like SetConsoleTextAttribute ( m_hConsole, FOREGROUND_RED | FOREGROUND_GREEN); //set the ttribute to the original one SetConsoleTextAttribute ( m_hConsole, m_currentConsoleAttr);
希望这是有帮助的。