C# Tutorials and offshore development in India
    Tutorials   Resources   Forum   Reviews   Communities   Interview   Jobs   Projects   Training   Your Ad Here    
Silverlight Games | Mentor | Code Converter | Articles | Code Factory | Computer Jokes | Members | Peer Appraisal | IT Companies | Bookmarks | Polls | Revenue Sharing | Lobby | Gift Shop |


Prizes & Awards
My Profile



Active Members
TodayLast 7 Days more...






Resources » Articles » .NET Framework »

Coloring Console Window and changing Title of console


Posted Date: 13 Oct 2004    Resource Type: Articles    Category: .NET Framework
Author: Kashif BilalMember Level: Bronze    
Rating: 1 out of 5Points: 5



Coloring the console


When working with console applications in c#, always a black screen with white foreground comes. We can change the fore color as well as back ground color of our console application by using win32 API SetConsoleTextAttribute().Its my first article so be kind enough.

SetConsoleTextAttribute takes two arguments

1. Handle to console screen buffer
2. character attributes

BOOL SetConsoleTextAttribute(
HANDLE hConsoleOutput,
WORD wAttributes
);


We can get handle to console screen buffer by using another function of win32 API i.e. GetStdHandle(),which takes a parameter and returns handle for input, output or error device.
We give -10 for input,-11 for output and -12 for error device as parameter to GetStdHandle function.

We have attributes for fore ground and background colors like 0x0001 for fore ground blue and 0x0010 for back ground blue.

How Use Win32 API Function in C#.



First of all declare the function using DllImport attribute. An API function must be declared static extern.


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*/

/* declaring the getstdhandle funtion to get thehandle that
would be used in setConsoletextattribute function */
[DllImport("kernel32.dll")]
public static extern IntPtr GetStdHandle(int nStdHandle);

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
}
}
}


Change the Title of Console:



Changing the Title of Console is also much easier, just use SetConsoleTitle()function and provide a string to it as 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 ... ");


So You have seen that how easily we can use win32 API’s in C# and can do a lot of things that are not provided by .NET Framework. We can also change the font and cursor of console application using win32 API’s.

Thanks …
Kashif Bilal




Responses


No responses found. Be the first to respond and make money from revenue sharing program.

Feedbacks      
Popular Tags   What are tags ?   Search Tags  
Sign In to add tags.
(No tags found.)

Post Feedback


This is a strictly moderated forum. Only approved messages will appear in the site. Please use 'Spell Check' in Google toolbar before you submit.
You must Sign In to post a response.
Next Resource: To access Oracle Database from VB.NET application using ADO.NET
Previous Resource: Securing Remoting Calls and Validating Client
Return to Discussion Resource Index
Post New Resource
Category: .NET Framework


Post resources and earn money!
 
Related Resources



dotNet Slackers

About Us    Contact Us    Privacy Policy    Terms Of Use