2009-05-22 5 views
7

Ich möchte eine Console-Klasse schreiben, die farbigen Text auf der Konsole ausgeben kann.Drucken von farbigem Text zur Konsole in C++

So kann ich so etwas wie (im Grunde ein Wrapper für printf) tun:

Console::Print("This is a non-coloured message\n"); 
Console::Warning("This is a YELLOW warning message\n"); 
Console::Error("This is a RED error message\n"); 

Wie würde ich verschiedenen farbigen Text in der Windows-Konsole drucken?

Antwort

9

Check out this guide. Ich würde eine benutzerdefinierte Manipulator machen, damit ich so etwas wie tun könnte:

std::cout << "standard text" << setcolour(red) << "red text" << std::endl; 

Here ‚s eine kleine Anleitung, wie Sie Ihren eigenen Manipulator zu implementieren.

Ein kurzes Codebeispiel:

#include <iostream> 
#include <windows.h> 
#include <iomanip> 

using namespace std; 

enum colour { DARKBLUE = 1, DARKGREEN, DARKTEAL, DARKRED, DARKPINK, DARKYELLOW, GRAY, DARKGRAY, BLUE, GREEN, TEAL, RED, PINK, YELLOW, WHITE }; 

struct setcolour 
{ 
    colour _c; 
    HANDLE _console_handle; 


     setcolour(colour c, HANDLE console_handle) 
      : _c(c), _console_handle(0) 
     { 
      _console_handle = console_handle; 
     } 
}; 

// We could use a template here, making it more generic. Wide streams won't 
// work with this version. 
basic_ostream<char> &operator<<(basic_ostream<char> &s, const setcolour &ref) 
{ 
    SetConsoleTextAttribute(ref._console_handle, ref._c); 
    return s; 
} 

int main(int argc, char *argv[]) 
{ 
    HANDLE chandle = GetStdHandle(STD_OUTPUT_HANDLE); 
    cout << "standard text" << setcolour(RED, chandle) << " red text" << endl; 

    cin.get(); 
} 
+0

Dieser Code funktioniert nicht wirklich, Ihre vorherige Version, die Sie gerade ersetzt haben aber tat. Ill Doppelscheck ich habe es richtig, obwohl –

+0

@Brock Woolf: Ja, tut mir leid. Ich hatte ein Problem beim Kopieren des GRIFFS. – Skurmedel

+0

Wunderbar, das funktioniert perfekt. Vielen Dank! : D –

1

Ich habe eine Suche nach "C++ Konsole schreiben farbigen Text" und kam mit this page bei etwa 4 oder 5. Da die Website eine Kopie & Paste Abschnitt Ich dachte, ich würde es hier (eine andere Frage auf Link rot auch aufgefordert, diese):

#include <stdlib.h> 
#include <windows.h> 
#include <iostream> 

using namespace std; 

enum Color { DBLUE=1,GREEN,GREY,DRED,DPURP,BROWN,LGREY,DGREY,BLUE,LIMEG,TEAL, 
    RED,PURPLE,YELLOW,WHITE,B_B }; 
/* These are the first 16 colors anyways. You test the other hundreds yourself. 
    After 15 they are all combos of different color text/backgrounds. */ 

bool quit; 

void col(unsigned short color) 
{ 
    HANDLE hcon = GetStdHandle(STD_OUTPUT_HANDLE); 
    SetConsoleTextAttribute(hcon,color); 
} 

istream &operator>> (istream &in, Color &c) 
{ 
    int tint; 
    cin >> tint; 
    if (tint==-1) quit=true; 
    c=(Color)tint; 
} 

int main() 
{ 
    do { 
     col(7); // Defaults color for each round. 
     cout << "Enter a color code, or -1 to quit... "; 
     Color y; 
     cin >> y; // Notice that >> is defined above for Color types. 
     col(y); // Sets output color to y. 
     if (!quit) cout << "Color: " << (int)y << endl; 
    } while (!quit); 
    return 0; 
} 

Für C# gibt es this page

+0

Das erste Link war auch informativ hatte, obwohl es C# war. –

+0

Der Suchbegriff war "C# console.write farbiger Text" in diesem Fall – ChrisF

0

Look here um herauszufinden, wie auf die Konsole in Windows zu schreiben und here wie Attribute auf sich anzuwenden.
An welchem ​​Betriebssystem arbeiten Sie?

0
#include <windows.h> 
#include <iostream.h> 
using namespace std; 

int main() 
{ 
    HANDLE hOut; 

    hOut = GetStdHandle(STD_OUTPUT_HANDLE); 

    SetConsoleTextAttribute(hOut, 
          BACKGROUND_RED); 
    cout << "Red  " << flush; 
    SetConsoleTextAttribute(hOut, 
          BACKGROUND_RED | 
          BACKGROUND_INTENSITY); 
    cout << "Red  " << endl; 

    SetConsoleTextAttribute(hOut, 
          BACKGROUND_GREEN); 
    cout << "Green " << flush; 
    SetConsoleTextAttribute(hOut, 
          BACKGROUND_GREEN | 
          BACKGROUND_INTENSITY); 
    cout << "Green " << endl; 

    SetConsoleTextAttribute(hOut, 
          BACKGROUND_BLUE); 
    cout << "Blue " << flush; 
    SetConsoleTextAttribute(hOut, 
          BACKGROUND_BLUE | 
          BACKGROUND_INTENSITY); 
    cout << "Blue " << endl; 

    SetConsoleTextAttribute(hOut, 
          BACKGROUND_RED | 
          BACKGROUND_GREEN); 
    cout << "Yellow " << flush; 
    SetConsoleTextAttribute(hOut, 
          BACKGROUND_RED | 
          BACKGROUND_GREEN | 
          BACKGROUND_INTENSITY); 
    cout << "Yellow " << endl; 

    SetConsoleTextAttribute(hOut, 
          BACKGROUND_GREEN | 
          BACKGROUND_BLUE); 
    cout << "Cyan " << flush; 
    SetConsoleTextAttribute(hOut, 
          BACKGROUND_GREEN | 
          BACKGROUND_BLUE | 
          BACKGROUND_INTENSITY); 
    cout << "Cyan " << endl; 

    SetConsoleTextAttribute(hOut, 
          BACKGROUND_BLUE | 
          BACKGROUND_RED); 
    cout << "Magenta " << flush; 
    SetConsoleTextAttribute(hOut, 
          BACKGROUND_BLUE | 
          BACKGROUND_RED | 
          BACKGROUND_INTENSITY); 
    cout << "Magenta " << endl; 

    SetConsoleTextAttribute(hOut, 
          BACKGROUND_RED | 
          BACKGROUND_GREEN | 
          BACKGROUND_BLUE); 
    cout << "White " << flush; 
    SetConsoleTextAttribute(hOut, 
          BACKGROUND_RED | 
          BACKGROUND_GREEN | 
          BACKGROUND_BLUE | 
          BACKGROUND_INTENSITY); 
    cout << "White " << endl; 

    return 0; 
} 
+0

hoffe das funktioniert – madhudskumar

0

Gebrauch diese Funktionen

enum c_color{BLACK=30,RED=31,GREEN=32,YELLOW=33,BLUE=34,MAGENTA=35,CYAN=36,WHITE=37}; 
enum c_decoration{NORMAL=0,BOLD=1,FAINT=2,ITALIC=3,UNDERLINE=4,RIVERCED=26,FRAMED=51}; 
void pr(const string str,c_color color,c_decoration decoration=c_decoration::NORMAL){ 
    cout<<"\033["<<decoration<<";"<<color<<"m"<<str<<"\033[0m"; 
} 

void prl(const string str,c_color color,c_decoration decoration=c_decoration::NORMAL){ 
    cout<<"\033["<<decoration<<";"<<color<<"m"<<str<<"\033[0m"<<endl; 
}