We're a community of 1076K IT Pros here for help, advice, solutions, professional growth and fun. Join us!
1,075,527 Members — Technology Publication meets Social Media
Username:
Password:
Lost login information?

Clear the Screen using WinApi functions

1
By vegaseat on Mar 26th, 2005 9:30 pm

Many of the code samples on the internet are written in Turbo C. The clrscr() function litters these samples, and even to this day people want to wipe your screen. You can use system("CLS"), but that uses a DOS command. DOS commands are with us only at the whim of William the Mighty. Here is the Windows version of clearing the console screen with WinApi functions, in the hope that these functions will stay around a little longer.

// test of the official Windows Console clear the screen function
// tested with Pelles C    vegaseat     26mar2005

#include <stdio.h>

void clrscr(void);

#include <windows.h> 

void clrscr(void)
{
  COORD coordScreen = { 0, 0 };  // upper left corner
  DWORD cCharsWritten;
  DWORD dwConSize;
  HANDLE hCon = GetStdHandle(STD_OUTPUT_HANDLE);
  CONSOLE_SCREEN_BUFFER_INFO  csbi;
    
  GetConsoleScreenBufferInfo(hCon, &csbi);
  dwConSize = csbi.dwSize.X * csbi.dwSize.Y;
  // fill with spaces
  FillConsoleOutputCharacter(hCon, TEXT(' '), dwConSize, coordScreen, &cCharsWritten);
  GetConsoleScreenBufferInfo(hCon, &csbi);
  FillConsoleOutputAttribute(hCon, csbi.wAttributes, dwConSize, coordScreen, &cCharsWritten);
  // cursor to upper left corner
  SetConsoleCursorPosition(hCon, coordScreen);
}


int main(void)
{
  int k;
  
  for(k = 0; k < 25; k++)
  {
    printf("Test........\n");
  }
  printf("Press Enter...");
  getchar();
  clrscr();
  getchar();  // wait  
  return 0;
}

Post: Markdown Syntax: Formatting Help
 
You
 
© 2013 DaniWeb® LLC
Page rendered in 0.0635 seconds using 2.64MB