OS Min: Win9x / Win200/XP
Inc Header: Windows.h
Library: Kernel32.lib
This function is used to clear the console screen of console applications and return the caret to home. It is a win32 version of the msdos cls command.
Code:
//
// cls()
//
// Clear the console screen
//
void cls( void )
{
//get console output handle
HANDLE hCon = GetStdHandle(STD_OUTPUT_HANDLE);
//get console info
CONSOLE_SCREEN_BUFFER_INFO csbi;
GetConsoleScreenBufferInfo(hCon, &csbi);
//calculate number of charaters in console
DWORD br;
DWORD N = csbi.dwSize.X * csbi.dwCursorPosition.Y + csbi.dwCursorPosition.X + 1;
//write spaces to the console thus clearing it
COORD curhome = {0,0};
FillConsoleOutputCharacter(hCon, ' ', N, curhome, &br);
//set info
csbi.srWindow.Bottom -= csbi.srWindow.Top;
csbi.srWindow.Top = 0;
SetConsoleWindowInfo(hCon, TRUE, &csbi.srWindow);
//return the cursor to home
SetConsoleCursorPosition(hCon, curhome);
}
// cls()
//
// Clear the console screen
//
void cls( void )
{
//get console output handle
HANDLE hCon = GetStdHandle(STD_OUTPUT_HANDLE);
//get console info
CONSOLE_SCREEN_BUFFER_INFO csbi;
GetConsoleScreenBufferInfo(hCon, &csbi);
//calculate number of charaters in console
DWORD br;
DWORD N = csbi.dwSize.X * csbi.dwCursorPosition.Y + csbi.dwCursorPosition.X + 1;
//write spaces to the console thus clearing it
COORD curhome = {0,0};
FillConsoleOutputCharacter(hCon, ' ', N, curhome, &br);
//set info
csbi.srWindow.Bottom -= csbi.srWindow.Top;
csbi.srWindow.Top = 0;
SetConsoleWindowInfo(hCon, TRUE, &csbi.srWindow);
//return the cursor to home
SetConsoleCursorPosition(hCon, curhome);
}












