Search Forum
(53671 Postings)
Search Site/Articles

Archived Articles
712 Articles

C# Books
C# Consultants
What Is C#?
Download Compiler
Code Archive
Archived Articles
Advertise
Contribute
C# Jobs
Beginners Tutorial
C# Contractors
C# Consulting
Links
C# Manual
Contact Us
Legal

GoDiagram for .NET from Northwoods Software www.nwoods.com


 
Printable Version

Console Apps: Colour Text
By Philip Fitzsimons

Introduction

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 form 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 the problems with the Console class.

.Net Console applications appear to use the win32 standard console, implemented in "Kernel32.dll". This means that all of the console functions documented in the Platform SDK will work. (see MSDN http://msdn.microsoft.com/library/default.asp?url=/library/en-us/dllproc/conchar_3vg3.asp?frame=true) for the SDK details.

ConsoleColour.cs
using System;
using System.Runtime.InteropServices;

namespace PFitzsimons.ConsoleColour
{
  /// 
  /// Static class for console colour manipulation.
  /// 
  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;

    [DllImportAttribute("Kernel32.dll")]
    private static extern IntPtr GetStdHandle
    (
      int nStdHandle      // input, output, or error device
    );

    [DllImportAttribute("Kernel32.dll")]
    private static extern bool SetConsoleTextAttribute
    (
      IntPtr hConsoleOutput,  // handle to screen buffer
      int wAttributes        // text and background colors
    );

    // colours that can be set
    [Flags]
    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 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;
      else
        colourMap = (int)foreGroundColour;

      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, everythink will work - just in colour Console.cs
// we want color output
using PFitzsimons.ConsoleColour;

namespace MyApp
{
  public class MyApp
  {
      ConsoleColour.SetForeGroundColour(ConsoleColour.ForeGroundColour.Green);

      Console.WriteLine("Text in green");

      ConsoleColour.SetForeGroundColour(ConsoleColour.ForeGroundColour.Red);

      Console.WriteLine("Text in red");

      // reset console back to a default
      ConsoleColour.SetForeGroundColour();
  }
}
As you can see the code is pretty simple, but from a user point of view - its much easier to read the output.

Conclusion

As you can see, it's 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 ommisions.