Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Mike Lewis on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Clearscreen

Status
Not open for further replies.

teser

Technical User
Mar 6, 2001
194
US

How can I get the clear screen to work in my Visual C++ 6.0.

I tried the 'system("cls");' with no luck.

It works with 'clrscr();' in Borland but I need to get it to work in Visual C++.
 
Usually the system("cls") should work.
However, there is another , more complicated way of clearing the screen by using API functions from MSDN especially FillConsoleOutputCharacter

You have a sample of its usage in the following article from MSDN:
"HOWTO: Performing Clear Screen (CLS) in a Console Application"

HTH, s-)

Blessed is he who in the name of justice and goodwill, sheperds the weak through the valley of darkness...
 
The following code might look like overkill for just clearing the screen but it works... :)

int clear_console()
{

// Get the handle to Standard Output (STDOUT)

HANDLE hndlStdOut = GetStdHandle(STD_OUTPUT_HANDLE);
CONSOLE_SCREEN_BUFFER_INFO csBi;

// Fill console screen buffer
GetConsoleScreenBufferInfo(hndlStdOut, &csBi);
DWORD written;
DWORD SZ;

SZ = csBi.dwSize.X * csBi.dwCursorPosition.Y;
// csBi.dwCursorPosition.X + 1;
COORD curhome = {0, 0};

FillConsoleOutputCharacter(hndlStdOut, SPACE, SZ, curhome, &written);

csBi.srWindow.Bottom -= csBi.srWindow.Top;
csBi.srWindow.Top = 0;

SetConsoleWindowInfo(hndlStdOut, TRUE, &csBi.srWindow);
SetConsoleCursorPosition(hndlStdOut, curhome);

return 0;
}


Anand
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
Mail me at abpillai@excite.com if you want additional help. I dont promise that I can solve your problem, but I will try my best.
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
 
And a slightly simpler way of doing it:

void clearScreen()
{
HANDLE hConsole = GetStdHandle(STD_OUTPUT_HANDLE);
for (int i = 0; i<50; i++) printf(&quot;\n&quot;);
COORD pos0 = {0,0};
SetConsoleCursorPosition(hConsole,pos0);
}

 
Make sure you include the stdlib.h and system(&quot;cls&quot;) should work.

#inclide <stdlib.h>
 
Status
Not open for further replies.

Similar threads

Part and Inventory Search

Sponsor

Back
Top