Outputting Strings in the Console

   
Outputting Strings in the Console
        

#include <windows.h>

class Console {
public:
  enum fore_color {
    f_black  = 0                                                                         ,
    f_white  = FOREGROUND_INTENSITY | FOREGROUND_RED | FOREGROUND_GREEN | FOREGROUND_BLUE,
    f_red    = FOREGROUND_INTENSITY | FOREGROUND_RED                                     ,
    f_green  = FOREGROUND_INTENSITY |                  FOREGROUND_GREEN                  ,
    f_yellow = FOREGROUND_INTENSITY | FOREGROUND_RED | FOREGROUND_GREEN                  ,
    f_blue   = FOREGROUND_INTENSITY |                                     FOREGROUND_BLUE,
    f_magenta= FOREGROUND_INTENSITY | FOREGROUND_RED |                    FOREGROUND_BLUE,
    f_cyan   = FOREGROUND_INTENSITY |                  FOREGROUND_GREEN | FOREGROUND_BLUE,
  };

  enum back_color {
    b_black  = 0                                                                         ,
    b_white  = BACKGROUND_INTENSITY | BACKGROUND_RED | BACKGROUND_GREEN | BACKGROUND_BLUE,
    b_red    = BACKGROUND_INTENSITY | BACKGROUND_RED                                     ,
    b_green  = BACKGROUND_INTENSITY |                  BACKGROUND_GREEN                  ,
    b_yellow = BACKGROUND_INTENSITY | BACKGROUND_RED | BACKGROUND_GREEN                  ,
    b_blue   = BACKGROUND_INTENSITY |                                     BACKGROUND_BLUE,
    b_magenta= BACKGROUND_INTENSITY | BACKGROUND_RED |                    BACKGROUND_BLUE,
    b_cyan   = BACKGROUND_INTENSITY |                  BACKGROUND_GREEN | BACKGROUND_BLUE,
  };

  void Move (int x, int y);
  void Color(fore_color fg, back_color bg);
  void Cls(back_color bg=b_black);
};

void Console::Cls(back_color bg) {
  COORD coordScreen = { 0, 0 };
  DWORD cCharsWritten;
  CONSOLE_SCREEN_BUFFER_INFO csbi;
  DWORD dwConSize;
  HANDLE hConsole = GetStdHandle(STD_OUTPUT_HANDLE);

  SetConsoleTextAttribute(hConsole, bg);
 
  GetConsoleScreenBufferInfo(hConsole, &csbi);
  dwConSize = csbi.dwSize.X * csbi.dwSize.Y;
  FillConsoleOutputCharacter(hConsole, TEXT(' '),
                             dwConSize,
                             coordScreen,
                             &cCharsWritten);
  GetConsoleScreenBufferInfo(hConsole, &csbi);
  FillConsoleOutputAttribute(hConsole,
                             csbi.wAttributes,
                             dwConSize,
                             coordScreen,
                             &cCharsWritten);
  SetConsoleCursorPosition(hConsole, coordScreen);
}

void Console::Move(int x, int y) {
  COORD point;
  point.X = x; point.Y = y;
  SetConsoleCursorPosition(
    GetStdHandle(STD_OUTPUT_HANDLE),point);
}

void Console::Color(fore_color fg, back_color bg) {
  SetConsoleTextAttribute(
    GetStdHandle(STD_OUTPUT_HANDLE), fg | bg);
}

#include <iostream>

using namespace std;

int main(int argc, char* argv[]){
  Console c;
  c.Cls(Console::b_green);
  c.Color(Console::f_red , Console::b_blue);
  c.Move(4,4);
  cout << "xxx";
  c.Color(Console::f_blue, Console::b_yellow);
  c.Move(4,5);
  cout << "yyyyy";

  return 0;
}

原文地址:https://www.cnblogs.com/honeynm/p/4695890.html