We're a community of 1.1M IT Pros here for help, advice, solutions, professional growth and fun. Join us!
1,080,586 Members — Technology Publication meets Social Media
Username:
Password:
Lost login information?

Add a little Color to your Console Text

2
By vegaseat on Nov 8th, 2004 12:19 am

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;
}

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

venomlash
Junior Poster
143 posts since Oct 2006
Reputation Points: 86
Solved Threads: 2
Skill Endorsements: 0

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 ???

Akilah712
Newbie Poster
21 posts since Jun 2007
Reputation Points: 10
Solved Threads: 0
Skill Endorsements: 0

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

mistercow.pnoy
Newbie Poster
7 posts since Feb 2008
Reputation Points: 10
Solved Threads: 0
Skill Endorsements: 0

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);

dombit
Newbie Poster
10 posts since Jun 2009
Reputation Points: 10
Solved Threads: 0
Skill Endorsements: 0

Just what I was looking for. Cheers!

setherith
Newbie Poster
16 posts since Aug 2010
Reputation Points: 10
Solved Threads: 1
Skill Endorsements: 0

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

jamesysco
Newbie Poster
1 post since Jan 2011
Reputation Points: 17
Solved Threads: 0
Skill Endorsements: 0

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).

pwnedu46
Newbie Poster
1 post since Feb 2011
Reputation Points: 10
Solved Threads: 0
Skill Endorsements: 0
#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;
}
shirish7151
Newbie Poster
1 post since Jan 2012
Reputation Points: 6
Solved Threads: 0
Skill Endorsements: 0

Post: Markdown Syntax: Formatting Help
 
You
 
© 2013 DaniWeb® LLC
Page generated in 0.1059 seconds using 2.73MB