# A different way to clear my screen

## Recommended Posts

Hi all, I have a simple question. How can I clear my screen without using system("cls") ? I try'd using a function that copies black text over the whole screen, but after I almost finished it I realized I didn't know how to create black text... (I can do red. green, blue, orange etc, but NO BLACK ;( ). Thank you all for at least looking at my post :D Joshua

##### Share on other sites
Im not sure how to make black text, but couldnt you just flood the screen with '\n' new line characters or ' ' space characters?

##### Share on other sites
no :( already try'd that

##### Share on other sites
Well, I can't understand, why you don't want to use system("cls") or "clean" the screens with a loop of "\n"s .

Ok, here is a link to the msdn-page - hope that helps you: http://msdn.microsoft.com/library/default.asp?url=/library/en-us/dllproc/base/clearing_the_screen.asp

Alexander

##### Share on other sites
If youre using windows.h and have your function ready to output black text every where needed then, SetConsoleTextAttribute(HANDLE,color);
where color would normaly be FOREGROUND_RED | BACKGROUND_GREEN ect.

use 0 for the color and voila, you've got black.

##### Share on other sites
try this - it writes over the buffer with empty characters ( ' ' ). just make sure (if you modify it) that the foreground attributes are defined in the loop (to something) otherwise the console becomes highly pixelated. width and height need to be defined.
void ClearConsole(HANDLE h){  CHAR_INFO screenBuffer[width * height];  SMALL_RECT drawRect = {0, 0, width - 1, height - 1};  HANDLE cls;  if(!h)    cls = GetStdHandle(STD_OUTPUT_HANDLE); // the output handle, could be globally defined  else    cls = h;  COORD gridSize = {width , height};   // This is a COORD that holds the width and height of our grid that we will display  COORD zeroZero = {0, 0};  for(int y = 0; y < height; y++)  {     for(int x = 0; x < width; x++)     {        // set the character to '' - clear        screenBuffer[x + y * width].Char.AsciiChar = ' ';        // set the foreground attribute        // doesn't need to be anything special, just defined        screenBuffer[x + y * width].Attributes = FOREGROUND_RED | BACKGROUND_GREEN;     }  }  // write it all to the buffer  WriteConsoleOutput(cls, screenBuffer, gridSize, zeroZero, &drawRect);}

hope that helped.
cheers.

##### Share on other sites
Quote:
 Original post by JoshnathanHi all, I have a simple question. How can I clear my screen without using system("cls") ?

It works perfectly fine on my box.

## Create an account or sign in to comment

You need to be a member in order to leave a comment

## Create an account

Sign up for a new account in our community. It's easy!

Register a new account

• ### Forum Statistics

• Total Topics
628334
• Total Posts
2982147

• 9
• 24
• 9
• 9
• 13