#pragma once #include #include #include #include class text_color { public: enum Color { Black = 0, White = FOREGROUND_BLUE | FOREGROUND_GREEN | FOREGROUND_RED, Blue = FOREGROUND_BLUE, Green = FOREGROUND_GREEN, Red = FOREGROUND_RED, Yellow = FOREGROUND_RED | FOREGROUND_GREEN, Magenta = FOREGROUND_RED | FOREGROUND_BLUE, LightWhite = FOREGROUND_BLUE | FOREGROUND_GREEN | FOREGROUND_RED | FOREGROUND_INTENSITY, LightBlue = FOREGROUND_BLUE | FOREGROUND_INTENSITY, LightGreen = FOREGROUND_GREEN | FOREGROUND_INTENSITY, LightRed = FOREGROUND_RED | FOREGROUND_INTENSITY, LightYellow = FOREGROUND_RED | FOREGROUND_GREEN | FOREGROUND_INTENSITY, LightMagenta = FOREGROUND_RED | FOREGROUND_BLUE | FOREGROUND_INTENSITY, }; }; class bg_color { public: enum Color { Black = 0, White = BACKGROUND_BLUE | BACKGROUND_GREEN | BACKGROUND_RED, Blue = BACKGROUND_BLUE, Green = BACKGROUND_GREEN, Red = BACKGROUND_RED, Yellow = BACKGROUND_RED | BACKGROUND_GREEN, Magenta = BACKGROUND_RED | BACKGROUND_BLUE, LightWhite = BACKGROUND_BLUE | BACKGROUND_GREEN | BACKGROUND_RED | BACKGROUND_INTENSITY, LightBlue = BACKGROUND_BLUE | BACKGROUND_INTENSITY, LightGreen = BACKGROUND_GREEN | BACKGROUND_INTENSITY, LightRed = BACKGROUND_RED | BACKGROUND_INTENSITY, LightYellow = BACKGROUND_RED | BACKGROUND_GREEN | BACKGROUND_INTENSITY, LightMagenta = BACKGROUND_RED | BACKGROUND_BLUE | BACKGROUND_INTENSITY, }; }; struct console_pos { int X; int Y; console_pos(int x = 0, int y = 0):X(x), Y(y) {} }; struct console_colors { text_color::Color TextColor; bg_color::Color BackColor; console_colors(const text_color::Color& crText, const bg_color::Color& crBack): TextColor(crText), BackColor(crBack) {} }; template class basic_console { HANDLE m_hStdin; HANDLE m_hStdout; CONSOLE_SCREEN_BUFFER_INFO m_csbiInfoDefault; public: typedef basic_console<_Elem, _Traits> _Myt; typedef std::basic_ostream<_Elem, _Traits> _Myos; typedef std::basic_ios<_Elem, _Traits> _Myios; typedef std::basic_streambuf<_Elem, _Traits> _Mysb; public: basic_console(void) { m_hStdin = GetStdHandle(STD_INPUT_HANDLE); m_hStdout = GetStdHandle(STD_OUTPUT_HANDLE); GetConsoleScreenBufferInfo(m_hStdout, &m_csbiInfoDefault); } virtual ~basic_console(void) { SetConsoleCursorPosition(m_hStdout, m_csbiInfoDefault.dwCursorPosition); SetConsoleTextAttribute(m_hStdout, m_csbiInfoDefault.wAttributes); } void GoTo(int x, int y) { COORD coord; coord.X = x; coord.Y = y; SetConsoleCursorPosition(m_hStdout, coord); } void GoToX(int x) { CONSOLE_SCREEN_BUFFER_INFO info; GetConsoleScreenBufferInfo(m_hStdout, &info); info.dwCursorPosition.X = x; SetConsoleCursorPosition(m_hStdout, info.dwCursorPosition); } void GoToY(int y) { CONSOLE_SCREEN_BUFFER_INFO info; GetConsoleScreenBufferInfo(m_hStdout, &info); info.dwCursorPosition.Y = y; SetConsoleCursorPosition(m_hStdout, info.dwCursorPosition); } void SetForeColor(text_color::Color crText) { WORD attr = m_csbiInfoDefault.wAttributes & 0xFF00; attr |= crText; SetConsoleTextAttribute(m_hStdout, attr); } void SetBackColor(bg_color::Color crBack) { WORD attr = m_csbiInfoDefault.wAttributes & 0xFF00; attr |= crBack; SetConsoleTextAttribute(m_hStdout, attr); } void SetColors(text_color::Color crText, bg_color::Color crBack) { WORD attr = m_csbiInfoDefault.wAttributes & 0xFF00; attr |= crText; attr |= crBack; SetConsoleTextAttribute(m_hStdout, attr); } void SetColors(WORD colors) { WORD attr = m_csbiInfoDefault.wAttributes & 0xFF00; attr |= colors; SetConsoleTextAttribute(m_hStdout, attr); } void SetColors(WORD crText, WORD crBack) { WORD attr = m_csbiInfoDefault.wAttributes & 0xFF00; attr |= crText; attr |= crBack; SetConsoleTextAttribute(m_hStdout, attr); } void UseDefaultColors() { SetConsoleTextAttribute(m_hStdout, m_csbiInfoDefault.wAttributes); } _Myt& __CLR_OR_THIS_CALL operator<<(_Myt& (__cdecl *_Pfn)(_Myt&)) { // call basic_ostream manipulator _DEBUG_POINTER(_Pfn); return ((*_Pfn)(*this)); } _Myt& __CLR_OR_THIS_CALL operator<<(_Myios& (__cdecl *_Pfn)(_Myios&)) { // call basic_ios manipulator _DEBUG_POINTER(_Pfn); (*_Pfn)(*(_Myios *)this); return (*this); } _Myt& __CLR_OR_THIS_CALL operator<<(std::ios_base& (__cdecl *_Pfn)(std::ios_base&)) { // call ios_base manipulator _DEBUG_POINTER(_Pfn); (*_Pfn)(*(std::ios_base *)this); return (*this); } _Myt& operator<<(char* val) { std::cout << val; return *this; } _Myt& operator<<(bool val) { std::cout << val; return *this; } _Myt& operator<<(char val) { std::cout << val; return *this; } _Myt& operator<<(unsigned char val) { std::cout << val; return *this; } _Myt& operator<<(short val) { std::cout << val; return *this; } _Myt& operator<<(unsigned short val) { std::cout << val; return *this; } _Myt& operator<<(int val) { std::cout << val; return *this; } _Myt& operator<<(unsigned int val) { std::cout << val; return *this; } _Myt& operator<<(long val) { std::cout << val; return *this; } _Myt& operator<<(unsigned long val) { std::cout << val; return *this; } _Myt& operator<<(float val) { std::cout << val; return *this; } _Myt& operator<<(double val) { std::cout << val; return *this; } _Myt& operator<<(std::basic_string<_Elem> val) { std::cout << val; return *this; } _Myt& operator<<(std::ostream& stval) { std::cout << stval; return *this; } _Myt& operator>>(char* val) { std::cin >> val; return *this; } _Myt& operator>>(bool& val) { std::cin >> val; return *this; } _Myt& operator>>(char& val) { std::cin >> val; return *this; } _Myt& operator>>(unsigned char& val) { std::cin >> val; return *this; } _Myt& operator>>(short& val) { std::cin >> val; return *this; } _Myt& operator>>(unsigned short& val) { std::cin >> val; return *this; } _Myt& operator>>(int& val) { std::cin >> val; return *this; } _Myt& operator>>(unsigned int& val) { std::cin >> val; return *this; } _Myt& operator>>(long& val) { std::cin >> val; return *this; } _Myt& operator>>(unsigned long& val) { std::cin >> val; return *this; } _Myt& operator>>(float& val) { std::cin >> val; return *this; } _Myt& operator>>(double& val) { std::cin >> val; return *this; } _Myt& operator>>(std::basic_string<_Elem>& val) { std::cin >> val; return *this; } _Myt& operator>>(std::istream& stval) { std::cin >> stval; return *this; } _Myt& operator<<(const console_pos& pos) { GoTo(pos.X, pos.Y); return *this; } _Myt& operator<<(const console_colors& col) { SetColors(col.TextColor, col.BackColor); return *this; } }; typedef basic_console> console; typedef basic_console> wconsole;