Originally published on: Sat, 20 Mar 2010 23:41:26 +0000
Please note! If you're having difficulties compiling the C source code presented below, please see my post: Compiling C from the Command Line with Pelles C
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
Views expressed in this blog are those of the author and do not necessary reflect those of the author's employer. Views expressed in the comments are those of the responding individual.

Save to StumbleUpon
Digg it
Save to Reddit
Share on Facebook
Share on Twitter
More bookmarks
Compiling C from the Command Line with Pelles C
A Forthcoming Marvel Movie Villain
Uninstalling Problematic Windows Software
A JavaScript REPL for Android Devices
Compiling C from the Command Line with Pelles C
Extracting URL Addresses from Text in C
Internet Protocols and Rhino JavaScript
My First C64 Interrupt Program
Playing with OS/X Text to Speech
The Villain at the end of Marvel's Avengers Move is...
Processing GMail items with Ruby
Happy 30th Birthday, Commodore 64
MailWrench CSharp Command Line Mailer for Windows is now Free Software
Rainy Day Fun with the HTML DOM
Building a World War II Foxhole Radio
Prototerp – Unleashing a JavaScript REPL via the Mobile Browser