hi...
Can anyone tell me how to change text color in C?
I'm working on a school project and i would like to change the text color depending on a users level.
This is a discussion on Change text color in C within the C Programming forums, part of the General Programming Boards category; hi... Can anyone tell me how to change text color in C? I'm working on a school project and i ...
hi...
Can anyone tell me how to change text color in C?
I'm working on a school project and i would like to change the text color depending on a users level.
Compiler/OS?
7. It is easier to write an incorrect program than understand a correct one.
40. There are two ways to write error-free programs; only the third one works.*
hey Dave!
I'm using Visual Studio 2005 - Windows Vista x64
7. It is easier to write an incorrect program than understand a correct one.
40. There are two ways to write error-free programs; only the third one works.*
humm...
i tryed this example, and it didn't work :-(
ERROR: "error C2065: 'MAGENTA' : undeclared identifier"Code:#include <stdio.h> #include <conio.h> int main ( void ) { textcolor ( MAGENTA ); cprintf ( "This is a test\n" ); return 0; }
*sigh*
You said you were running on Windows (64-bit version!). Use the Win32 (Win64 anyway?)approach listed there, not the DOS one.
![]()
i dont understand what the codes does :-S
This way i only have 5 possible colors, why?Code:HANDLE h = GetStdHandle ( STD_OUTPUT_HANDLE ); //->whats this for? cant it be changed? WORD wOldColorAttrs; //->whats this for? cant it be changed? CONSOLE_SCREEN_BUFFER_INFO csbiInfo; //->whats this for? cant it be changed? /* * First save the current color information */ GetConsoleScreenBufferInfo(h, &csbiInfo); wOldColorAttrs = csbiInfo.wAttributes; /* * Set the new color information */ SetConsoleTextAttribute ( h, FOREGROUND_RED | FOREGROUND_INTENSITY ); printf ( "This is a test\n" ); /* * Restore the original colors */ SetConsoleTextAttribute ( h, wOldColorAttrs); return 0; }
How can i now change the background color?
You have the RGB colors for both background and foreground. Using those three values and combining them, you can come up with other colors. Think about the combinations just foreground for a second:
- Red
- Green
- Blue
- Red + Green
- Red + Blue
- Blue + Green
- Red + Green + Blue
- Black (ie. No RGB)
Going through the code now....
HANDLE is a Windows API datatype that is just a another way of saying void *. It's a pointer to a "handle". Handles, and the many other similar types in Windows, are generally pointers to files and the like.Code:HANDLE h = GetStdHandle ( STD_OUTPUT_HANDLE ); //->whats this for? cant it be changed?
In this case, if you remember, when you write a C program, you have 3 "files" already opened for you:
- stdout
- stdin
- stderr
You may not realize you're using them. When you use printf(), you're writing to stdout. When you use scanf(), you're reading from stdin.
The code gets the HANDLE associated with stdout.
This is a WORD variable that is going to be used to store the old data color info. You generally want to do this because if you change the color of the console text in your application, when your program is terminated, the color will not revert back to what it was before. That is your job to set it back to normal.Code:WORD wOldColorAttrs; //->whats this for? cant it be changed?
This is another one of those large structs that Microsoft has. The name of the datatype tells you that it contains information on the console.Code:CONSOLE_SCREEN_BUFFER_INFO csbiInfo; //->whats this for? cant it be changed?
The comment says this is one way of saving the current console text color scheme.Code:/* * First save the current color information */ GetConsoleScreenBufferInfo(h, &csbiInfo); wOldColorAttrs = csbiInfo.wAttributes;
Obviously this changes the color of the text. The interesting thing you should realize is that it performs this on HANDLE h. Remember, h is just a HANDLE to the file representing stdout. This is how Windows will know which console window you want to change the color.Code:/* * Set the new color information */ SetConsoleTextAttribute ( h, FOREGROUND_RED | FOREGROUND_INTENSITY );
Obvious.Code:printf ( "This is a test\n" );
Simple restore the colors back to what they were before you changed them. That way when your program quits, the colors are back to normal.Code:/* * Restore the original colors */ SetConsoleTextAttribute ( h, wOldColorAttrs);
wow!!
tnx alot MacGyver, you rock!
couldn't ask for a better explanation ;-)
going to start some color testing![]()