Click here to Skip to main content
11,929,675 members (55,480 online)
Click here to Skip to main content
Add your own
alternative version


32 bookmarked

Putting colour/color to work on the console

, 9 Jul 2002
Rate this:
Please Sign up or sign in to vote.
How to change the colour/color of text in a console app


Whilst developing application running on the console is probably not the most exciting place, it is a great place to use for test harnesses or utilities. Of course when you are outputting text to the console window it can get pretty hard to see what's going on - what you need to be able to do is change the colour of the output text...

However, one of my main gripes with the .NET framework is the amount of functionality missing from various areas - don't even get me started about some of the problems with the System.Net area. This articles gives a quick intro into how to resolve some of the problems with the Console class.

.NET Console applications appear to use the standard Win32 console, implemented in kernel32.dll. This means that all of the console functions documented in the Platform SDK will work. (see MSDN) for the SDK details.

In order to make changing colours easy and simple, we will implement a static class that allows the console colour to be changed:


using System;

// need this to make API calls
using System.Runtime.InteropServices;

namespace PFitzsimons.ConsoleColour
    /// <summary>
    /// Static class for console colour manipulation.
    /// </summary>
    public class ConsoleColour
        // constants for console streams
        const int STD_INPUT_HANDLE = -10;
        const int STD_OUTPUT_HANDLE = -11;
        const int STD_ERROR_HANDLE = -12;

        private static extern IntPtr GetStdHandle
            int nStdHandle // input, output, or error device

        private static extern bool SetConsoleTextAttribute
            IntPtr hConsoleOutput, // handle to screen buffer
            int wAttributes    // text and background colors

        // colours that can be set
        public enum ForeGroundColour
            Black = 0x0000,
            Blue = 0x0001,
            Green = 0x0002, 
            Cyan = 0x0003,
            Red = 0x0004,
            Magenta = 0x0005,
            Yellow = 0x0006,
            Grey = 0x0007,
            White = 0x0008

        // class can not be created, so we can set colours 
        // without a variable
        private ConsoleColour()

        public static bool SetForeGroundColour()
            // default to a white-grey
            return SetForeGroundColour(ForeGroundColour.Grey);

        public static bool SetForeGroundColour(
            ForeGroundColour foreGroundColour)
            // default to a bright white-grey
            return SetForeGroundColour(foreGroundColour, true);

        public static bool SetForeGroundColour(
            ForeGroundColour foreGroundColour, 
            bool brightColours)
            // get the current console handle
            IntPtr nConsole = GetStdHandle(STD_OUTPUT_HANDLE);
            int colourMap;
            // if we want bright colours OR it with white
            if (brightColours)
                colourMap = (int) foreGroundColour | 
                    (int) ForeGroundColour.White;
                colourMap = (int) foreGroundColour;

            // call the api and return the result
            return SetConsoleTextAttribute(nConsole, colourMap);

Now that we have a console colour class we can use it anywhere in our apps to change the current colour. Note that we can use any standard .NET console calls, everything will work - just in colour - reminds me of all those new mobile adverts, who can make such a fuss out of a bit of colour? Smile | :)

Now we can put our class to work:


using System;

// we want color output
using PFitzsimons.ConsoleColour;

namespace MyApp
    public class MyApp
        public static void Main()

            Console.WriteLine("Text in green");


            Console.WriteLine("Text in red");

            // reset console back to a default

As you can see the code is pretty simple, but from a user point of view - its much easier to read the output.


As you can see, its' pretty straight forward, in production code you would expect have the class tell you what the current console colour was, use properties rather than function etc - but I've tried to keep the code to a minimum to show how simple this is.

If you read the SDK you will see that you can also position the cursor, peek at console input (useful to see if the user has pressed a key), set font sizes and set the window title. These are all things that should have been in the Console class to begin with - lets hope MS resolve this in the next version of the framework, until then we are all going to have to write ancillary classes to fix these kinds of omissions.


This article has no explicit license attached to it but may contain usage terms in the article text or the download files themselves. If in doubt please contact the author via the discussion board below.

A list of licenses authors might use can be found here


About the Author

Philip Fitzsimons
United Kingdom United Kingdom
still alive, just.
Campaign to bring back Windows 3.11 Look-N-Feel!

You may also be interested in...

Comments and Discussions

Questionwhat about an easier way Pin
Shivan114-Apr-09 22:16
memberShivan114-Apr-09 22:16 
AnswerRe: what about an easier way Pin
Philip Fitzsimons15-Apr-09 10:10
memberPhilip Fitzsimons15-Apr-09 10:10 
GeneralRe: what about an easier way Pin
postor24-Jan-10 21:38
memberpostor24-Jan-10 21:38 
GeneralLogical bug Pin
Meghani29-Jul-04 4:15
memberMeghani29-Jul-04 4:15 
Generalmulticoloured words Pin
jsilva9-Oct-03 15:58
memberjsilva9-Oct-03 15:58 
GeneralRe: multicoloured words Pin
Philip Fitzsimons10-Oct-03 0:44
memberPhilip Fitzsimons10-Oct-03 0:44 
Generalfix to ForeGroundColor enums Pin
msisson22-Jun-03 12:24
membermsisson22-Jun-03 12:24 
Generalgood class for colour console Pin
Paroca Lee27-May-03 19:44
memberParoca Lee27-May-03 19:44 
QuestionHmmm, Problem? Pin
Pumpkin Carver10-Jul-02 11:17
memberPumpkin Carver10-Jul-02 11:17 
AnswerRe: Hmmm, Problem? Pin
Andreas Philipson10-Jul-02 21:36
memberAndreas Philipson10-Jul-02 21:36 
GeneralRe: Hmmm, Problem? Pin
Philip Fitzsimons10-Jul-02 23:58
memberPhilip Fitzsimons10-Jul-02 23:58 
GeneralRe: Hmmm, Problem? Pin
Pumpkin Carver11-Jul-02 4:49
memberPumpkin Carver11-Jul-02 4:49 
GeneralRe: Hmmm, Problem? Pin
Philip Fitzsimons11-Jul-02 5:21
memberPhilip Fitzsimons11-Jul-02 5:21 
GeneralRe: Hmmm, Problem? Pin
Andreas Philipson11-Jul-02 5:49
memberAndreas Philipson11-Jul-02 5:49 
GeneralRe: Hmmm, Problem? Pin
Nick Hird19-Mar-03 5:51
memberNick Hird19-Mar-03 5:51 
GeneralNice! Pin
Andreas Philipson9-Jul-02 21:20
memberAndreas Philipson9-Jul-02 21:20 
GeneralRe: Nice! Pin
Philip Fitzsimons10-Jul-02 0:00
memberPhilip Fitzsimons10-Jul-02 0:00 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.

| Advertise | Privacy | Terms of Use | Mobile
Web03 | 2.8.151126.1 | Last Updated 10 Jul 2002
Article Copyright 2002 by Philip Fitzsimons
Everything else Copyright © CodeProject, 1999-2015
Layout: fixed | fluid