Add a little Color to your Console Text

vegaseat 2 Tallied Votes 69K Views Share

Things don't have to be black and white all the time. Use a Windows API call to add some color to your text output.

// color your text in Windows console mode
// colors are 0=black 1=blue 2=green and so on to 15=white  
// colorattribute = foreground + background * 16
// to get red text on yellow use 4 + 14*16 = 228
// light red on yellow would be 12 + 14*16 = 236
// a Dev-C++ tested console application by  vegaseat  07nov2004

#include <iostream>
#include <windows.h>   // WinApi header

using namespace std;    // std::cout, std::cin

int main()
{
  HANDLE  hConsole;
	int k;
	
  hConsole = GetStdHandle(STD_OUTPUT_HANDLE);

  // you can loop k higher to see more color choices
  for(k = 1; k < 255; k++)
  {
    // pick the colorattribute k you want
    SetConsoleTextAttribute(hConsole, k);
    cout << k << " I want to be nice today!" << endl;
  }
  
  cin.get(); // wait
  return 0;
}
venomlash 55 Junior Poster

Nice...
How do you change the background color?
Pretty please?
And is there a way to make the program appear fullscreen when started up?

Akilah712 0 Newbie Poster

I have tried to use this in my program.

However when I used #include<windows.h> I get errors when I compile.

I am using the MSVisual 6.0 ???

mistercow.pnoy 0 Newbie Poster

on windows only:
system("color <put your colors here>");

colors the whole window and all text to any of the standard 16 colors

//0 = Black 8 = Gray
//1 = Blue 9 = Light Blue
//2 = Green a = Light Green
//3 = Aqua b = Light Aqua
//4 = Red c = Light Red
//5 = Purple d = Light Purple
//6 = Yellow e = Light Yellow
//7 = White f = Bright White

you put two characters, first one is background color, second is text color:
system("color c0"); //colors background to light red, with black text

dombit 0 Newbie Poster

ya, bakround color use . - system("color f0"); and #include <stdlib.h> it will make the bacround wight with black text for more color codes type 'color help' in cmd prompt. the first nuber ids the backround and the seconed is the text. also to start the consol in full screen send the keys alt and enter like this

keybd_event(VK_MENU, 0x38, 0, 0);
keybd_event(VK_RETURN, 0x1c, 0, 0);
keybd_event(VK_RETURN, 0X1c, KEYEVENTF_KEYUP, 0);
keybd_event(VK_MENU, 0x38, KEYEVENTF_KEYUP, 0);

setherith 0 Newbie Poster

Just what I was looking for. Cheers!

jamesysco 0 Newbie Poster

Nice one :) the "system" function...who'd have thought it :) Thanks!

pwnedu46 0 Newbie Poster

To get all the system() commands (WINDOWS ONLY!), open up the command prompt (start>accessories>Command Prompt), and type "help" (without the quotes). For help on a specific command, type "help <command name>" (again, no quotes).

shirish7151 -4 Newbie Poster
#include<conio.h>
#include<iostream>
#include<dos.h>
int main() {
textbackground(BLUE);
textcolor(YELLOW);
std::cout<<"Yellow text in a blue console application";
std::cin.get();
return 0;
}
WaltP commented: Worthless in today's world -4
Philip_19 commented: it doesn't work in todays world, but cool legacy code +0
Philip_19 27 Newbie Poster

yes, its sad you need the string as a const, so i made a function to change the text color:

void setcolor(int color)
{
    if (color == 1)
    {
        system("color 01");
    }
    else if (color == 2)
    {
        system("color 02");
    }
    else if (color == 3)
    {
        system("color 03");
    }
    else if (color == 4)
    {
        system("color 04");
    }
    else if (color == 5)
    {
        system("color 05");
    }
    else if (color == 6)
    {
        system("color 06");
    }
    else if (color == 7)
    {
        system("color 07");
    }
    else if (color == 8)
    {
        system("color 08");
    }
    else if (color == 9)
    {
        system("color 09");
    }
    else if (color == 10)
    {
        system("color 0a");
    }
    else if (color == 11)
    {
        system("color 0b");
    }
    else if (color == 12)
    {
        system("color 0c");
    }
    else if (color == 13)
    {
        system("color 0d");
    }
    else if (color == 14)
    {
        system("color 0e");
    }
    else if (color == 15)
    {
        system("color 0f");
    }
    return;
}
Reverend Jim commented: atrocious code -3
rproffitt commented: Did ChatGPT write this? No, I think it could do better. -4
Dani commented: Thank you for joining and contributing, I'm sorry for the rude comments you received +34
Reverend Jim 4,780 Hi, I'm Jim, one of DaniWeb's moderators. Moderator Featured Poster

Aside from that being really god-awful code, this thread is many years old and not worth reviving.

Dani 4,084 The Queen of DaniWeb Administrator Featured Poster Premium Member

Philip,

Thank you for your contribution. I know it can be difficult coming into a new community and wanting to participate, especially as a beginner programmer. I apologize that your first impressions here have been people bashing your code. I do agree that the code you contributed is inefficient, but the better way to go about it would have to have suggested a better way so that you could learn instead of just being insulting. I would hope that DaniWeb would be about us sharing knowledge with each other, instead of bashing those who are brave enough to share their best.

Also, there is nothing wrong with resurrecting an old topic if you think you have something to add. The beauty of forums is that they live on for years and years.

I really wish I knew C++ enough to help Philip out here instead of just bashing him. Can someone help?

Much thanks!

Reverend Jim 4,780 Hi, I'm Jim, one of DaniWeb's moderators. Moderator Featured Poster

Having spent years maintaining and rewriting abominable code by supposedly professional programmers (the company had actually been paid several million dollars for their system) I tend to occasionally go ballistic when I come across bad code so I sincerely apologize for over-reacting. I do not know your level of expertise and I forget what it was like when I was just starting to code (many many many years ago).

Dani is quite correct. Instead, I could have just as easily said "there is a much better way to do this, and here it is."

You have a number in the range 1-15. Instead of testing for every possible number, convert the int to a hex string. The code below may not be exactly right because I have not programmed in C/C++ in many years but it should give you the basic idea.

void setcolor(int color) {

    char s[9];

    if (color >= 1 && color <= 15) {
        sprintf(s, "color %02x", color);
        system(s);
    }
}
Dani 4,084 The Queen of DaniWeb Administrator Featured Poster Premium Member

Good on you, Jim.

Philip, please don’t get discouraged from programming. Jim bashed your effort, yes, but in his next post he admitted that, even with a long career as a professional programmer, his suggestion still might not be perfect/fool-proof.

I just hope people stumbling across this thread don’t get the wrong impression that DaniWeb isn’t a safe space for new programmers, or that all skill levels don’t deserve to contribute. Even as a beginner, there is always something to teach the next person.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.