Go4Expert

Go4Expert (http://www.go4expert.com/)
-   Win32 (http://www.go4expert.com/articles/win32-tutorials/)
-   -   Add color to console projects (http://www.go4expert.com/articles/add-color-console-projects-t2965/)

Sanskruti 12Feb2007 18:50

Add color to console projects
 
When a new process is created, the "subsystem" field in the executable file header determines whether it was linked as a console (also called CUI or "character-based") or graphical user interface (GUI) application. A console application has a character-mode console window, which it inherits from its parent process upon its creation; if the parent process is not attached to a console, Windows creates a new console and is associates it with the launched application. A newly created GUI application is not attached to a console even if it is launched from a console application.

In C++ it is not possible to change colors but it can be done with help of Win 32. It is not necessary to concern yourself over the values of the various bits. There are a series of constants defined in windows.h, (actually in wincon.h but that is included in windows.h), which symbolically allow you to build the required value, it is very easy.

Look at this program

Code: CPP

#include <windows.h>
#include <iostream.h>

int main()
{
    HANDLE hOut;

    hOut = GetStdHandle(STD_OUTPUT_HANDLE);

    SetConsoleTextAttribute(hOut,
                            FOREGROUND_RED);
    cout << "This text is red." << endl;

    SetConsoleTextAttribute(hOut,
                            FOREGROUND_GREEN);
    cout << "This text is green." << endl;

    SetConsoleTextAttribute(hOut,
                            FOREGROUND_BLUE);
    cout << "This text is blue." << endl;

    return 0;
}

Let us see one more example Using OR operator

Code: CPP

#include <windows.h>
#include <iostream.h>

int main()
{
    HANDLE hOut;

    hOut = GetStdHandle(STD_OUTPUT_HANDLE);

    SetConsoleTextAttribute(hOut,
                            FOREGROUND_RED |
                            FOREGROUND_GREEN);
    cout << "This text is yellow." << endl;

    SetConsoleTextAttribute(hOut,
                            FOREGROUND_GREEN |
                            FOREGROUND_BLUE);
    cout << "This text is cyan." << endl;

    SetConsoleTextAttribute(hOut,
                            FOREGROUND_BLUE |
                            FOREGROUND_RED);
    cout << "This text is magenta." << endl;

    SetConsoleTextAttribute(hOut,
                            FOREGROUND_RED |
                            FOREGROUND_GREEN |
                            FOREGROUND_BLUE);
    cout << "This text is white." << endl;

    return 0;
}

In this way we add color for console projects.

parvez.yu 6Mar2008 14:59

Re: Add color to console projects
 
wow i didnt kew about this


All times are GMT +5.5. The time now is 04:43.