Don't. There is no reason to clear a console screen. It's designed for input and output streams, not pretty graphics. If you want to code pretty graphics, learn a proper GUI package like Qt, wx, etc... |
I guess according to you then there would of been no reason for early game developers to make games to run on the DOS OS. It wasn't meant to run games it was meant for serious business. :P
To the OP:
Binarybob's example works, but getting the return value of the functions with bSuccess and not doing anything with it is a bit silly. Here's a function that Duoas provided somewhere that I grabbed a good while back:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38
|
void ClearScreen()
{
HANDLE hStdOut;
CONSOLE_SCREEN_BUFFER_INFO csbi;
DWORD count;
DWORD cellCount;
COORD homeCoords = { 0, 0 };
hStdOut = GetStdHandle( STD_OUTPUT_HANDLE );
if (hStdOut == INVALID_HANDLE_VALUE) return;
/* Get the number of cells in the current buffer */
if (!GetConsoleScreenBufferInfo( hStdOut, &csbi )) return;
cellCount = csbi.dwSize.X *csbi.dwSize.Y;
/* Fill the entire buffer with spaces */
if (!FillConsoleOutputCharacter(
hStdOut,
(TCHAR) ' ',
cellCount,
homeCoords,
&count
)) return;
/* Fill the entire buffer with the current colors and attributes */
if (!FillConsoleOutputAttribute(
hStdOut,
csbi.wAttributes,
cellCount,
homeCoords,
&count
)) return;
/* Move the cursor home */
SetConsoleCursorPosition( hStdOut, homeCoords );
}
|
And personally I say doing stuff like this with the console is fine when you're learning. It's definitely not what the console was designed for, but if it was never meant to clear itself then the command 'CLS' wouldn't exist (Go type that directly into a command prompt). Basically, if you want to do stuff like this for something serious you don't really want to rely on the console. But for messing around and making a simple game, it can be a pretty awesome way to learn.
I owe a good bit of my advancement in programming to trying to get the console to function in ways it wasn't really intended. For example, print out a border with a dimension of 54 cells wide by, say, 10 cells deep. Then try to figure out how you can get the console to print messages inside that box, and wrap them logically (based on the whole word, rather than cutting mid word). You have to program a lot of the functionality yourself, but most of it can be done with logic and doesn't require learning the ins and outs of a library. You can pretty much do this whole border/word wrap thing with SetConsoleCursorPosition(), strings, and some creative thinking.
Most importantly, that I feel anyway, is the fact that it makes you have to think a lot. Think about how you can get the console to do what you want it to when there are no functions to do so.