Click here to Skip to main content
15,894,907 members
Articles / Programming Languages / XML

Coloring Consle and changing its Title

Rate me:
Please Sign up or sign in to vote.
2.35/5 (11 votes)
12 Oct 20041 min read 35K   410   15  
This article explains how to change fore and back ground color of text in a console application. It also explains how we can change the title of Console windows and the use of Win32 APIs.
using System;
using System.Runtime.InteropServices; // for DllImport attribute 

namespace color_console
{
	
	class Class1
	{
		
		
		static void Main(string[] args)
		{
			
			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 = 0x0001,
			FOREGROUND_GREEN = 0x0002,
			FOREGROUND_RED = 0x0004,
			FOREGROUND_INTENSITY = 0x0008,
			BACKGROUND_BLUE = 0x0010,
			BACKGROUND_GREEN = 0x0020,
			BACKGROUND_RED = 0x0040,
			BACKGROUND_INTENSITY = 0x0080,
			COMMON_LVB_LEADING_BYTE = 0x0100,
			COMMON_LVB_TRAILING_BYTE = 0x0200,
			COMMON_LVB_GRID_HORIZONTAL = 0x0400,
			COMMON_LVB_GRID_LVERTICAL = 0x0800,
			COMMON_LVB_GRID_RVERTICAL = 0x1000,
			COMMON_LVB_REVERSE_VIDEO = 0x4000,
			COMMON_LVB_UNDERSCORE = 0x8000
		}

	}

}

By viewing downloads associated with this article you agree to the Terms of Service and the article's licence.

If a file you wish to view isn't highlighted, and is a text file (not binary), please let us know and we'll add colourisation support for it.

License

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


Written By
Instructor/Trainer
Pakistan Pakistan
I have done MCS recently . I likePoetry and Hicking.

Comments and Discussions