Clearning the screen in a Win32 Console Application.. (C++)

Started by
10 comments, last by ApochPiQ 12 years, 9 months ago
Howdey! Does anyone have a clue on how one would go about clearing the screen in a Win32 Console Application in C++?
    
cout << endl << endl << endl << endl << endl << endl << endl << endl << endl << endl << endl << endl << endl;
cout << endl << endl << endl << endl << endl << endl << endl << endl << endl << endl << endl;
   

 and

       
 cout << "\n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n";
     
are the only things I have been able to get to work.. But they look horable in source.. as you can see. -Kedaeus- Edited by - Kedaeus on June 21, 2001 3:35:10 PM
-Eater of Files-
Advertisement
Clearing the screen using the Win32 Console API:
  bool Clear() {   HANDLE hconsole = GetStdHandle (STD_OUTPUT_HANDLE);   // get the number of character cells in the current buffer   CONSOLE_SCREEN_BUFFER_INFO csbi;   if (!GetConsoleScreenBufferInfo (hconsole, &csbi))       return false;   COORD coordScreen = { 0, 0 };    // here''s where we''ll home the cursor   DWORD cCharsWritten;             // number of chars written by console output routines   DWORD dwConSize = csbi.dwSize.X * csbi.dwSize.Y;   // fill the entire screen with blanks   return (FillConsoleOutputCharacter (hconsole, '' '', dwConSize, coordScreen, &cCharsWritten)              &&           FillConsoleOutputAttribute (hconsole, csbi.wAttributes, dwConSize, coordScreen, &cCharsWritten) &&           SetConsoleCursorPosition   (hconsole, coordScreen));}  


[Resist Windows XP''s Invasive Production Activation Technology!]
Holy...

Just to clear the screen?

Can someone tell me what was wrong with the BASIC statement "CLS"?!

Thanks for the info.

Also.. a question on using this knowledge you have bestowed upon me..

I'm still rather new to C++ and doubt I would know how to use this..

Every time, after that is defined, I wanted to clear the screen I would just use:

   clear();   
?

Also.. If I wanted to take that and create a header file (to save me from having to do that every time I created a win32 console App) would it be possable for me to just #include that in my project?

I'm sorry i'm asking so many questions. It's a habbit of mine.

-Kedaeus-

-Eater of Files-

Edited by - Kedaeus on June 21, 2001 5:10:15 PM
-Eater of Files-
Yes, you have to do all that just to clear the screen , and yes, with the above function, you just have to call clear(). Oh, and yes, you could put that in a header file ... although a better idea might be to put all console-related functions in a .cpp file, and add that file to your project.

~~~~~~~~~~
Martee
ReactOS - an Open-source operating system compatible with Windows NT apps and drivers
If you're trying to clear the screen in a console app then you can make use of system calls to clear the screen.
You need to #include to use the system() function.

Here's the syntax for clearing the screen, it's pretty simple:
system("cls");

And here's an overly complicated demonstration of this simple concept:

  #include <stdlib.h>#include <fstream.h>void main(){        int loop_on = 1;	char choice;	cout << "Press '1' to display message 1, press '2' todisplay message 2. \n";	cout << "Press the Q key to quit. \n";	while(loop_on == 1)	{		cin >> choice;		switch (choice)		{		case '1':			system("cls");			cout << "message 1!!! \n";			break;		case '2':			system("cls");			cout << "message 2!!! \n";			break;		case 'q':			system("cls");			loop_on--;			break;		}	}}  


Edited by - eotvos on June 21, 2001 6:22:54 PM
Yeah, you could do that, but I suspect that it would involve a performance hit. Also, you are relying on the assumption that the command processor supports the ''cls'' command. Who knows what future versions of Windows will do?

~~~~~~~~~~
Martee
ReactOS - an Open-source operating system compatible with Windows NT apps and drivers
Yeah, you probably won't do any ascii animation with it :D.
I dunno what Kedaeus wants to clear the screen for, but for a console app it's alot better than cout << endl << endl etc..
I'm using Win98 SE, so I can't gurantee it'll work on any later versions.

Kedaeus: If you use clear(), don't forget to to put
    #define WIN32_LEAN_AND_MEAN#include <windows.h>    

at the top of your source, won't work without it.




Edited by - eotvos on June 21, 2001 7:43:22 PM
I''m using it for a text based adventure..

So.. no, no animation is involved.

Thanks, it took me a while but I finaly remembered that

-Kedaeus-
-Eater of Files-
#include

...


system("CLS"); //clear the screen

...

hope that helped.
-Dave



---------------------------------------
"When I lie, I am telling the truth"
thats #include (stdlib.h) brackets are HTML, I forgot!

---------------------------------------
"When I lie, I am telling the truth"

This topic is closed to new replies.

Advertisement