2011-12-20 13 views
6

Wiem, jak je ustawić (SetConsoleTextAttribute), ale nie ma GetConsoleTextAttribute, aby pobrać te informacje. Na nienaruszonej konsoli powinien być int 7.jak uzyskać aktualne tło pulpitu i kolory tekstu?

Problem polega na tym, że podczas wychodzenia z programu, który ustawia kolor tekstu, pozostaje taki sam dla danego czasu uruchomionych okien i nie mogę założyć, że użytkownik nie ustawił kolory do jego własnych upodobań.

Odpowiedz

4

Szybkie grep z wincon.h pokazuje, że CONSOLE_SCREEN_BUFFER_INFO ma wAttributes członka, który jest documented as „Atrybuty znaków pisanych do bufora ekranu przez funkcje WriteFile i WriteConsole lub echem do bufora ekranu przez funkcje readfile i ReadConsole. " To pasuje do the description of SetConsoleTextAttribute: "Ustawia atrybuty znaków zapisanych w buforze ekranu konsoli przez funkcję WriteFile lub WriteConsole lub jest odbijane przez funkcję ReadFile lub ReadConsole." Struktura jest zwracana przez GetConsoleScreenBufferInfo.

2

Oto fragment kodu.

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

mam nadzieję, że to pomoże.

6

Dzięki Talent25 Zrobiłem tę funkcję:

#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; 
} 

używając go:

GetColor(CurrentColor); 

currentcolor - zmienna liczba wyjściowego koloru tła (* 16 + główny kolor). Zwrócona wartość informuje, czy akcja zakończyła się powodzeniem.

Powiązane problemy