944,040 Members | Top Members by Rank

Ad:
  • C Code Snippet
  • Views: 8367
  • C RSS
0

Clear the Screen using WinApi functions

by on Mar 26th, 2005
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.
C Code Snippet (Toggle Plain Text)
  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:
Previous Thread in C Forum Timeline: searching and inserting node in a binary search tree
Next Thread in C Forum Timeline: Adding decimal numbers together?





About Us | Contact Us | Advertise | Acceptable Use Policy
Forum Index | Build Custom RSS Feed


Follow us on Twitter


© 2011 DaniWeb® LLC