Advertisement
Felanpro

useful borrowed functions

Feb 24th, 2019
163
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 1.57 KB | None | 0 0
  1. #define NOMINMAX
  2. #define WIN32_LEAN_AND_MEAN
  3. #include <Windows.h>
  4.  
  5. void cls()
  6. {
  7.     // Get the Win32 handle representing standard output.
  8.     // This generally only has to be done once, so we make it static.
  9.     static const HANDLE hOut = GetStdHandle(STD_OUTPUT_HANDLE);
  10.  
  11.     CONSOLE_SCREEN_BUFFER_INFO csbi;
  12.     COORD topLeft = { 0, 0 };
  13.  
  14.     // std::cout uses a buffer to batch writes to the underlying console.
  15.     // We need to flush that to the console because we're circumventing
  16.     // std::cout entirely; after we clear the console, we don't want
  17.     // stale buffered text to randomly be written out.
  18.     std::cout.flush();
  19.  
  20.     // Figure out the current width and height of the console window
  21.     if (!GetConsoleScreenBufferInfo(hOut, &csbi)) {
  22.         // TODO: Handle failure!
  23.         abort();
  24.     }
  25.     DWORD length = csbi.dwSize.X * csbi.dwSize.Y;
  26.  
  27.     DWORD written;
  28.  
  29.     // Flood-fill the console with spaces to clear it
  30.     FillConsoleOutputCharacter(hOut, TEXT(' '), length, topLeft, &written);
  31.  
  32.     // Reset the attributes of every character to the default.
  33.     // This clears all background colour formatting, if any.
  34.     FillConsoleOutputAttribute(hOut, csbi.wAttributes, length, topLeft, &written);
  35.  
  36.     // Move the cursor back to the top left for the next sequence of writes
  37.     SetConsoleCursorPosition(hOut, topLeft);
  38. }
  39.  
  40. void setCursorPosition(int x, int y)
  41. {
  42.     static const HANDLE hOut = GetStdHandle(STD_OUTPUT_HANDLE);
  43.     std::cout.flush();
  44.     COORD coord = { (SHORT)x, (SHORT)y };
  45.     SetConsoleCursorPosition(hOut, coord);
  46. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement