windows 如何获取当前的控制台背景和文本颜色?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/8578909/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-15 18:42:20  来源:igfitidea点击:

how to get current console background and text colors?

c++windowscolorsconsole

提问by rsk82

I know how to set them (SetConsoleTextAttribute) but there isn't a GetConsoleTextAttribute to retrieve this information. On a unaffected console it should be int 7.

我知道如何设置它们 (SetConsoleTextAttribute) 但没有 GetConsoleTextAttribute 来检索此信息。在未受影响的控制台上,它应该是 int 7。

The problem is that when exiting from a program that sets the text color it stays the same for the time given window runs, and I cannot assume that user hasn't set the colors to his custom liking.

问题是,当退出设置文本颜色的程序时,它在给定窗口运行的时间内保持不变,我不能假设用户没有根据他的自定义喜好设置颜色。

采纳答案by Raymond Chen

A quick grep of wincon.hshows that CONSOLE_SCREEN_BUFFER_INFOhas a wAttributesmember which is documented as"The attributes of the characters written to a screen buffer by the WriteFile and WriteConsole functions, or echoed to a screen buffer by the ReadFile and ReadConsole functions." This matches the description of SetConsoleTextAttribute: "Sets the attributes of characters written to the console screen buffer by the WriteFile or WriteConsole function, or echoed by the ReadFile or ReadConsole function." The structure is returned by GetConsoleScreenBufferInfo.

一个快速的 grepwincon.h显示,其中CONSOLE_SCREEN_BUFFER_INFO有一个wAttributes成员被记录为“由 WriteFile 和 WriteConsole 函数写入屏幕缓冲区的字符属性,或由 ReadFile 和 ReadConsole 函数回显到屏幕缓冲区的字符属性。” 这符合以下描述SetConsoleTextAttribute:“设置由 WriteFile 或 WriteConsole 函数写入控制台屏幕缓冲区的字符的属性,或由 ReadFile 或 ReadConsole 函数回显的字符的属性。” 该结构由 返回GetConsoleScreenBufferInfo

回答by Piotr Syrokomski

Thanks to Talent25 I made this function:

感谢 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;
}

using it:

使用它:

GetColor(CurrentColor);

CurrentColor - variable for output number of color (background * 16 + main color). Returned value informs if action was successful.

CurrentColor - 颜色输出数量的变量(背景 * 16 + 主色)。返回值通知操作是否成功。

回答by Maryam Shabani

Here is code snippet.

这是代码片段。

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);