SetColor - A Console-Window Color-Changing Tool

This source and application has been released into the public domain by Simon Cooke (

It is provided as is, and no liability is assumed by the author for any problems it may cause. It appears to work fine :)


A Win32 console-mode app that allows the user to set the color of following output text. This is somewhat better than the COLOR batch command, which forces the entire console window to change color.

Handy for colorizing batch scripts, and signalling error conditions.


  • SetColor.cpp - source code for setcolor utility.
  • SetColor.exe - setcolor utility


The app is called with a two-character parameter.
setcolor <foregroundcolor><backgroundcolor>
The parameter is:
  • 0 Black
  • 1 Blue
  • 2 Red
  • 3 Magenta
  • 4 Green
  • 5 Cyan
  • 6 Yellow
  • 7 Gray
  • 8 Dark Gray
  • 9 Bright Blue
  • a Bright Red
  • b Bright Magenta
  • c Bright Green
  • d Bright Cyan
  • e Bright Yellow
  • f Bright White
If foreground = background, the command does nothing.


c:/>setcolor fa
- all text output following the command will appear in bright white on a red background.
c:/>setcolor D0
- all text output will be in bright cyan, on a black background.

Compilation notes

Does not use the Microsoft C runtime library for space reasons. Imports kernel32.lib ONLY (ignores default libraries); compiled with minimize size, function level linking, debug output as map-file only, release mode turned on. Linker was set to use 32 byte alignment for size reasons [see notes]; compiled executable is 2Kb long.

Note: No usage information is compiled into the app to minimize size; static data entails another segment in the generated executable, which takes up space. Read The Frickin' Manual :)


May not work as-is on Windows 98; will work fine on Windows 2000/Windows NT4.0. This is because it was compiled with an alignment setting of 32 to minimize the size of the final application.


Download demo project - 1 Kb
Download source - 2 Kb


  • CloseHandle should not be used!

    Posted by qwe98765 on 01/27/2007 04:39pm

    In lines 38 and 45 a call to ::CloseHandle() is made. This is a mistake, because the handle is retrieved using ::GetStdHandle() function, which does not allocate a handle copy and merely retrieves existing handle to standard output. Calling ::CloseHandle() with such handle leads to closing console output for caller process (printf() and similar functions will no longer work). The only reason it works in this little program is because it doesn't output anything after a call to ::CloseHandle() and exits (console attributes persist across the processes for given console, handles don't).

Leave a Comment
  • Your email address will not be published. All fields are required.

Top White Papers and Webcasts

  • Featuring Doug Kaplan, Director of Customer Success at InfoArmor Live Event Date: October 26, 2016 @ 10 AM PT / 1 PM ET We'll be highlighting InfoArmor, a solution provider for identity protection software, who recently made the move to a cloud contact center to support their advanced threat protection services offered to their large user base in the cloud. Join InfoArmor and Genesys to hear: An insider view of how InfoArmor built a justification and gained consensus to make the move to the cloud Tips on …

  • Protect Your Business against Today's Rapidly Growing Threats Your business, employees, partners, and customers are depending more and more on network connectivity, and protecting data and infrastructure has moved to the top of the IT agenda. You can't arm yourself against today's rapidly increasing threats unless you understand how they work. Read this ebook to learn more about the top 10 DNS attacks that can target your external and internal DNS infrastructure, the impact they can have on the DNS server …

Most Popular Programming Stories

More for Developers

RSS Feeds

Thanks for your registration, follow us on our social networks to keep up-to-date