Text and background color in C++

  • (2 Pages)
  • +
  • 1
  • 2

21 Replies - 166688 Views - Last Post: 24 May 2013 - 04:36 PM Rate Topic: ***** 1 Votes

#1 spooky  Icon User is offline

  • New D.I.C Head

Reputation: 1
  • View blog
  • Posts: 10
  • Joined: 20-October 06

Text and background color in C++

Post icon  Posted 20 October 2006 - 06:36 AM

hi guyzi'd like to know how to set back and text color in C++ Im creating a prog in DOS
no idea about windows programming ;)

plz help me im searching all over the web and i can t find anythink!
oh i also what to know how i can make my prog opens in the whole screen

p.s sorry for my english... :music: B)

Is This A Good Question/Topic? 1
  • +

#26 BitByte  Icon User is offline

  • D.I.C Head

Reputation: 11
  • View blog
  • Posts: 194
  • Joined: 09-August 06

Re: Text and background color in C++

Posted 20 October 2006 - 08:22 AM

Here's some code that might get you started. Did it a long time ago. I don't know how to set the background though :rolleyes:

// February 2006 using excellent code::blocks IDE with mingw
// C++ Code for changing colors of text in the console window
#include <iostream>
#include <windows.h>

using namespace std;

int main()
{

	HANDLE hConsole;
	hConsole = GetStdHandle (STD_OUTPUT_HANDLE);

// Use the three primary colors for mixing any other color.
// Use FOREGROUND_INTENSITY for brighter colors.
	SetConsoleTextAttribute
	(hConsole, FOREGROUND_RED | FOREGROUND_INTENSITY);
			cout << "Bright red text\n";

	SetConsoleTextAttribute
	(hConsole, FOREGROUND_BLUE | FOREGROUND_INTENSITY);
			cout << "Bright blue text\n";

	SetConsoleTextAttribute
	(hConsole, FOREGROUND_RED | FOREGROUND_GREEN | FOREGROUND_BLUE );
			cout << "Back to normal white text\n";

// Wait for user pressing key before exiting
// Gives them a chance to see the output
			cout << "\n\nPress any key to exit program.....";
			cin.get();

		return 0;
}

Was This Post Helpful? 3

#27 spooky  Icon User is offline

  • New D.I.C Head

Reputation: 1
  • View blog
  • Posts: 10
  • Joined: 20-October 06

Re: Text and background color in C++

Posted 20 October 2006 - 02:59 PM

well can i use other colors exept red or blue?
i tried yello and gray but nothink happent...

does anyone know how to chang back color?
Was This Post Helpful? 0
  • +
  • -

#28 BitByte  Icon User is offline

  • D.I.C Head

Reputation: 11
  • View blog
  • Posts: 194
  • Joined: 09-August 06

Re: Text and background color in C++

Posted 20 October 2006 - 04:46 PM

You can mix the colours just like you do when your painting. Red and blue makes purple etc....

FOREGROUND_RED | FOREGROUND_BLUE

Just mess about with the colours. About the background you just use BACKGROUND_RED or whatever colour you like. Just found that out. Should of figured it out really :D
Was This Post Helpful? 2
  • +
  • -

#44 born2c0de  Icon User is offline

  • printf("I'm a %XR",195936478);
  • member icon

Reputation: 180
  • View blog
  • Posts: 4,667
  • Joined: 26-November 04

Re: Text and background color in C++

Posted 21 October 2006 - 04:06 AM

Woah...you needn't complicate your code for a DOS App.
Simply include conio.h and use these functions:
  • textcolor()
  • textbackground()
  • textattr()
Care must be taken to use cprintf() instead of printf() as printf() does not support formatted output.
Here's an example:
#include <conio.h>

int main(void)
{
   int i, j;

   clrscr();
   for (i=0; i<9; i++)
   {
	   for (j=0; j<80; j++)
		  cprintf("C");
	   cprintf("\r\n");
	   textcolor(i+1);
	   textbackground(i);
   }

   return 0;
}


Was This Post Helpful? 2

#45 smg5k  Icon User is offline

  • New D.I.C Head

Reputation: 1
  • View blog
  • Posts: 4
  • Joined: 26-October 07

Re: Text and background color in C++

Posted 21 January 2008 - 10:50 AM

Is there any way of using cout instead of cprintf??
and is there something that colors all the background
not only the text area...??

This post has been edited by smg5k: 21 January 2008 - 10:54 AM

Was This Post Helpful? 1
  • +
  • -

#46 Jingle  Icon User is offline

  • D.I.C Regular

Reputation: 9
  • View blog
  • Posts: 317
  • Joined: 20-October 07

Re: Text and background color in C++

Posted 21 January 2008 - 01:00 PM

make sure to note that none of these will work on a mac or linix for that matter.
Was This Post Helpful? 3
  • +
  • -

#47 no2pencil  Icon User is online

  • Toubabo Koomi
  • member icon

Reputation: 5306
  • View blog
  • Posts: 27,200
  • Joined: 10-May 07

Re: Text and background color in C++

Posted 21 January 2008 - 01:06 PM

View PostJingle, on 21 Jan, 2008 - 01:00 PM, said:

make sure to note that none of these will work on a mac or linix for that matter.

Well, he did say that this was for DOS. For Linux colors, I'm sure you would want to use escape characters or the ncurses library (if you want portability).

#include <stdio.h>

int main(void) {
  printf("\033[22;31mHello world!");

  return 0;
}



Quote

\033[22;30m - black
\033[22;31m - red
\033[22;32m - green
\033[22;33m - brown
\033[22;34m - blue
\033[22;35m - magenta
\033[22;36m - cyan
\033[22;37m - gray
\033[01;30m - dark gray
\033[01;31m - light red
\033[01;32m - light green
\033[01;33m - yellow
\033[01;34m - light blue
\033[01;35m - light magenta
\033[01;36m - light cyan
\033[01;37m - white


http://www.vt100.net
Was This Post Helpful? 2
  • +
  • -

#48 smg5k  Icon User is offline

  • New D.I.C Head

Reputation: 1
  • View blog
  • Posts: 4
  • Joined: 26-October 07

Re: Text and background color in C++

Posted 21 January 2008 - 01:37 PM

well it's not for dos... i want to use it in windows...
and don't know how...
Was This Post Helpful? 0
  • +
  • -

#49 Jingle  Icon User is offline

  • D.I.C Regular

Reputation: 9
  • View blog
  • Posts: 317
  • Joined: 20-October 07

Re: Text and background color in C++

Posted 21 January 2008 - 01:41 PM

View Postsmg5k, on 21 Jan, 2008 - 01:37 PM, said:

well it's not for dos... i want to use it in windows...
and don't know how...

the DOS prompt is part of windows
Was This Post Helpful? 1
  • +
  • -

#50 no2pencil  Icon User is online

  • Toubabo Koomi
  • member icon

Reputation: 5306
  • View blog
  • Posts: 27,200
  • Joined: 10-May 07

Re: Text and background color in C++

Posted 21 January 2008 - 01:42 PM

View Postspooky, on 20 Oct, 2006 - 06:36 AM, said:

hi guyzi'd like to know how to set back and text color in C++ Im creating a prog in DOS

This is what I was referring too.
Was This Post Helpful? 0
  • +
  • -

#51 smg5k  Icon User is offline

  • New D.I.C Head

Reputation: 1
  • View blog
  • Posts: 4
  • Joined: 26-October 07

Re: Text and background color in C++

Posted 21 January 2008 - 01:46 PM

sorry.. but it doesn't work.... it just shows me all the text including the color code
#include <stdio.h>

#include <windows.h>
  
int main(void) {
  
  printf("\033[22;33mHello world!");
  
  system("pause");
  
  return 0;
}

Was This Post Helpful? 0
  • +
  • -

#52 Jingle  Icon User is offline

  • D.I.C Regular

Reputation: 9
  • View blog
  • Posts: 317
  • Joined: 20-October 07

Re: Text and background color in C++

Posted 21 January 2008 - 01:50 PM

maybe this would work?
#include <stdio.h>

#define CSI 0x1b


void print_color(char *str, int color) {

	 printf("%c[%dm%s\n",CSI,color,str);
}

int main(int argc, char** argv) {
	 print_color(argv[2], atoi(argv[1]));
	 return 0;
}



this is for use command line.
BLACK = CSI + '30m'
RED = CSI + '31m'
GREEN = CSI + '32m'
YELLOW = CSI + '33m'
BLUE = CSI + '34m'
MAGENTA = CSI + '35m'
CYAN = CSI + '36m'
WHITE = CSI + '37m'
RESET = CSI + '39m'
Was This Post Helpful? 0
  • +
  • -

#53 smg5k  Icon User is offline

  • New D.I.C Head

Reputation: 1
  • View blog
  • Posts: 4
  • Joined: 26-October 07

Re: Text and background color in C++

Posted 21 January 2008 - 01:59 PM

it gives me some errors...
it sais undefined function atoi
Was This Post Helpful? 0
  • +
  • -

#54 no2pencil  Icon User is online

  • Toubabo Koomi
  • member icon

Reputation: 5306
  • View blog
  • Posts: 27,200
  • Joined: 10-May 07

Re: Text and background color in C++

Posted 21 January 2008 - 02:08 PM

View Postsmg5k, on 21 Jan, 2008 - 01:59 PM, said:

it gives me some errors...
it sais undefined function atoi

atoi is dependent of stdlib.h, include this header & your error should go away.
Was This Post Helpful? 0
  • +
  • -

  • (2 Pages)
  • +
  • 1
  • 2