Sign in to follow this  
Joshnathan

A different way to clear my screen

Recommended Posts

Joshnathan    124
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 this post


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

Share this post


Link to post
Share on other sites
Gaunlet    122
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 this post


Link to post
Share on other sites
GamerCon    144
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 this post


Link to post
Share on other sites
stormrunner    720
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 this post


Link to post
Share on other sites
Fruny    1658
Quote:
Original post by Joshnathan
Hi all, I have a simple question. How can I clear my screen without using system("cls") ?


Without more information about your system, I'll just tell you to puts("\x1B[2J");

It works perfectly fine on my box.

Share this post


Link to post
Share on other sites

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

Sign in

Already have an account? Sign in here.

Sign In Now

Sign in to follow this