2009-05-22 119 views
7

我想編寫一個控制檯類,它可以輸出彩色文本到控制檯。在C++中打印彩色文本到控制檯

所以我可以這樣做(基本上是printf的一個包裝):

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

我怎麼會打印不同顏色的文字到Windows控制檯?

回答

9

退房this guide。我會做一個自定義的操縱,所以我可以做這樣的事情:

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

Here的關於如何實現自己的手小導。

一個快速的代碼示例:

#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

此代碼實際上並不工作,但您剛更換的以前的版本卻沒有。生病雙重檢查,雖然我有,但 – 2009-05-22 19:41:55

1

我做了一個「C++控制檯寫彩色文本」的搜索,想出了this page約4或5.由於該網站有一個副本&粘貼部分,我想我會在這裏發佈它(關於鏈接腐爛的另一個問題也促使這一點):

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

對於C#有this page

+0

,你有第一個鏈接是內容豐富,即使它是C#。 – 2009-05-22 18:45:26

+0

在這種情況下,搜索條件是「c#console.write colored text」 – ChrisF 2009-05-22 18:48:21

0

Look here找出如何寫在Windows控制檯和here如何在其應用屬性。
你在做什麼操作系統?

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

使用這些功能

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