Menu

Color in C++

2006-05-18
2012-09-26
  • Nobody/Anonymous

    I'm currently using Dev 4.9.9.0. Not that I think it matters, but the "how to post" suggested it: Windows XP.

    My question is not of debugging, but more so of features. I was talking to a friend of mine about a program I recently made (Connect Four) and he mentioned that color would be pretty cool in future programs. I wasn't aware that you could even do colors in C++. I knew you could change the background color and all the text at once, but to set them in the actual code was not something I knew about. I looked around the internet for an answer to this, until I stumbled across this site and decided to just ask.

    My questions are: Can you actually have different colors set (for output)? And if so, how do you implement it?

     
    • Nobody/Anonymous

      Not exactly the color scheme you were going for, but this should give you some ideas.. you'll have to do a bit more work to get the column numbers and user input different colors.

      in the DrawColorString function I commented out the lines that sets the cursor position

      //COORD position={X, Y};
      //SetConsoleCursorPosition(OutputH, position);

      in your Print function I added a switch statement that changes the color according to which character is in the array, I also commented out the cin.get line and the return (not sure why those were there). I used numbers for the colors instead of the macros just to be quick and make the lines shorter.. not generaly good practice, better to use the macros.

      void Print (char Array[][15])
      { int Row_Size=14;
      int Col_Size=15;

      for (int row=0 ; row<Row_Size ; row++)
      {
      for (int col=0 ; col<Col_Size ; col++)
      {
        switch (Array[row][col])
        {
          case 'O':
                DrawColorString("", row, col, 12);
            break;
          case 'X':
                DrawColorString("", row, col, 13);
            break;
          default:
                DrawColorString("", row, col, 14);
            break;
        }
          cout<<Array[row][col];
      

      // cin.get();
      }
      cout<<endl;
      }
      // return;
      }

      b_a

       
    • Nobody/Anonymous

      Ok, heres what you do-

      1. Include windows.h as a header file

      this is your function prototype::

      void DrawColorString(string szText, int X, int Y, WORD color);

      This is the function

      void DrawColorString(string szText, int X, int Y, WORD color)
      {
      HANDLE OutputH;
      COORD position = {X, Y};

      OutputH = GetStdHandle(STD_OUTPUT_HANDLE);
      
      SetConsoleTextAttribute(OutputH, color);        
      SetConsoleCursorPosition(OutputH position);
      

      cout << szText;
      }

      In your function, when you want to implement the
      function, do this for the color. Remeber, the only
      colors you can have is RED, BLUE GREEN LIGH BLUE
      PURPLE, and YELLOW. Try mixing the background and
      foreground colors to see what results you get.
      Here is the code to implement the color:

      // text color test program
      // writern by Peter Watts (c)2006 NighhSoft Ltd

      include <windows.h>

      include <iostream>

      using namespace std;

      int main ( void )
      {
      // color function here -- see above for code

      // to change preceding text green::

      DrawColorString("", 0, 5, FOREGROUND_GREEN);

      // this will be green::
      cout << "I AM GREEN?" << endl;

      cin.get();

      return 0;
      } // end main

      Hope this helps! -pete

       
    • Nobody/Anonymous

      Console or GUI?

       
    • Anonymous

      Anonymous - 2006-05-18

      C and C++ are both languages that intrinsically have no support for I/O at all, whether it be text, graphics, filesystems, networking, colour or whatever. All these things are provided by libraries and OS API's, so there is no limitation on what C++ can do. In fact the majority of applications in the world, and certainly desktop applications, are probably written in C or C++, so to suggest that you cannot 'do colors' in C++ is erroneous.

      It is true that the standard libraries do not support colour. That is because they are intended to be 'lowest common denominator' and work on any system - that includes an 8-bit embedded microcontroller with just a serial port for console I/O, where colour support would be provided by whatever terminal device or terminal emulator you chose to connect to it.

      I am assuming that your conect four application is not graphical? It seems unlikely that you would have produced any king of graphical or GUI application without realising that colour was supported.

      To support advanced console mode text manipulation in Win32, there is a whole API dedicated to it; including colour support. http://msdn.microsoft.com/library/default.asp?url=/library/en-us/dllproc/base/console_functions.asp. You could also use a console library such as: http://www14.brinkster.com/aditsu/console/, a set of functions implemented using the Win32 API to emulate the Borland conio library. At the very least the sourcecode serves as a good example of how the Win32 consol API works.

      Clifford

       
    • Nobody/Anonymous

      if you go to www.chrispy.maelstrommedia.com you can download the game. Thanks, I'll be testing out that cose to see if I can get it. Thanks again.

       
    • Nobody/Anonymous

      Sorry for the double post:

      There needs to be a comma in the lins:
      SetConsoleCursorPosition(OutputH, position);

      and it's giving me an error of
      "string was not declared in this scope"
      "syntax error before ',' token"
      for the prototype

      void DrawColorString(string szText, int X, int Y, WORD color);

       
    • Nobody/Anonymous

      the string not declared in this scope error is probably because you don't have

      include <string>

      This post has the different colors you can use with SetConsoleTextAttribute:

      http://sourceforge.net/forum/message.php?msg_id=2424480

      One of the first things I was working on with Dev was a Trade Wars type game that needed a lot of color changes in the text, so I wrote a function similar to what pete had above, but I added some parsing to it so that I could embed color information in the strings sort of like the old ANSI text they used on BBS's, I'd send the function a string something like:

      "@3this is a @fcolorful @8string"

      the function would scan through the string and whenever it found a @ it would use a switch statement on the next character (I used hex 0-9 and a-f to keep it to one character) to set the color.
      I don't have access to the code at the moment, but it should be easy for you to recreate. I think I even posted it here in the forum once, but it was a while back.. not sure if the search will find it anymore.

      b_a

       
    • Nobody/Anonymous

      Unless I was suppose to add other stuff, I'm' still getting that error:

      // text color test program
      // writern by Peter Watts (c)2006 NighhSoft Ltd

      include <windows.h>

      include <iostream>

      include <string>

      void DrawColorString(string szText, int X, int Y, WORD color);

      using namespace std;

      int main ( void )
      { // color function here -- see above for code to change preceding text green
      DrawColorString("", 0, 5, FOREGROUND_GREEN);
      // this will be green
      cout<<"I AM GREEN?"<<endl;
      cin.get();
      return 0;
      }
      void DrawColorString(string szText, int X, int Y, WORD color)
      { HANDLE OutputH;
      COORD position={X, Y};

      OutputH = GetStdHandle(STD_OUTPUT_HANDLE);

      SetConsoleTextAttribute(OutputH, color);
      SetConsoleCursorPosition(OutputH, position);

      cout<<szText;
      }

       
    • Wayne Keen

      Wayne Keen - 2006-05-18

      The reason that you are getting that error is that the functions that are part of "string" are in the standard namespace. (Is this ringing some bells?)

      The simple, but klugey way to deal with this is to add

      using namespace std;

      after your includes.

      Wayne

      p.s. I checked, this code compiles and runs OK

      include <windows.h>

      include <iostream>

      include <string>

      using namespace std;

      void DrawColorString(string szText, int X, int Y, WORD color);

      using namespace std;

      int main ( void )
      { // color function here -- see above for code to change preceding text green
      DrawColorString("", 0, 5, FOREGROUND_GREEN);
      // this will be green
      cout<<"I AM GREEN?"<<endl;
      cin.get();
      return 0;
      }
      void DrawColorString(string szText, int X, int Y, WORD color)
      { HANDLE OutputH;
      COORD position={X, Y};

      OutputH = GetStdHandle(STD_OUTPUT_HANDLE);

      SetConsoleTextAttribute(OutputH, color);
      SetConsoleCursorPosition(OutputH, position);

      cout<<szText;
      }

       
    • Nobody/Anonymous

      Hey, thanks a lot. I was able to get that to work. Anotehr question though, I am putting this code into the connect four program. In the game, I have a game board, X's and O's, along with input. What I wanted to do was make the game board yellow, the X's black, O's red, probably the back ground white, with input black as well. The board and game pieces (along with numbers under each column) are all in a 2D array. I'm going to include a link to the source code just to show you all what's going on. I have included the function call once in there, in the function Print. Problem is how do I set each part the color I want, and when I run that as is, it only prints the first element of the array, how do I fix that?

      http://chrispy.maelstrommedia.com/Connect_Four.txt

       

Log in to post a comment.

Want the latest updates on software, tech news, and AI?
Get latest updates about software, tech news, and AI from SourceForge directly in your inbox once a month.