6 Replies - 792 Views - Last Post: 31 August 2009 - 08:09 AM Rate Topic: -----

#1 James_Alex  Icon User is offline

  • D.I.C Head
  • member icon

Reputation: 7
  • View blog
  • Posts: 156
  • Joined: 21-August 09

very simple question of how to get the console color

Posted 31 August 2009 - 05:55 AM

hi, i wanna now to get the console color
i've tryed
system("color a");


and i've changed my consle text color to green, now i wanna to get the console color
is there anyway to do it ?
don't say try
char col[256];
strcpy(col, "a");


i need a function like "system("getcolor");" for example

Is This A Good Question/Topic? 0
  • +

Replies To: very simple question of how to get the console color

#2 stev3  Icon User is offline

  • New D.I.C Head

Reputation: 1
  • View blog
  • Posts: 44
  • Joined: 04-June 09

Re: very simple question of how to get the console color

Posted 31 August 2009 - 06:46 AM

#include <iostream>
const int Colours = system("color 2A");
int main()
{

   return 0;
}



The first digit/letter in "color .." is the background; the next is the foreground

0 = Black
1 = Blue
2 = Green
3 = Aqua
4 = Red
5 = Purple
6 = Yellow
7 = White
8 = Gray
9 = Light Blue
A = Light Green
B = Light Aqua
C = Light Red
D = Light Purple
E = Light Yellow
F = Bright White
Was This Post Helpful? 0
  • +
  • -

#3 James_Alex  Icon User is offline

  • D.I.C Head
  • member icon

Reputation: 7
  • View blog
  • Posts: 156
  • Joined: 21-August 09

Re: very simple question of how to get the console color

Posted 31 August 2009 - 07:05 AM

View Poststev3, on 31 Aug, 2009 - 05:46 AM, said:

#include <iostream>
const int Colours = system("color 2A");
int main()
{

   return 0;
}



The first digit/letter in "color .." is the background; the next is the foreground

0 = Black
1 = Blue
2 = Green
3 = Aqua
4 = Red
5 = Purple
6 = Yellow
7 = White
8 = Gray
9 = Light Blue
A = Light Green
B = Light Aqua
C = Light Red
D = Light Purple
E = Light Yellow
F = Bright White

but this is to set the backround color no to get it
Was This Post Helpful? 0
  • +
  • -

#4 NickDMax  Icon User is offline

  • Can grep dead trees!
  • member icon

Reputation: 2250
  • View blog
  • Posts: 9,245
  • Joined: 18-February 07

Re: very simple question of how to get the console color

Posted 31 August 2009 - 07:17 AM

Using system to set the colors is really not a wonderful idea (it works though). You really should be using the API rather than exiting out to the system to do your dirty work for you. One good reason is that you can't get the current colors using the system command (unless you write your own utility to do it for you).

SO... how might you use the API? Well you can look at my snippet here which covers a number of useful functions. However I did not do a getConsoleColors() routine...

However if you look at the setColor() routine -- it needs to get the current text attributes so that it can set the forground color without affecting the background color... all we need to do is modify this routine to return the text attribute byte (the color). If we want we can even brake it up into a "getForground()" and "getBackground()"

#include <windows.h>
#include <stdio.h>


int GetColor(void);
int getForground(void);
int getBackground(void);


int main()
{   
	int forg, back;
	system("Color 17");
	forg = getForground();
	back = getBackground();
	printf("forground color: %d\nBackground color: %d\n", forg, back);
	return 0;
}

//This will set the forground color for printing in a console window.
int GetColor()
{
	 WORD wColor = 0; 
	 //We will need this handle to get the current background attribute
	 HANDLE hStdOut = GetStdHandle(STD_OUTPUT_HANDLE);
	 CONSOLE_SCREEN_BUFFER_INFO csbi;
	 
	 //We use csbi for the wAttributes word.
	 if(GetConsoleScreenBufferInfo(hStdOut, &csbi))
	 {
		//return the wAttributes data
		  wColor = (csbi.wAttributes & 0xFF);
	 }
	 return wColor;
}

int getForground() 
{
	//Mask out all but the lower nibble
	return GetColor() & 0x0F;
}

int getBackground()
{
	// Shift down one nibble (4bits) and then mask off everything else
	return (GetColor() >> 4) & 0x0F;
}

Was This Post Helpful? 0
  • +
  • -

#5 James_Alex  Icon User is offline

  • D.I.C Head
  • member icon

Reputation: 7
  • View blog
  • Posts: 156
  • Joined: 21-August 09

Re: very simple question of how to get the console color

Posted 31 August 2009 - 07:30 AM

thx
but a last thing, how can i color only one paragraphe
example i have
"this is a test
enjoy it"
and i like to color enjoy it only
how can i do it plz ?
Was This Post Helpful? 0
  • +
  • -

#6 NickDMax  Icon User is offline

  • Can grep dead trees!
  • member icon

Reputation: 2250
  • View blog
  • Posts: 9,245
  • Joined: 18-February 07

Re: very simple question of how to get the console color

Posted 31 August 2009 - 07:43 AM

well in the context of the snippet I gave a link to that would be really easy:

printf("this is a test\n");
SetColor(4);
printf("enjoy it!");
SetColor(7); //reset the color to the original.

Heck of you wanted you could even look at this snippet here by Hyper which works like printf but can do things like change the color in the format string.

So you could do something like:

printfExt("%cthis is a test\n%cenjoy it!", BLUE, RED);
Was This Post Helpful? 0
  • +
  • -

#7 James_Alex  Icon User is offline

  • D.I.C Head
  • member icon

Reputation: 7
  • View blog
  • Posts: 156
  • Joined: 21-August 09

Re: very simple question of how to get the console color

Posted 31 August 2009 - 08:09 AM

View PostNickDMax, on 31 Aug, 2009 - 06:43 AM, said:

well in the context of the snippet I gave a link to that would be really easy:

printf("this is a test\n");
SetColor(4);
printf("enjoy it!");
SetColor(7); //reset the color to the original.

Heck of you wanted you could even look at this snippet here by Hyper which works like printf but can do things like change the color in the format string.

So you could do something like:

printfExt("%cthis is a test\n%cenjoy it!", BLUE, RED);

thx a lot it works
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1