0

i have these code for clear the screen:

//clear the Console
    void Clear(int BackColor=0)
    {
         // home for the cursor
        COORD coordScreen = {0, 0};
        DWORD cCharsWritten;
        CONSOLE_SCREEN_BUFFER_INFO csbi;
        DWORD dwConSize;

        // Get the number of character cells in the current buffer
        if(!GetConsoleScreenBufferInfo(GetStdHandle(STD_OUTPUT_HANDLE), &csbi))
            return;
        dwConSize = csbi.dwSize.X * csbi.dwSize.Y;

        // Fill the entire screen with blanks
        if(!FillConsoleOutputCharacter(GetStdHandle(STD_OUTPUT_HANDLE), (WCHAR)' ', dwConSize, coordScreen, &cCharsWritten))
            return;
        SetConsoleTextAttribute(GetStdHandle(STD_OUTPUT_HANDLE), 15|BackColor<<4 );
        // Get the current text attribute.
        if(!GetConsoleScreenBufferInfo(GetStdHandle(STD_OUTPUT_HANDLE), &csbi))
            return;
        // Set the buffer's attributes accordingly.
        if(!FillConsoleOutputAttribute(GetStdHandle(STD_OUTPUT_HANDLE), csbi.wAttributes, dwConSize, coordScreen, &cCharsWritten))
            return;
        // Put the cursor at its home coordinates.
        SetConsoleCursorPosition(GetStdHandle(STD_OUTPUT_HANDLE), coordScreen);
    }

i see at least 1 problem: after clear, the text color is, always, white and not the last color. can anyone advice me?

2
Contributors
4
Replies
22
Views
3 Years
Discussion Span
Last Post by cambalinho
1

You need to get the console text attributes before changing them, then reset them after you're done (either before or after setting the cursor position).

Edited by deceptikon

0

finally i fix it:

//clear the Console
    void Clear(int BackColor=0)
    {
         // home for the cursor
        COORD coordScreen = {0, 0};
        DWORD cCharsWritten;
        CONSOLE_SCREEN_BUFFER_INFO csbi;
        DWORD dwConSize;

        // Get the number of character cells in the current buffer
        if(!GetConsoleScreenBufferInfo(GetStdHandle(STD_OUTPUT_HANDLE), &csbi))
            return;
        dwConSize = csbi.dwSize.X * csbi.dwSize.Y;

        // Fill the entire screen with blanks
        if(!FillConsoleOutputCharacter(GetStdHandle(STD_OUTPUT_HANDLE), (WCHAR)' ', dwConSize, coordScreen, &cCharsWritten))
            return;

        // Get the current text attribute.
        if(!GetConsoleScreenBufferInfo(GetStdHandle(STD_OUTPUT_HANDLE), &csbi))
            return;
        csbi.wAttributes=csbi.wAttributes | BackColor; //heres i change the backcolor
        SetConsoleTextAttribute(GetStdHandle(STD_OUTPUT_HANDLE), csbi.wAttributes );
        // Set the buffer's attributes accordingly.
        if(!FillConsoleOutputAttribute(GetStdHandle(STD_OUTPUT_HANDLE), csbi.wAttributes, dwConSize, coordScreen, &cCharsWritten))
            return;
        // Put the cursor at its home coordinates.
        SetConsoleCursorPosition(GetStdHandle(STD_OUTPUT_HANDLE), coordScreen);
    }

if these line isn't correct:

csbi.wAttributes=csbi.wAttributes | BackColor; //heres i change the backcolor

please tell me.
thanks for all

Note: i change my write() function for accept the blink... very cool and works fine ;)

Edited by cambalinho

0

seems that i don't fix it:(
from here:

csbi.wAttributes

how can i calculate the Backcolor and textcolor?

0

with another help, i fix the code:

//clear the Console
    void Clear(int BackColor=-1)
    {
         // home for the cursor
        COORD coordScreen = {0, 0};
        DWORD cCharsWritten;
        CONSOLE_SCREEN_BUFFER_INFO csbi;
        DWORD dwConSize;

        // Get the number of character cells in the current buffer
        if(!GetConsoleScreenBufferInfo(GetStdHandle(STD_OUTPUT_HANDLE), &csbi))
            return;
        dwConSize = csbi.dwSize.X * csbi.dwSize.Y;

        // Fill the entire screen with blanks
        if(!FillConsoleOutputCharacter(GetStdHandle(STD_OUTPUT_HANDLE), (WCHAR)' ', dwConSize, coordScreen, &cCharsWritten))
            return;

        // Get the current text attribute.
        if(!GetConsoleScreenBufferInfo(GetStdHandle(STD_OUTPUT_HANDLE), &csbi))
            return;
        if (BackColor!=-1)
        {
            DWORD textcolor = csbi.wAttributes & 0xff0f;
            //DWORD backcolor = (csbi.wAttributes & 0xfff0) >> 4;
            csbi.wAttributes=textcolor | BackColor ;//here we change the backcolor
        }
        SetConsoleTextAttribute(GetStdHandle(STD_OUTPUT_HANDLE), csbi.wAttributes );
        // Set the buffer's attributes accordingly.
        if(!FillConsoleOutputAttribute(GetStdHandle(STD_OUTPUT_HANDLE), csbi.wAttributes, dwConSize, coordScreen, &cCharsWritten))
            return;
        // Put the cursor at its home coordinates.
        SetConsoleCursorPosition(GetStdHandle(STD_OUTPUT_HANDLE), coordScreen);
    }

now, when i clear the screen i can use the last colors(backcolor=-1) or change the backcolor.
thanks for all

This question has already been answered. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.