13 Replies - 101215 Views - Last Post: 22 December 2008 - 10:43 PM Rate Topic: -----

#1 swifty528  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 3
  • Joined: 18-November 06

Change font color in C program

Posted 18 November 2006 - 07:50 AM

i'd like to change the font color in my C program, i know you can change it to red, green and blue, what other colors can i choose from (if any)?

HANDLE hStdout;

	hStdout = GetStdHandle(STD_OUTPUT_HANDLE); 
	SetConsoleTextAttribute(hStdout, FOREGROUND_RED);
	printf("This is red.\n");
	SetConsoleTextAttribute(hStdout, FOREGROUND_BLUE);
	printf("This is blue.\n");
	SetConsoleTextAttribute(hStdout, FOREGROUND_GREEN);
	printf("This is green.\n");



i've tried stuff like YELLOW, YEL, it doesnt work hehe...

Is This A Good Question/Topic? 0
  • +

Replies To: Change font color in C program

#2 Xing  Icon User is offline

  • D.I.C Addict
  • member icon

Reputation: 19
  • View blog
  • Posts: 725
  • Joined: 22-July 06

Re: Change font color in C program

Posted 18 November 2006 - 10:06 AM

IIRC then we can use 16 colors, 0=black 1=blue 2=green and so on to 15=white

This post has been edited by Xing: 18 November 2006 - 08:51 PM

Was This Post Helpful? 0
  • +
  • -

#3 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: Change font color in C program

Posted 18 November 2006 - 10:25 PM

Since the constants are only for RGB Colors, you have to combine them to get the other colors.
The color value is stored in a byte, where the Most Significant Bit is the Intensity (Light or Dark) of the color and the last 7 represent the color.

So for a darker yellow, use this:
SetConsoleTextAttribute(hStdout, FOREGROUND_RED | FOREGROUND_GREEN);
printf("yellow");



If you want a brighter yellow, add the FOREGROUND_INTENSITY CONSTANT with the other colors like this:
SetConsoleTextAttribute(hStdout, FOREGROUND_INTENSITY|FOREGROUND_RED | FOREGROUND_GREEN);
printf("yellow");


Was This Post Helpful? 0
  • +
  • -

#4 swifty528  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 3
  • Joined: 18-November 06

Re: Change font color in C program

Posted 19 November 2006 - 01:41 AM

thanks you guys really helped :D
Was This Post Helpful? 0
  • +
  • -

#5 swifty528  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 3
  • Joined: 18-November 06

Re: Change font color in C program

Posted 19 November 2006 - 01:49 AM

View PostXing, on 18 Nov, 2006 - 10:06 AM, said:

IIRC then we can use 16 colors, 0=black 1=blue 2=green and so on to 15=white


can you give me an example of the syntax? thanks :)
Was This Post Helpful? 0
  • +
  • -

#6 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: Change font color in C program

Posted 20 November 2006 - 09:00 AM

Quote

can you give me an example of the syntax? thanks


Internally, the constants you use are hard-coded as constants in the final executable code.

You can place them as literals directly in the function but that would reduce readability.
int color = 15;  // Change Value Here
SetConsoleTextAttribute(hStdout, color);


Was This Post Helpful? 0
  • +
  • -

#7 okyup  Icon User is offline

  • D.I.C Head
  • member icon

Reputation: 0
  • View blog
  • Posts: 207
  • Joined: 06-November 06

Re: Change font color in C program

Posted 20 November 2006 - 11:13 AM

Theres also example(s) of this in code snippets.
Was This Post Helpful? 0
  • +
  • -

#8 aericron  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 6
  • Joined: 15-December 08

Re: Change font color in C program

Posted 15 December 2008 - 12:13 PM

Hey guys, I'm new here and I'm working on my final in C, and I ran into a little problem.

For my final, I'm making this menu and it does a whole bunch of different stuff, such as change the color of font/background, and I still need to find out how to make that user friendly. I'm thinking of this basically

1.---
2. Change font/background colors
3.-----
4.-----
5.----

Didn't feel like typing the other options but anyway

I'm thinking of having it ask which they would like to change (font or back ground) and then giving them a list of the colors. I know the colors from cmd prompt, but I'm not sure how to let them just hit a number and let it work.

***Also, I posted in this forum basically because this was the topic I needed help with
Any suggestions?

This post has been edited by aericron: 15 December 2008 - 12:13 PM

Was This Post Helpful? 0
  • +
  • -

#9 no2pencil  Icon User is offline

  • Admiral Fancy Pants
  • member icon

Reputation: 5363
  • View blog
  • Posts: 27,325
  • Joined: 10-May 07

Re: Change font color in C program

Posted 15 December 2008 - 12:29 PM

View Postaericron, on 15 Dec, 2008 - 01:13 PM, said:

I'm not sure how to let them just hit a number and let it work.

Run the menu results through a switch case, & have a function that changes the colors.
Was This Post Helpful? 0
  • +
  • -

#10 aericron  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 6
  • Joined: 15-December 08

Re: Change font color in C program

Posted 15 December 2008 - 01:20 PM

View Postno2pencil, on 15 Dec, 2008 - 11:29 AM, said:

View Postaericron, on 15 Dec, 2008 - 01:13 PM, said:

I'm not sure how to let them just hit a number and let it work.

Run the menu results through a switch case, & have a function that changes the colors.


That's what I'm doing so far, but I have yet to find the function...
Was This Post Helpful? 0
  • +
  • -

#11 no2pencil  Icon User is offline

  • Admiral Fancy Pants
  • member icon

Reputation: 5363
  • View blog
  • Posts: 27,325
  • Joined: 10-May 07

Re: Change font color in C program

Posted 15 December 2008 - 01:36 PM

View Postaericron, on 15 Dec, 2008 - 02:20 PM, said:

That's what I'm doing so far, but I have yet to find the function...

Showing us your code would allow us to help you further.

:code:
Was This Post Helpful? 0
  • +
  • -

#12 aericron  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 6
  • Joined: 15-December 08

Re: Change font color in C program

Posted 17 December 2008 - 12:10 PM

Ok, I'm only hitting a little bit of it, just because of how I have it display, it'd take a page or two.

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

#define BELL '\x07'

char ans;

void menu(void);
void disp_menu();
void change_colors(void);
void simple_calc(void);
void dragonforce_test(void);
void dragonforce_lyrics(void);
void err_msg(char err_msg[]);



/**************************************************************************/
void menu(void)
{
	do
	{ disp_menu();		// Display menu for the user
	scanf(" %d", &ans);
	switch (ans)
	{ case (1) : { change_colors();
		break; }
	case (2) : { simple_calc();
		break; }
	case (3) : { dragonforce_test();
		break; }
	case (4) : { dragonforce_lyrics();
		break; }
	case (5) : { break; }
	default : { err_msg("*** Please select a valid option ***");
		break; }
	}
	return 0;
	}

/**************************************************************************/
void disp_menu()
{
	clear_sc();

	printf("\t\t*** Main Menu ***\n");
	printf("\t\t	--------- \n\n\n\n");
	printf("Do you want to:\n\n\n");
	printf("\t1. Change the colors of your font or background\n\n\n");
	printf("\t2. Use a simple calculator function\n\n\n");
	printf("\t3. Question your knowledge of the hardest metal known to man\n\n\n");
	printf("\t4. Get started on memorizing the lyrics of said band\n\n\n");
	printf("\t5. Exit this program\n\n\n");
	printf("What is your choice? ");
	return 0;
}
/**************************************************************************/
void change_colors(void)
{
	int a;
	int b;
	
	{system("color %d%d", a,b);}
	scanf(" %d%d", a,b);
	return 0;
}


/**************************************************************************/




I'm using an exercise as my base and looking around for different snippets of code that are of use.

I'm trying to base this off of a piece of paper I saw in class and what a few friends have used.

 #include<windows.h>
{system("color 5e");printf("An enemy appears");}



okay, so I'm trying to use that as my base, but allow it to have input for the color values as you can see with the fail of scanf. Is there anyway to do as I dream, or will I have to list out every possibility or be doomed to a few options?

This post has been edited by aericron: 17 December 2008 - 12:15 PM

Was This Post Helpful? 0
  • +
  • -

#13 aericron  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 6
  • Joined: 15-December 08

Re: Change font color in C program

Posted 22 December 2008 - 11:50 AM

Ok, I've decided on a few limited color schemes seeing as how I can't get detailed with user input without killing everyone with 256 options...

Now I have another problem, but this isn't the place to ask about it.

Thanks for all the help!
Was This Post Helpful? 0
  • +
  • -

#49 Hyper  Icon User is offline

  • Banned

Reputation: 108
  • View blog
  • Posts: 2,129
  • Joined: 15-October 08

Re: Change font color in C program

Posted 22 December 2008 - 10:43 PM

View Postswifty528, on 18 Nov, 2006 - 06:50 AM, said:

i'd like to change the font color in my C program, i know you can change it to red, green and blue, what other colors can i choose from (if any)?

i've tried stuff like YELLOW, YEL, it doesnt work hehe...


Coloring text in consoles is really easy (when you understand how). Here's a simple enumeration/function I put together (and use quite often myself):

/* Colors defined for SetColor(int) */
enum {
    BLACK = 0,
    DARK_BLUE = 1,
    DARK_GREEN = 2,
    TEAL = 3,
    DARK_RED = 4,
    DARK_PURPLE = 5,
    GOLD = 6,
    GREY = 7,
    DARK_WHITE = 8,
    BLUE = 9,
    GREEN = 10,
    CYAN = 11,
    RED = 12,
    PURPLE = 13,
    YELLOW = 14,
    WHITE = 15
};

void SetColor(const int foreground, const int background) {
    
    int Color = foreground + (background * 16);
    HANDLE hConsole = GetStdHandle(STD_OUTPUT_HANDLE);
    SetConsoleTextAttribute(hConsole, Color);

    return;
}

void PlaceCursor(const int x, const int y) {

    HANDLE hConsole = GetStdHandle(STD_OUTPUT_HANDLE);

    COORD PlaceCursorHere;
    PlaceCursorHere.X = x;
    PlaceCursorHere.Y = y;

    SetConsoleCursorPosition(hConsole, PlaceCursorHere);
    return;
}


The very bottom is another common function I use (and again, made myself like SetColor).
Hope this helps! If it did, remember to click the "This Was a Helpful Post!" button. :)
Was This Post Helpful? 2

Page 1 of 1