Originally published on: Sat, 20 Mar 2010 23:41:26 +0000
A number of visitors to my blog end up here via a search for information on changing the text color in a C program. Unfortunately, the only post I have on the subject pertains to changing the output text color for the Commodore 64. I doubt that the technique I used there is of any kind of importance to these searchers.
In a Win32 environment, the console mode text background color and foreground color can be changed by a single Windows API function. The SetConsoleTextAttribute() function was first exposed via kernel32.dll in Windows 2000.
This API call accepts a console output handle as the first parameter. The second parameter is the attribute used to control both the background color and foreground color. The console text color palette is divided into sixteen different values. Each color value can be represented by one hexadecimal digit:
When specifying a digit to the SetConsoleTextAttribute() function, the background color is specified in the first digit and the foreground color is specified in the second. Passing the value 1F to SetConsoleTextAttribute() would yield a blue background with bright white text.
Note that you can already experiment with these color combinations from the command prompt by using the standard color command:
color 1F
By typing the above command at a command prompt, the entire screen should now show bright white on blue. To go back to the default color scheme simply type:
color
Do not supply any parameters to the above.
Unfortunately, my C compiler is a bit old, so I did not have the definitions nor link-time information to allow me to easily compile a program. As such, I had to explicitly attempt to load the function from kernel32.dll as a pointer. I then had invoked the function using the asterisk as a dereference operator via my function-pointer named doSetConsoleTextAttribute()
I wrote a demonstration program that will loop through all 256 combinations of foreground and background colors and will display them in the console window. You may have to scroll up in your console window to see all of the output.

colordemo.c
In addition to this demo, I've written a command-line utility called chgcolor. The post describing how to use this utility in batch files ( with C source code ) can be found at http://www.mailsend-online.com/blog?p=77. A link to the EXE file can also be found there.
The source code and executable file for colordemo can be downloaded from a single archive at: http://www.mailsend-online.com/wp/colordemo.zip
Unless otherwise noted, all code and text entries are Copyright ©2010 by James K. Lawless
Save to del.icio.us
Save to StumbleUpon
Digg it
Save to Reddit
Share on Facebook
Share on Twitter
More bookmarks
Subscribe!
Auto Save Clipboard Images Redux
Extending SpiderMonkey JavaScript on Windows
Rhino JavaScript to EXE with launch4j
Compiling Rhino JavaScript to Java
Directory Traversal in Rhino JavaScript
A Command-Line MP3 Player for Windows
Auto Save Images from the Clipboard
Java in a Windows EXE with launch4j
An Interview with Tom Zimmer: Forth System Developer
Setting Windows Console Text Colors in C
Understanding TRS-80 CMD Files
Setting Text Color in a Batch File
Jim Butterfield : The Commodore Guru
Book Review : Using Google App Engine
Thwarting HTTP Referer Trackbacks
A Simple Associative Array Library in C
Mad Schemes : Learning Lisp via SICP
MicroISV on a Shoestring
DadHacker
The Bottom Feeder
Writin' That Code!
The Recursive ISV
The Thomsen Blog
Prototypically Speaking
The Reinvigorated Programmer