1. This site uses cookies. By continuing to use this site, you are agreeing to our use of cookies. Learn More.

Is there any code for display color screen in C++

Discussion in 'C++' started by ballurohit, Mar 29, 2012.

  1. ballurohit

    ballurohit New Member

    Joined:
    Nov 18, 2011
    Messages:
    43
    Likes Received:
    0
    Trophy Points:
    0
    Occupation:
    retired
    Location:
    Gujarat
    I want to give better screen show. Therefore I want to know whether
    any formula/code do exit to depict color screen in c++.
     
  2. hobbyist

    hobbyist New Member

    Joined:
    Jan 7, 2012
    Messages:
    141
    Likes Received:
    0
    Trophy Points:
    0
    There is no standard C/C++ library for that. What compiler are you using? What target are you developing for? console, gui?
     
  3. ballurohit

    ballurohit New Member

    Joined:
    Nov 18, 2011
    Messages:
    43
    Likes Received:
    0
    Trophy Points:
    0
    Occupation:
    retired
    Location:
    Gujarat
    QUOTE Sir, I use C++ compiler. I don't have any target for developing. Also I have no
    idea about gui(Graphic User Interface).UNQUOTE

    (b.n.rohit)
     
  4. hobbyist

    hobbyist New Member

    Joined:
    Jan 7, 2012
    Messages:
    141
    Likes Received:
    0
    Trophy Points:
    0
    Hello,

    By compiler, I mean which company makes the one that you are using? Borland, Microsoft, etc. For example, Borland's older compilers like Turbo C, Borland CPP have the function textbackground.

    There's a bit of an issue when using older compilers in a Windows environment for stuff like getch, background color, etc... the "console" is in fact a window that emulates the old text mode. The program may have goofy behavior when non-standard functions are used.

    For more modern compilers, you'll probably need to use the API console functions throughout as cout, I think, will default to having gray text on a black background no matter how many times the background / foreground colors are changed. Maybe someone else knows for certain.

    Here's a crude attempt using API functions.

    Code:
    #include <iostream>
    #include <windows.h>
    
    int main() {
    
         TCHAR out_text[] = "Hello World!";
         COORD init_coord = { 0 ,  0 }, /* start at upper left corner   */
    	       place_at =   { 40, 12 }; /* location to display out_text */
         DWORD ret;
         CONSOLE_SCREEN_BUFFER_INFO cbuf; // console window paramters
         HANDLE hnd_out = GetStdHandle(STD_OUTPUT_HANDLE); // get handle for console output
    
         GetConsoleScreenBufferInfo(hnd_out, &cbuf); // call function to retrieve parameters
                                                     // needed to "fill the screen"
    
    	 // blue background and yellow text
         FillConsoleOutputAttribute(hnd_out,
    	                        FOREGROUND_GREEN | FOREGROUND_RED | FOREGROUND_INTENSITY | BACKGROUND_BLUE,
                                    cbuf.dwSize.X * cbuf.dwSize.Y, init_coord, &ret);
    
         WriteConsoleOutputCharacter(hnd_out, out_text, sizeof out_text, place_at, &ret);
    
         return 0;
    }
    Hope that helps
     

Share This Page