Coloring The Console

When working with console applications in c#,black screens with white foregrounds are always used. We can change theforeground color as well as background color of our console applicationby using win32 API SetConsoleTextAttribute().

SetConsoleTextAttribute takes two arguments
1. Handle to console screen buffer
2. character attributes
BOOL SetConsoleTextAttribute(
HANDLE hConsoleOutput,
WORD wAttributes
);

We can get the handle to the console screenbuffer by using a function of win32 API i.e. GetStdHandle(),which takesa parameter and returns a handle for input, output or error device.We give -10 for input,-11 for output and -12 for error device asparameter to GetStdHandle function.We have attributes for fore ground and background colors like 0x0001for fore ground blue and 0×0010 for back ground blue.

How To Use Win32 API Function in C#.

First of all declare the function using DllImport attribute. An API function must be declared static extern.DllImport is used to call an unmanaged code inside a mangaed code, so we must have to use it to call unmanaged win32 APIs.

Let's start an example

using System;
using System.Runtime.InteropServices; // for DllImport attribute

namespace color_console
{

class Class1
{

static void Main(string[] args)
{
//
// TODO: Add code to start application here
//
Class1 c =new Class1();
c.change();

}
[DllImport("kernel32.dll", SetLastError=true)]
public static extern bool SetConsoleTextAttribute(
IntPtr hConsoleOutput,
CharacterAttributes wAttributes); /* declaring the setconsoletextattribute function*/

[DllImport("kernel32.dll")]
public static extern IntPtr GetStdHandle(int nStdHandle); //declaring the getstdhandle funtion
/* to get thehandle that would be used in setConsoletextattribute function */
void change()
{
IntPtr hOut; /* declaring varianle to get handle*/
hOut= GetStdHandle(-11);/* -11 is sent for output device*/

/*Displaying text in different colors and background colors*/

SetConsoleTextAttribute(hOut, CharacterAttributes.FOREGROUND_BLUE );
Console.WriteLine(" Subhan ALLAH ");

SetConsoleTextAttribute(hOut, CharacterAttributes.BACKGROUND_RED);
Console.WriteLine(" Alkhamdolillah ");
SetConsoleTextAttribute(hOut, CharacterAttributes.BACKGROUND_GREEN );
Console.WriteLine(" Allah O Akbar ");
SetConsoleTextAttribute(hOut, CharacterAttributes.FOREGROUND_RED );
Console.WriteLine(" Pakistan ");

}
/* This enumeration lists all of the character attributes. You can combine attributes to */
/* achieve specific effects.*/

public enum CharacterAttributes
{
FOREGROUND_BLUE = 0×0001,
FOREGROUND_GREEN = 0×0002,
FOREGROUND_RED = 0×0004,
FOREGROUND_INTENSITY = 0×0008,
BACKGROUND_BLUE = 0×0010,
BACKGROUND_GREEN = 0×0020,
BACKGROUND_RED = 0×0040,
BACKGROUND_INTENSITY = 0×0080,
COMMON_LVB_LEADING_BYTE = 0×0100,
COMMON_LVB_TRAILING_BYTE = 0×0200,
COMMON_LVB_GRID_HORIZONTAL = 0×0400,
COMMON_LVB_GRID_LVERTICAL = 0×0800,
COMMON_LVB_GRID_RVERTICAL = 0×1000,
COMMON_LVB_REVERSE_VIDEO = 0×4000,
COMMON_LVB_UNDERSCORE = 0×8000
}

}

}

We can also change the font and cursor ofconsole application using win32 APIs.Changing the title of the console is also easy, just useSetConsoleTitle()function and provide a string to it as a parameter.That would be title of console. You can do it easily

[DllImport("kernel32.dll")
public static extern bool SetConsoleTitle(String lpConsoleTitle);
First declare setconsoletitle function and then use it
SetConsoleTitle(" ALlah O Akbar … ");

By
Kashif Bilal
kashiff@gmail.com

Twitter Digg Delicious Stumbleupon Technorati Facebook Email

No comments yet... Be the first to leave a reply!