Konsolen Farbe ändern
-
Hallo
als kleine Übung habe ich ein Lottoprogramm geschrieben welches Zufallszahlen erzeugt.
Ich will allerdings bestimmten Zahlen andere Farben geben.
Die Anwendung läuft über ein Konsolenfenster.Ich habe mir sowas wie:
... system("color F4"); cout<<"\t[X], "; system("color 0F"); ...
Überlegt, so dass er quasi die Farbe vor dem cout ändert und nach dem cout wieder in den Normalzustand versetzt. Allerdings ändert sich die Farbe wieder mit.
MfG
-
GetStdHandle
Das sollte reichen.
-
Mit Standard C++ Mitteln hast du das keine Chance.
Auf Windows kannst du auf die Console Functions zurückgreifen. Interessant dürfte für dich vor allem SetConsoleTextAttribute sein. Unter anderen Betriebsystemen habe ich keine Ahnung, wie man dies machen könnte.
Grüssli
-
Kóyaánasqatsi schrieb:
GetStdHandle
Das sollte reichen.
Welche inlclude Dateien benötige ich?
Und wie müsste ich das im Vergleich zum Code im ersten Post einfügen ?
lg
-
#include <windows.h> #include <iostream> void ColorCout(int color, std::string text) { HANDLE handle = GetStdHandle(STD_OUTPUT_HANDLE); SetConsoleTextAttribute(handle, color); std::cout << text; }
Edit: Editieren mit Lag ist scheiße ...
Am besten wäre es wenn du vielleicht dann noch ein enum dafür nutzt, wäre übersichtlicher und besser
enum Colors { hblue, grey }; ... ColorCout(hblue, "hallo");
Etc...
-
FreakY<3Cpp schrieb:
#include <windows.h> #include <iostream> void ColorCout(int color, std::string text) { HANDLE handle = GetStdHandle(STD_OUTPUT_HANDLE); SetConsoleTextAttribute(handle, color); std::cout << text; }
void ColorCout(const WORD color, const std::string text) { HANDLE handle = GetStdHandle(STD_OUTPUT_HANDLE); SetConsoleTextAttribute(handle, color); std::cout << text; }
enum { FOREGROUND_* = 1, //1: Blau (dunkel) FOREGROUND_*, //2: Grün (dunkel) FOREGROUND_*, //3: Turkeyse (dunkel) FOREGROUND_*, //4: Rot (dunkel) FOREGROUND_*, //5: Violett (dunkel) FOREGROUND_*, //6: Gelb (dunkel) FOREGROUND_*, //7: Weiß (dunkel) FOREGROUND_GREY, //8: Grau FOREGROUND_BLUE, //9: Blau (hell) FOREGROUND_GREEN, //10: Grün (hell) FOREGROUND_*, //11: Turkeyse (hell) FOREGROUND_RED, //12: Rot (hell) FOREGROUND_*, //13: Violett (hell) FOREGROUND_YELLOW, //14: Gelb (hell) FOREGROUND_WHITE //15: Weiß (hell) };
-
FreakY<3Cpp schrieb:
#include <windows.h> #include <iostream> void ColorCout(int color, std::string text) { HANDLE handle = GetStdHandle(STD_OUTPUT_HANDLE); SetConsoleTextAttribute(handle, color); std::cout << text; }
Edit: Editieren mit Lag ist scheiße ...
Am besten wäre es wenn du vielleicht dann noch ein enum dafür nutzt, wäre übersichtlicher und besser
enum Colors { hblue, grey }; ... ColorCout(hblue, "hallo");
Etc...
Sozusagen
#include <windows.h>
#include <iostream>
#include <conio.h>
using namespace std;void ColorCout(int color, std::string text)
{
HANDLE handle = GetStdHandle(STD_OUTPUT_HANDLE);
SetConsoleTextAttribute(handle, color);
std::cout << "X";
}int main ()
{
enum Colors { hblue, red };
ColorCout( hblue,"X");
getch();
}mhh komme nicht so klar :S
-
Kóyaánasqatsi schrieb:
FreakY<3Cpp schrieb:
#include <windows.h> #include <iostream> void ColorCout(int color, std::string text) { HANDLE handle = GetStdHandle(STD_OUTPUT_HANDLE); SetConsoleTextAttribute(handle, color); std::cout << text; }
void ColorCout(const WORD color, const std::string text) { HANDLE handle = GetStdHandle(STD_OUTPUT_HANDLE); SetConsoleTextAttribute(handle, color); std::cout << text; }
enum { FOREGROUND_* = 1, //1: Blau (dunkel) FOREGROUND_*, //2: Grün (dunkel) FOREGROUND_*, //3: Turkeyse (dunkel) FOREGROUND_*, //4: Rot (dunkel) FOREGROUND_*, //5: Violett (dunkel) FOREGROUND_*, //6: Gelb (dunkel) FOREGROUND_*, //7: Weiß (dunkel) FOREGROUND_GREY, //8: Grau FOREGROUND_BLUE, //9: Blau (hell) FOREGROUND_GREEN, //10: Grün (hell) FOREGROUND_*, //11: Turkeyse (hell) FOREGROUND_RED, //12: Rot (hell) FOREGROUND_*, //13: Violett (hell) FOREGROUND_YELLOW, //14: Gelb (hell) FOREGROUND_WHITE //15: Weiß (hell) };
Funktioniert irgendwie nicht :S
16 C:\Dokumente und Einstellungen\Administrator\Eigene Dateien\Untitled2.cpp expected
,' or
...' before "color"Kannst du vielleicht näher erläutern wie ich das alles nutze
-
Kóyaánasqatsi schrieb:
...
Stimmt, ist natürlich ein wenig besser.
-
Wie wäre es mit einer Overkill Variante?
#include <ostream> #include <iostream> #include <windows.h> namespace console { enum color_type { RED, GREEN, BLUE, // Weitere möglich... }; namespace priv { WORD convert_color_to_foreground(color_type color) { switch(color) { case RED : return FOREGROUND_RED | FOREGROUND_INTENSITY; case GREEN : return FOREGROUND_GREEN | FOREGROUND_INTENSITY; case BLUE : return FOREGROUND_BLUE | FOREGROUND_INTENSITY; // Weitere möglich... (Kombination zwischen den Farben und Stärken) default : return 0; } } WORD const FOREGROUND_COLOR_MASK = ~FOREGROUND_BLUE & ~FOREGROUND_RED & ~FOREGROUND_GREEN & ~FOREGROUND_INTENSITY; WORD convert_color_to_background(color_type color) { switch(color) { case RED : return BACKGROUND_RED | BACKGROUND_INTENSITY; case GREEN : return BACKGROUND_GREEN | BACKGROUND_INTENSITY; case BLUE : return BACKGROUND_BLUE | BACKGROUND_INTENSITY; // Weitere möglich... (Kombination zwischen den Farben und Stärken) default : return 0; } } WORD const BACKGROUND_COLOR_MASK = ~BACKGROUND_BLUE & ~BACKGROUND_RED & ~BACKGROUND_GREEN & ~BACKGROUND_INTENSITY; } // priv struct fcolor { private: color_type color_; public: fcolor(color_type color) : color_(color) { } color_type get_color() const { return color_; } }; struct bcolor { private: color_type color_; public: bcolor(color_type color) : color_(color) { } color_type get_color() const { return color_; } }; template<typename CharT, typename TraitsT> std::basic_ostream<CharT, TraitsT>& operator <<(std::basic_ostream<CharT, TraitsT>& out, fcolor const& color) { HANDLE handle = GetStdHandle(STD_OUTPUT_HANDLE); CONSOLE_SCREEN_BUFFER_INFO screenBufferInfo = { }; GetConsoleScreenBufferInfo(handle, &screenBufferInfo); screenBufferInfo.wAttributes &= priv::FOREGROUND_COLOR_MASK; screenBufferInfo.wAttributes |= priv::convert_color_to_foreground(color.get_color()); SetConsoleTextAttribute(handle, screenBufferInfo.wAttributes); return out; } template<typename CharT, typename TraitsT> std::basic_ostream<CharT, TraitsT>& operator <<(std::basic_ostream<CharT, TraitsT>& out, bcolor const& color) { HANDLE handle = GetStdHandle(STD_OUTPUT_HANDLE); CONSOLE_SCREEN_BUFFER_INFO screenBufferInfo = { }; GetConsoleScreenBufferInfo(handle, &screenBufferInfo); screenBufferInfo.wAttributes &= priv::BACKGROUND_COLOR_MASK; screenBufferInfo.wAttributes |= priv::convert_color_to_background(color.get_color()); SetConsoleTextAttribute(handle, screenBufferInfo.wAttributes); return out; } } // console int main() { using namespace console; std::cout << bcolor(RED) << fcolor(BLUE); std::cout << "Hallo\n"; std::cin.get(); return 0; }
Schon irgendwie ernüchternd, wenn man das zu C# vergleicht:
using System; namespace Space { public class Program { public static void Main(string[] args) { Console.BackgroundColor = ConsoleColor.Red; Console.ForegroundColor = ConsoleColor.Blue; Console.WriteLine("Hallo"); Console.ReadKey(true); } } }
(Den Vergleich ironisch nehmen ;))Grüssli
-
Noch eins:
namespace Console { static inline void SetForeground( int color ); static inline void SetBackground( int color ); #define COLOR_MANIP_FB( NAME, FOREBACK, VALUE ) \ template<typename C, typename T> inline std::basic_ostream<C, T>& \ NAME( std::basic_ostream<C, T>& stream ) { \ Set ## FOREBACK ( VALUE ); return stream; } #define COLOR_MANIP( NAME, VALUE ) \ COLOR_MANIP_FB( NAME, Foreground, VALUE ) \ COLOR_MANIP_FB( NAME ## _bg, Background, VALUE ) COLOR_MANIP( Black, 0 ); COLOR_MANIP( Blue, 1 ); COLOR_MANIP( Green, 2 ); COLOR_MANIP( Cyan, 3 ); COLOR_MANIP( Red, 4 ); COLOR_MANIP( Magenta, 5 ); COLOR_MANIP( Brown, 6 ); COLOR_MANIP( LightGray, 7 ); COLOR_MANIP( DarkGray, 8 ); COLOR_MANIP( LightBlue, 9 ); COLOR_MANIP( LightGreen, 10 ); COLOR_MANIP( LightCyan, 11 ); COLOR_MANIP( LightRed, 12 ); COLOR_MANIP( LightMagenta, 13 ); COLOR_MANIP( Yellow, 14 ); COLOR_MANIP( White, 15 ); #undef COLOR_MANIP_FB #undef COLOR_MANIP namespace Private { static const unsigned MASK_BLUE = 1; static const unsigned MASK_GREEN = 2; static const unsigned MASK_RED = 4; static const unsigned MASK_INTENSITY = 8; static const unsigned FOREGROUND = FOREGROUND_BLUE | FOREGROUND_GREEN | FOREGROUND_RED | FOREGROUND_INTENSITY; static const unsigned BACKGROUND = BACKGROUND_BLUE | BACKGROUND_GREEN | BACKGROUND_RED | BACKGROUND_INTENSITY; static inline void W32SetColor( int color, int mask ) { CONSOLE_SCREEN_BUFFER_INFO s; const HANDLE h = GetStdHandle( STD_OUTPUT_HANDLE ); if ( h != INVALID_HANDLE_VALUE && h != NULL && GetConsoleScreenBufferInfo( h, &s ) != 0 ) { SetConsoleTextAttribute( h, ( WORD )( color | ( s.wAttributes & mask ) ) ); } } } static inline void SetForeground( int color ) { using namespace Private; int value = 0; if ( color & MASK_BLUE ) value |= FOREGROUND_BLUE; if ( color & MASK_GREEN ) value |= FOREGROUND_GREEN; if ( color & MASK_RED ) value |= FOREGROUND_RED; if ( color & MASK_INTENSITY ) value |= FOREGROUND_INTENSITY; W32SetColor( value, BACKGROUND ); } static inline void SetBackground( int color ) { using namespace Private; int value = 0; if ( color & MASK_BLUE ) value |= BACKGROUND_BLUE; if ( color & MASK_GREEN ) value |= BACKGROUND_GREEN; if ( color & MASK_RED ) value |= BACKGROUND_RED; if ( color & MASK_INTENSITY ) value |= BACKGROUND_INTENSITY; W32SetColor( value, FOREGROUND ); } } int main() { std::cout << Console::Yellow << Console::Green_bg << "Hallo " << Console::DarkGray << Console::Blue_bg << "Welt"; }
-
Dravere schrieb:
Wie wäre es mit einer Overkill Variante?
#include <ostream> #include <iostream> #include <windows.h> namespace console { enum color_type { RED, GREEN, BLUE, // Weitere möglich... }; namespace priv { WORD convert_color_to_foreground(color_type color) { switch(color) { case RED : return FOREGROUND_RED | FOREGROUND_INTENSITY; case GREEN : return FOREGROUND_GREEN | FOREGROUND_INTENSITY; case BLUE : return FOREGROUND_BLUE | FOREGROUND_INTENSITY; // Weitere möglich... (Kombination zwischen den Farben und Stärken) default : return 0; } } WORD const FOREGROUND_COLOR_MASK = ~FOREGROUND_BLUE & ~FOREGROUND_RED & ~FOREGROUND_GREEN & ~FOREGROUND_INTENSITY; WORD convert_color_to_background(color_type color) { switch(color) { case RED : return BACKGROUND_RED | BACKGROUND_INTENSITY; case GREEN : return BACKGROUND_GREEN | BACKGROUND_INTENSITY; case BLUE : return BACKGROUND_BLUE | BACKGROUND_INTENSITY; // Weitere möglich... (Kombination zwischen den Farben und Stärken) default : return 0; } } WORD const BACKGROUND_COLOR_MASK = ~BACKGROUND_BLUE & ~BACKGROUND_RED & ~BACKGROUND_GREEN & ~BACKGROUND_INTENSITY; } // priv struct fcolor { private: color_type color_; public: fcolor(color_type color) : color_(color) { } color_type get_color() const { return color_; } }; struct bcolor { private: color_type color_; public: bcolor(color_type color) : color_(color) { } color_type get_color() const { return color_; } }; template<typename CharT, typename TraitsT> std::basic_ostream<CharT, TraitsT>& operator <<(std::basic_ostream<CharT, TraitsT>& out, fcolor const& color) { HANDLE handle = GetStdHandle(STD_OUTPUT_HANDLE); CONSOLE_SCREEN_BUFFER_INFO screenBufferInfo = { }; GetConsoleScreenBufferInfo(handle, &screenBufferInfo); screenBufferInfo.wAttributes &= priv::FOREGROUND_COLOR_MASK; screenBufferInfo.wAttributes |= priv::convert_color_to_foreground(color.get_color()); SetConsoleTextAttribute(handle, screenBufferInfo.wAttributes); return out; } template<typename CharT, typename TraitsT> std::basic_ostream<CharT, TraitsT>& operator <<(std::basic_ostream<CharT, TraitsT>& out, bcolor const& color) { HANDLE handle = GetStdHandle(STD_OUTPUT_HANDLE); CONSOLE_SCREEN_BUFFER_INFO screenBufferInfo = { }; GetConsoleScreenBufferInfo(handle, &screenBufferInfo); screenBufferInfo.wAttributes &= priv::BACKGROUND_COLOR_MASK; screenBufferInfo.wAttributes |= priv::convert_color_to_background(color.get_color()); SetConsoleTextAttribute(handle, screenBufferInfo.wAttributes); return out; } } // console int main() { using namespace console; std::cout << bcolor(RED) << fcolor(BLUE); std::cout << "Hallo\n"; std::cin.get(); return 0; }
Schon irgendwie ernüchternd, wenn man das zu C# vergleicht:
using System; namespace Space { public class Program { public static void Main(string[] args) { Console.BackgroundColor = ConsoleColor.Red; Console.ForegroundColor = ConsoleColor.Blue; Console.WriteLine("Hallo"); Console.ReadKey(true); } } }
(Den Vergleich ironisch nehmen ;))Grüssli
Vielen Lieben dank
Könntest du mir noch zeigen wie ich die Farbe Schwarz und Weiß einfüge?Und auch danke an alle anderen
lg
-
Mein Code kann alle 16 Farben:
int main() { using namespace Console; std::cout << Black << White_bg << "foobar" << std::endl; // Schwarzer Text auf weißem Hintergrund }
-
David_pb schrieb:
Mein Code kann alle 16 Farben:
int main() { using namespace Console; std::cout << Black << White_bg << "foobar" << std::endl; // Schwarzer Text auf weißem Hintergrund }
Hmm der Funktioniert so nicht
121 \.psf\Home\Desktop\Unbenannt3.cpp expected namespace-name before ';' token
Als Include Dateien hab ich iostream und conio.h genommen
-
Und hast den Code, von mir, von ganz oben mit eingebunden? Außerdem benötigst du natürlich den Header windows.h.