Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <windows.h>
- void ClearScreen() {
- HANDLE hStdOut;
- hStdOut = GetStdHandle(STD_OUTPUT_HANDLE);
- // Fetch existing console mode so we correctly add a flag and not turn off others
- DWORD mode = 0;
- if (!GetConsoleMode(hStdOut, &mode))
- {
- return;
- }
- // Hold original mode to restore on exit to be cooperative with other command-line apps.
- const DWORD originalMode = mode;
- mode |= ENABLE_VIRTUAL_TERMINAL_PROCESSING;
- // Try to set the mode.
- if (!SetConsoleMode(hStdOut, mode))
- {
- return;
- }
- // Write the sequence for clearing the display.
- DWORD written = 0;
- PCWSTR sequence = L"\x1b[2J";
- if (!WriteConsoleW(hStdOut, sequence, (DWORD)wcslen(sequence), &written, NULL))
- {
- // If we fail, try to restore the mode on the way out.
- SetConsoleMode(hStdOut, originalMode);
- return;
- }
- // To also clear the scroll back, emit L"\x1b[3J" as well.
- // 2J only clears the visible window and 3J only clears the scroll back.
- // Restore the mode on the way out to be nice to other command-line applications.
- SetConsoleMode(hStdOut, originalMode);
- }
Add Comment
Please, Sign In to add comment