Friday, July 31, 2009

How to mak a color output text in visual c# 2005?

first: i am new at c# and i want to make an output text in the consol application as to output: "heloo world" but in green color?


second: i have a variable carry a number i want to get the square root of the number?

How to mak a color output text in visual c# 2005?
// change text color in Windows console mode


// colors are 0=black 1=blue 2=green 4=red and so on to 15=white


// colorattribute = foreground + background * 16


// to get red text on yellow use 4 + 14*16 = 228


// light red on yellow would be 12 + 14*16 = 236


//


// the necessary WinApi functions are in kernel32.dll


// in C# Handle = IntPtr DWORD = uint WORD = int


// STD_OUTPUT_HANDLE = 0xfffffff5 (from winbase.h)


//


// a Console Application tested with VCS.NET 2003





using System;


using System.Runtime.InteropServices; // DllImport()





namespace TextColor1


{


class MainClass


{


[DllImport("kernel32.dll")]


public static extern bool SetConsoleTextAttribute(IntPtr hConsoleOutput,


int wAttributes);


[DllImport("kernel32.dll")]


public static extern IntPtr GetStdHandle(uint nStdHandle);





public static void Main(string[] args)


{


uint STD_OUTPUT_HANDLE = 0xfffffff5;


IntPtr hConsole = GetStdHandle(STD_OUTPUT_HANDLE);


// increase k for more color options


for (int k = 1; k %26lt; 255; k++)


{


SetConsoleTextAttribute(hConsole, k);


Console.WriteLine("{0:d3} I want to be nice today!",k);


}


// final setting


SetConsoleTextAttribute(hConsole, 236);





Console.WriteLine("Press Enter to exit ...");


Console.Read(); // wait


}


}


}


No comments:

Post a Comment