Advertisement
rc-chuah

ClearScreen5

Mar 3rd, 2022 (edited)
883
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 1.69 KB | None | 0 0
  1. #include <windows.h>
  2.  
  3. void ClearScreen() {
  4.     HANDLE hStdOut;
  5.     COORD coordScreen = { 0, 0 };    // home for the cursor
  6.     DWORD cCharsWritten;
  7.     CONSOLE_SCREEN_BUFFER_INFO csbi;
  8.     DWORD dwConSize;
  9.     hStdOut = GetStdHandle(STD_OUTPUT_HANDLE);
  10.     // Get the number of character cells in the current buffer.
  11.     if (!GetConsoleScreenBufferInfo(hStdOut, &csbi))
  12.     {
  13.         return;
  14.     }
  15.     dwConSize = csbi.dwSize.X * csbi.dwSize.Y;
  16.     // Fill the entire screen with blanks.
  17.     if (!FillConsoleOutputCharacter(hStdOut,         // Handle to console screen buffer
  18.                                     (TCHAR)' ',      // Character to write to the buffer
  19.                                     dwConSize,       // Number of cells to write
  20.                                     coordScreen,     // Coordinates of first cell
  21.                                     &cCharsWritten)) // Receive number of characters written
  22.     {
  23.         return;
  24.     }
  25.     // Get the current text attribute.
  26.     if (!GetConsoleScreenBufferInfo(hStdOut, &csbi))
  27.     {
  28.         return;
  29.     }
  30.     // Set the buffer's attributes accordingly.
  31.     if (!FillConsoleOutputAttribute(hStdOut,          // Handle to console screen buffer
  32.                                     csbi.wAttributes, // Character attributes to use
  33.                                     dwConSize,        // Number of cells to set attribute
  34.                                     coordScreen,      // Coordinates of first cell
  35.                                     &cCharsWritten))  // Receive number of characters written
  36.     {
  37.         return;
  38.     }
  39.     // Put the cursor at its home coordinates.
  40.     SetConsoleCursorPosition(hStdOut, coordScreen);
  41. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement