1. We have moved from vBulletin to XenForo and you are viewing the site in the middle of the move. Though the functional aspect of everything is working fine, we are still working on other changes including the new design on Xenforo.
    Dismiss Notice

Add color to console projects

Discussion in 'Win32' started by Sanskruti, Feb 12, 2007.

  1. Sanskruti

    Sanskruti New Member

    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:
    #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:
    #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.
     
  2. parvez.yu

    parvez.yu New Member

    wow i didnt kew about this
     

Share This Page