Home > C++, VC++, Windows API > How to clear screen in MS VC?

How to clear screen in MS VC?

One option is to use

system( "cls" );

Another option is to to use this function. I copied this from MSDN.  😉

void cls( HANDLE hConsole )
{
   /* here's where we'll home the cursor */        

   COORD coordScreen = { 0, 0 };       

   BOOL bSuccess = FALSE;
   DWORD cCharsWritten = 0;       

   /* to get buffer info */
   CONSOLE_SCREEN_BUFFER_INFO csbi = { 0 };       

   /* number of character cells in the current buffer */
   DWORD dwConSize;

   /* get the number of character cells in the current buffer */
   bSuccess = GetConsoleScreenBufferInfo( hConsole, &csbi );
   dwConSize = csbi.dwSize.X * csbi.dwSize.Y;

   /* fill the entire screen with blanks */
   bSuccess = FillConsoleOutputCharacter( hConsole,
				     ( TCHAR )' ',
				     dwConSize,
				     coordScreen,
				     &cCharsWritten );       

   /* get the current text attribute */
   bSuccess = GetConsoleScreenBufferInfo( hConsole, &csbi );

   /* now set the buffer's attributes accordingly */
   bSuccess = FillConsoleOutputAttribute( hConsole,
				     csbi.wAttributes,
			              dwConSize,
				     coordScreen,
				     &cCharsWritten );
   /* put the cursor at (0, 0) */
   bSuccess = SetConsoleCursorPosition( hConsole, coordScreen );
   return;
}// End cls
Advertisements
  1. No comments yet.
  1. No trackbacks yet.

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

%d bloggers like this: