how to get current console background and text colors?

前端 未结 3 1356
半阙折子戏
半阙折子戏 2021-01-01 19:06

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.

3条回答
  •  庸人自扰
    2021-01-01 19:52

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

提交回复
热议问题