Welcome to Dream.In.Code
Getting C++ Help is Easy!

Join 136,047 C++ Programmers for FREE! Get instant access to thousands of C++ experts, tutorials, code snippets, and more! There are 1,557 people online right now. Registration is fast and FREE... Join Now!




Clearing the Screen in a Win32 Console Application (C++)

 
Reply to this topicStart new topic

Clearing the Screen in a Win32 Console Application (C++), using system("cls") gives a green screen

cJr.
4 Aug, 2008 - 11:56 AM
Post #1

New D.I.C Head
*

Joined: 10 May, 2008
Posts: 30


My Contributions
I am currently in the process of developing my own Win32 console application, trying to incorporate all of the C++ theory we have learnt in the first year of uni, for revision purposes.

In order to do this, I obviously have to clear the screen of the console, for example, when I would like to display the menu, I need to get rid of the text from my word count feature.

I have tried the system("cls") from this Microsoft Help page & have also tried the Clrscr(); function, similar to the one in the Borland compiler, yet they both end up giving me a green screen on the console, instead of black (the only things that aren't green are the text from my displayMenu & displayHeaderFooter, functions & the background of that text.

Here is part of the code in my whole application which I think you will need to help:

CODE

//---PART OF THE CODE IN THE MAIN FUNCTION (int main)---\\


//DISPLAYING THE MENU AFTER AGE APPROVAL
  keyPressed = getKeyPress();
    if (keyPressed == 'M')
        {
            system("cls");
            displayHeaderFooter(currentMonth, currentYear);
            displayMenu(keyPressed, currentMonth, currentYear);
        }


//---THE TWO FUNCTIONS I THINK YOU NEED TO HELP ME---\\


void displayHeaderFooter( int& currentMonth, int& currentYear)
{ //to paint the universal header and footer of the program on the screen
    
    int currentDate;
    int currentHour, currentMinute, currentSecond;
    
    
    //FOOTER: COPYRIGHT MESSAGE:
    Gotoxy(0, 23);
    SelectTextColour( clDarkCyan);
    cout << "--------------------------------------------------------------------------------"<< endl;
    Gotoxy(0, 24);
    SelectTextColour( clDarkGrey);
    cout << "Developed by Calum Richardson © 2008"<< endl;

    //HEADER: WELCOME MESSAGE TO THE USER:
    Gotoxy(0, 0);
    SelectTextColour( clGrey);
    SelectBackColour( clDarkCyan);
    cout << "        " << "WELCOME" << "          " << endl;
    Gotoxy(22, 0);
    SelectTextColour( clDarkCyan);
    SelectBackColour( clBlack);
    cout << "________________________________________________"<< endl;

    //HEADER: RETRIEVING THE CURRENT DATE AND TIME:
    time_t result;  
    result = time(NULL);                          //retrieving current date and time in long seconds  
    struct tm* tp = localtime(&result);          //converting current (local)time into a struct for time attributes  
    currentDate  = tp->tm_mday;
    currentMonth = tp->tm_mon + 1;                //month since January; May would be 4, add 1  
    currentYear  = tp->tm_year + 1900;           //year since 1900; 2008 would be 108  
    currentHour   = tp->tm_hour;  
    currentMinute    = tp->tm_min;  
    currentSecond    = tp->tm_sec;  
  
    //DISPLAYING THE CURRENT DATE AND TIME:  
    Gotoxy(57, 0);
    SelectBackColour( clDarkCyan);
    SelectTextColour( clGrey);
    cout << " " << currentDate << "/" << currentMonth << "/" << currentYear << "      ";
    Gotoxy(68, 0);
    cout << "- ";
    Gotoxy(71, 0);
    cout << currentHour << ":" << currentMinute << ":" << currentSecond << " ";

    //DISPLAYING END OF HEADER:
    Gotoxy(0, 3);
    SelectBackColour( clBlack);
    SelectTextColour( clDarkGrey);
    cout << "--------------------------------------------------------------------------------"<< endl;
} //end of the 'displayHeaderFooter' function



void displayMenu(int& keyPressed, int& currentMonth, int& currentYear)
{ //to the menu in the console
    
    int getKeyPress();

    void wordCounter(int& keyPressed, int& currentMonth, int& currentYear);
    void endProgram();
    
    Gotoxy(6, 6);
    SelectTextColour( clGrey);
    SelectBackColour( clBlack);
    cout << "Please press the letter corresponding the option you wish to choose:";
    Gotoxy(28, 9);
    SelectTextColour( clDarkCyan);
    cout << "Word count - (W)";
    Gotoxy(28, 10);
    cout << "Number count - (N)";
    Gotoxy(28, 11);
    cout << "Mouse & Cheese game - (G)";
    Gotoxy(28, 12);
    SelectTextColour( clGreen);
    cout << "HELP - (H)";
    Gotoxy(28, 13);
    SelectTextColour( clYellow);
    cout << "ABOUT - (A)";
    Gotoxy(28, 14);
    SelectTextColour( clRed);
    cout << "EXIT - (E)";
    
    keyPressed = getKeyPress();
    if (keyPressed == 'W')
        {
            wordCounter(keyPressed, currentMonth, currentYear);
        }

    keyPressed = getKeyPress();
    if (keyPressed == 'E')
        {
            endProgram();
        }

} //end of the 'displayMenu' function




Any help really would be much appreciated. I'd just like the screen to be cleared to a black colour before I paint the Header, Footer & Menu onto the screen, instead of being cleared green.

Thank you in advance.
Calum.


----------------
Now playing on iTunes: All-4-One - I Swear
via FoxyTunes
User is offlineProfile CardPM
+Quote Post

PixelCard
RE: Clearing The Screen In A Win32 Console Application (C++)
4 Aug, 2008 - 11:59 AM
Post #2

D.I.C Head
Group Icon

Joined: 20 Jun, 2008
Posts: 139



Thanked: 3 times
Dream Kudos: 650
My Contributions
Take a look at this article (Microsoft Knowledge Base):
http://support.microsoft.com/kb/99261
User is offlineProfile CardPM
+Quote Post

cJr.
RE: Clearing The Screen In A Win32 Console Application (C++)
4 Aug, 2008 - 12:16 PM
Post #3

New D.I.C Head
*

Joined: 10 May, 2008
Posts: 30


My Contributions
Thank you for the reply.

Yes that is where I actually got the information about system("cls") from. I meant to link it in my original post. I have tried both ways described in that article, but it still appears green instead of black.

Is there anything else I can try.

This post has been edited by cJr.: 4 Aug, 2008 - 12:26 PM
User is offlineProfile CardPM
+Quote Post

NickDMax
RE: Clearing The Screen In A Win32 Console Application (C++)
4 Aug, 2008 - 12:28 PM
Post #4

2B||!2B
Group Icon

Joined: 18 Feb, 2007
Posts: 2,858



Thanked: 49 times
Dream Kudos: 550
My Contributions
I have a snippet on win32 console... You can change the forground and background colors that will be used for a clear screen. I didn't make a nice enumeration or set of constants for the various colors (I think they are defined in the windows.h -- but I am not sure). but here are the basics:

0 - black
1 - blue
2 - green
3 - cyan
4 - red
5 - magenta
6 - orange (ish)
7 - gray

Colors 8-15 are a little different since they have a different affect when selected as the background... (I believe they make the text blink)

8 - drk grey (bright black is an easy way to remember these 0 + 8)
9 - bright blue
10 - bright green
11 - bright cyan
12 - bright red
13 - bright magenta
14 - Yellow (bright orange)
15 - White (bright gray)

User is offlineProfile CardPM
+Quote Post

cJr.
RE: Clearing The Screen In A Win32 Console Application (C++)
6 Aug, 2008 - 11:04 AM
Post #5

New D.I.C Head
*

Joined: 10 May, 2008
Posts: 30


My Contributions
Thank you for your help Nick.

However, I realised that the reason why it was changing the background to green was because of my "SelectBackColour( clGreen)" code from my own library (which I got from Uni). Clearing the screen worked perfectly, if I put "SelectBackColour( clBlack)" after the text I had changed to green.

Thank you once again.
User is offlineProfile CardPM
+Quote Post

Reply to this topicStart new topic
Time is now: 12/1/08 05:27PM

Live C++ Help!

C++ Tutorials

Reference Sheets

C++ Snippets

DIC Chatroom

Bye Bye Ads

Monthly Drawing

Thumb Drive

Top Contributors

Top 10 Kudos This Month