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

  • 相关阅读:
    ArrayList源码 (基于1.7)
    java.lang.Class类中的某些方法
    jdk1.8新特性 : 接口中可以有普通方法(非静态方法)和静态方法 , 颠覆了之前我的理解 : 接口中只能有共有常量和抽象方法的概念,后面必须要加一句jdk1.7和1..7之前
    Cesium中级教程10
    Cesium中级教程9
    Cesium中级教程8
    Cesium中级教程7
    Cesium中级教程6
    Cesium中级教程5
    Cesium中级教程4
  • 原文地址:https://www.cnblogs.com/honeynm/p/4695890.html
Copyright © 2020-2023  润新知