for the past few hours i have been searching for at least the briefest explanation on handles and commands within windows.h and cant seem to find a single one and the the other posts on the same subject direct you to sites with no info at all can someone send a link or explain the use of handles in the specific function below? (it uses only the windows.h library for whom i cant find a single explanation on how to use).
I'll see if I can do any justice. This doesn't clear the screen, this just relocates your DOS/Console courser to the top left corner. Standard default console is 80x24 characters in size, but since XP, the console can now have a lot of rows which allows you to scroll back up to see what went off the screen. Therefore, you have to be mindful of how many lines that you have passed beyond the 24 lines. Therefore, when you place the cursor to the top left corner, you still have all your old text on the screen, and as many rows down as you were.
HANDLE is a predefined class .. I believe, and we create a copy of it called hOut.
- I don't know why it's called HANDLE, but I get the impression that this is something the Kernel needs to handle directly as a threaded object.
COORD is a predefined class/struct I believe, and it makes it easier to pass multiple values around as just a predefined class/struct. We created a copy of COORD called Position.
In our class hOut we want to define how it behaves, so "Get-Standard-Handle" type STD_OUTPUT_HANDLE is stuffed into our class. What ever those pre-defined values are.
We set the X & Y to zero which is the top left corner of the display, but no text has been changed.
To actually clear the screen, you'll have find the buffer size and write a space to it ' '. Here is some code I found which was some Windows Kung-fu:
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 );
}
thank you for the explanation! but is there any shorter way to clear the screen without system("cls") and without looping manually and putting spaces? lets say using conio.h or
consoleapi.h? because i cant find explanations to them aswell it directs me to math.h and it has nothing to do with the other 2 libraries and is there any examples in the sites tutorial file?
i know its to much but i really cant find anything and want to learn it :(
There is no "system("cls");" in the code I showed you. The code is strung out to be easily readable, so here is the code again, but this time compressed. It's not that big at all and it does all this weird crazy looping for you, so you don't have to stress about it. This is a function you can call any time by the name ClearScreen();
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 );
}
All you have to do is put this text above into a file like MyClearScreen.h, then include a line in your main file with the other, for example #include "MyClearScreen.h" and then any where in your code where you want to clear the screen you put in the line: ClearScreen();
I was thinking about your comments and I wanted to add a few suggestions.
When I first started I got myself two books. One was a "Teach yourself C++ in 21 Days" book, and the other one was, "A Complete Reference to C++"
And yes, books are still very useful!
You've clearly found this website which is a great source.
thanks man you helped me a lot ! i'm spending about 5-6 hours a day learning c++ and programming and don't get discouraged i am just the type of person that wants to know every fact and use possible for the commands/functions i find and learn, and always want to find the most effective way possible to write my code.