how to get current console background and text colors?

前端 未结 3 1351
半阙折子戏
半阙折子戏 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

    Thanks to Talent25 I made this function:

    #include     
    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.

提交回复
热议问题