Clear the Screen using WinApi functions

Please support our C advertiser: Programming Forums - DaniWeb Sister Site
vegaseat vegaseat is offline Offline Mar 26th, 2005, 11:30 am |
0
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.
Quick reply to this message  
C Syntax
  1. // test of the official Windows Console clear the screen function
  2. // tested with Pelles C vegaseat 26mar2005
  3.  
  4. #include <stdio.h>
  5.  
  6. void clrscr(void);
  7.  
  8. #include <windows.h>
  9.  
  10. void clrscr(void)
  11. {
  12. COORD coordScreen = { 0, 0 }; // upper left corner
  13. DWORD cCharsWritten;
  14. DWORD dwConSize;
  15. HANDLE hCon = GetStdHandle(STD_OUTPUT_HANDLE);
  16. CONSOLE_SCREEN_BUFFER_INFO csbi;
  17.  
  18. GetConsoleScreenBufferInfo(hCon, &csbi);
  19. dwConSize = csbi.dwSize.X * csbi.dwSize.Y;
  20. // fill with spaces
  21. FillConsoleOutputCharacter(hCon, TEXT(' '), dwConSize, coordScreen, &cCharsWritten);
  22. GetConsoleScreenBufferInfo(hCon, &csbi);
  23. FillConsoleOutputAttribute(hCon, csbi.wAttributes, dwConSize, coordScreen, &cCharsWritten);
  24. // cursor to upper left corner
  25. SetConsoleCursorPosition(hCon, coordScreen);
  26. }
  27.  
  28.  
  29. int main(void)
  30. {
  31. int k;
  32.  
  33. for(k = 0; k < 25; k++)
  34. {
  35. printf("Test........\n");
  36. }
  37. printf("Press Enter...");
  38. getchar();
  39. clrscr();
  40. getchar(); // wait
  41. return 0;
  42. }
  43.  

Message:


Similar Threads
Thread Tools Search this Thread



About Us | Contact Us | Advertise | DaniWeb | Acceptable Use Policy | RSS Feed

©2003 - 2009 DaniWeb® LLC