Facebook
From Ja, 5 Years ago, written in Plain Text.
Embed
Download Paste or View Raw
Hits: 790
  1. #include <iostream>
  2. #include <windows.h>
  3. #include <strsafe.h>
  4. #include <vector>
  5. #include <string>
  6. #include "atlstr.h"
  7.  
  8. class Console
  9. {
  10. private:
  11.         CONSOLE_SCREEN_BUFFER_INFO csbi;
  12.         LPDWORD flags;
  13.         int columns, rows;
  14.         short cursorX, cursorY;
  15.         int maxWindowSizeX, maxWindowSizeY;
  16.         char title[MAX_PATH];
  17.  
  18. public:
  19.  
  20.         Console()
  21.         {
  22.                 GetConsoleScreenBufferInfo(GetStdHandle(STD_OUTPUT_HANDLE), &csbi);
  23.                 cursorX = csbi.dwCursorPosition.X;
  24.                 cursorY = csbi.dwCursorPosition.Y;
  25.                 maxWindowSizeX = csbi.dwMaximumWindowSize.X;
  26.                 maxWindowSizeY = csbi.dwMaximumWindowSize.Y;
  27.                 columns = csbi.srWindow.Right - csbi.srWindow.Left + 1;
  28.                 rows = csbi.srWindow.Bottom - csbi.srWindow.Top + 1;
  29.  
  30.                 TCHAR t[MAX_PATH];
  31.                 GetConsoleTitle(t, MAX_PATH);
  32.                 size_t nNumCharConverted;
  33.                 wcstombs_s(&nNumCharConverted, title, MAX_PATH, t, MAX_PATH); // Convert
  34.         }
  35.  
  36.         void clear() {
  37.                 COORD topLeft = { 0, 0 };
  38.                 HANDLE console = GetStdHandle(STD_OUTPUT_HANDLE);
  39.                 CONSOLE_SCREEN_BUFFER_INFO screen;
  40.                 DWORD written;
  41.  
  42.                 GetConsoleScreenBufferInfo(console, &screen);
  43.                 FillConsoleOutputCharacterA(
  44.                         console, ' ', screen.dwSize.X * screen.dwSize.Y, topLeft, &written
  45.                 );
  46.                 FillConsoleOutputAttribute(
  47.                         console, FOREGROUND_GREEN | FOREGROUND_RED | FOREGROUND_BLUE,
  48.                         screen.dwSize.X * screen.dwSize.Y, topLeft, &written
  49.                 );
  50.                 SetConsoleCursorPosition(console, topLeft);
  51.         }
  52.  
  53.         bool isFullScreen()
  54.         {
  55.                 DWORD dwValue = 12345;
  56.                 LPDWORD pVal = &dwValue;
  57.                 GetConsoleDisplayMode(pVal);
  58.                 int mode = *pVal;
  59.                 if (mode == CONSOLE_FULLSCREEN_MODE) return true;
  60.                 return false;
  61.         }
  62.  
  63.         void toggleFullscreen()
  64.         {
  65.                 keybd_event(VK_MENU, 0x38, 0, 0);
  66.                 keybd_event(VK_RETURN, 0x1c, 0, 0);
  67.                 keybd_event(VK_RETURN, 0x1c, KEYEVENTF_KEYUP, 0);
  68.                 keybd_event(VK_MENU, 0x38, KEYEVENTF_KEYUP, 0);
  69.         }
  70.  
  71.         int getCols()
  72.         {
  73.                 return columns;
  74.         }
  75.  
  76.         int getRows()
  77.         {
  78.                 return rows;
  79.         }
  80.  
  81.         int getCurX()
  82.         {
  83.                 return cursorX;
  84.         }
  85.  
  86.         int getCurY()
  87.         {
  88.                 return cursorY;
  89.         }
  90.  
  91.         int getMaxWinX()
  92.         {
  93.                 return maxWindowSizeX;
  94.         }
  95.  
  96.         int getMaxWinY()
  97.         {
  98.                 return maxWindowSizeY;
  99.         }
  100.  
  101.         void minimize()
  102.         {
  103.                 ShowWindow(GetConsoleWindow(), SW_MINIMIZE);
  104.         }
  105.  
  106.         void maximize()
  107.         {
  108.                 ShowWindow(GetConsoleWindow(), SW_MAXIMIZE);
  109.         }
  110.  
  111.         void show()
  112.         {
  113.                 ShowWindow(GetConsoleWindow(), SW_SHOW);
  114.         }
  115.  
  116.         void hide()
  117.         {
  118.                 ShowWindow(GetConsoleWindow(), SW_HIDE);
  119.         }
  120.  
  121.         void setCurPos(short x, short y)
  122.         {
  123.                 SetConsoleCursorPosition(GetStdHandle(STD_OUTPUT_HANDLE), { x, y });
  124.         }
  125.  
  126.         void setCurX(short x)
  127.         {
  128.                 SetConsoleCursorPosition(GetStdHandle(STD_OUTPUT_HANDLE), { x, cursorY });
  129.         }
  130.  
  131.         void setCurY(short y)
  132.         {
  133.                 SetConsoleCursorPosition(GetStdHandle(STD_OUTPUT_HANDLE), { cursorX, y });
  134.         }
  135.  
  136.         void setTitle(const char* title)
  137.         {
  138.                 TCHAR newT[MAX_PATH];
  139.                 USES_CONVERSION;
  140.                 wcscpy_s(newT, A2T(title));
  141.                 SetConsoleTitle(newT);
  142.         }
  143.  
  144.         char* getTitle()
  145.         {
  146.                 return title;
  147.         }
  148.  
  149.         void setColor(int color)
  150.         {
  151.                 SetConsoleTextAttribute(GetStdHandle(STD_OUTPUT_HANDLE), color);
  152.         }
  153. };