- #include <iostream>
- #include <windows.h>
- #include <strsafe.h>
- #include <vector>
- #include <string>
- #include "atlstr.h"
- class Console
- {
- private:
- CONSOLE_SCREEN_BUFFER_INFO csbi;
- LPDWORD flags;
- int columns, rows;
- short cursorX, cursorY;
- int maxWindowSizeX, maxWindowSizeY;
- char title[MAX_PATH];
- public:
- Console()
- {
- GetConsoleScreenBufferInfo(GetStdHandle(STD_OUTPUT_HANDLE), &csbi);
- cursorX = csbi.dwCursorPosition.X;
- cursorY = csbi.dwCursorPosition.Y;
- maxWindowSizeX = csbi.dwMaximumWindowSize.X;
- maxWindowSizeY = csbi.dwMaximumWindowSize.Y;
- columns = csbi.srWindow.Right - csbi.srWindow.Left + 1;
- rows = csbi.srWindow.Bottom - csbi.srWindow.Top + 1;
- TCHAR t[MAX_PATH];
- GetConsoleTitle(t, MAX_PATH);
- size_t nNumCharConverted;
- wcstombs_s(&nNumCharConverted, title, MAX_PATH, t, MAX_PATH); // Convert
- }
- void clear() {
- COORD topLeft = { 0, 0 };
- HANDLE console = GetStdHandle(STD_OUTPUT_HANDLE);
- CONSOLE_SCREEN_BUFFER_INFO screen;
- DWORD written;
- GetConsoleScreenBufferInfo(console, &screen);
- FillConsoleOutputCharacterA(
- console, ' ', screen.dwSize.X * screen.dwSize.Y, topLeft, &written
- );
- FillConsoleOutputAttribute(
- console, FOREGROUND_GREEN | FOREGROUND_RED | FOREGROUND_BLUE,
- screen.dwSize.X * screen.dwSize.Y, topLeft, &written
- );
- SetConsoleCursorPosition(console, topLeft);
- }
- bool isFullScreen()
- {
- DWORD dwValue = 12345;
- LPDWORD pVal = &dwValue;
- GetConsoleDisplayMode(pVal);
- int mode = *pVal;
- if (mode == CONSOLE_FULLSCREEN_MODE) return true;
- return false;
- }
- void toggleFullscreen()
- {
- keybd_event(VK_MENU, 0x38, 0, 0);
- keybd_event(VK_RETURN, 0x1c, 0, 0);
- keybd_event(VK_RETURN, 0x1c, KEYEVENTF_KEYUP, 0);
- keybd_event(VK_MENU, 0x38, KEYEVENTF_KEYUP, 0);
- }
- int getCols()
- {
- return columns;
- }
- int getRows()
- {
- return rows;
- }
- int getCurX()
- {
- return cursorX;
- }
- int getCurY()
- {
- return cursorY;
- }
- int getMaxWinX()
- {
- return maxWindowSizeX;
- }
- int getMaxWinY()
- {
- return maxWindowSizeY;
- }
- void minimize()
- {
- ShowWindow(GetConsoleWindow(), SW_MINIMIZE);
- }
- void maximize()
- {
- ShowWindow(GetConsoleWindow(), SW_MAXIMIZE);
- }
- void show()
- {
- ShowWindow(GetConsoleWindow(), SW_SHOW);
- }
- void hide()
- {
- ShowWindow(GetConsoleWindow(), SW_HIDE);
- }
- void setCurPos(short x, short y)
- {
- SetConsoleCursorPosition(GetStdHandle(STD_OUTPUT_HANDLE), { x, y });
- }
- void setCurX(short x)
- {
- SetConsoleCursorPosition(GetStdHandle(STD_OUTPUT_HANDLE), { x, cursorY });
- }
- void setCurY(short y)
- {
- SetConsoleCursorPosition(GetStdHandle(STD_OUTPUT_HANDLE), { cursorX, y });
- }
- void setTitle(const char* title)
- {
- TCHAR newT[MAX_PATH];
- USES_CONVERSION;
- wcscpy_s(newT, A2T(title));
- SetConsoleTitle(newT);
- }
- char* getTitle()
- {
- return title;
- }
- void setColor(int color)
- {
- SetConsoleTextAttribute(GetStdHandle(STD_OUTPUT_HANDLE), color);
- }
- };