User Name Password Register
DaniWeb IT Discussion Community
All
What is DaniWeb IT Discussion Community?
You're currently browsing the C# section within the Software Development category of DaniWeb, a massive community of 402,500 software developers, web developers, Internet marketers, and tech gurus who are all enthusiastic about making contacts, networking, and learning from each other. In fact, there are 2,753 IT professionals currently interacting right now! Registration is free, only takes a minute and lets you enjoy all of the interactive features of the site.
Please support our C# advertiser: Programming Forums
Dec 30th, 2004
Views: 18,756
This short piece of code shows you how to display text in color in a C# windows console program. The corresponding WIN32 API functions are in the kernel32.dll and are imported and declared external. The only other problem is to assign variable types that accommodate the types listed in the Win32 API help file.
csharp Syntax | 4 stars
  1. // change text color in Windows console mode
  2. // colors are 0=black 1=blue 2=green 4=red and so on to 15=white
  3. // colorattribute = foreground + background * 16
  4. // to get red text on yellow use 4 + 14*16 = 228
  5. // light red on yellow would be 12 + 14*16 = 236
  6. //
  7. // the necessary WinApi functions are in kernel32.dll
  8. // in C# Handle = IntPtr DWORD = uint WORD = int
  9. // STD_OUTPUT_HANDLE = 0xfffffff5 (from winbase.h)
  10. //
  11. // a Console Application tested with VCS.NET 2003
  12.  
  13. using System;
  14. using System.Runtime.InteropServices; // DllImport()
  15.  
  16. namespace TextColor1
  17. {
  18. class MainClass
  19. {
  20. [DllImport("kernel32.dll")]
  21. public static extern bool SetConsoleTextAttribute(IntPtr hConsoleOutput,
  22. int wAttributes);
  23. [DllImport("kernel32.dll")]
  24. public static extern IntPtr GetStdHandle(uint nStdHandle);
  25.  
  26. public static void Main(string[] args)
  27. {
  28. uint STD_OUTPUT_HANDLE = 0xfffffff5;
  29. IntPtr hConsole = GetStdHandle(STD_OUTPUT_HANDLE);
  30. // increase k for more color options
  31. for (int k = 1; k < 255; k++)
  32. {
  33. SetConsoleTextAttribute(hConsole, k);
  34. Console.WriteLine("{0:d3} I want to be nice today!",k);
  35. }
  36. // final setting
  37. SetConsoleTextAttribute(hConsole, 236);
  38.  
  39. Console.WriteLine("Press Enter to exit ...");
  40. Console.Read(); // wait
  41. }
  42. }
  43. }
  44.  
Comments (Newest First)
armando.miani | Newbie Poster | Jun 25th, 2008
Why don't use:

Console.ForegroundColor = ConsoleColor.Red;
Console.WriteLine("Printing in red...");

Console.ForegroundColor = ConsoleColor.Red;
Console.WriteLine("Printing in green...");

Console.ForegroundColor = ConsoleColor.Blue;
Console.WriteLine("Printing in blue...");
catapult | Newbie Poster | Jun 22nd, 2008
Change Console Color in C#

using System;
using System.Runtime.InteropServices; // DllImport() needed to change the backgr color

namespace color_console
{
class MainClass
{
// DllImport - change the backgr color
[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)
{


//use white font and change bckgr color to blue
uint STD_OUTPUT_HANDLE = 0xfffffff5;
IntPtr hConsole = GetStdHandle(STD_OUTPUT_HANDLE);
SetConsoleTextAttribute(hConsole, 15);
Console.BackgroundColor = ConsoleColor.Blue;
Console.Clear();
// THAT WAS ALL the code needed to change the background color


// print something
Console.WriteLine("Hello World!\n\nPress Enter to continue...");
Console.Read();


//the rest of the code...







// OTHER EXAMPLES of color background
String[] colorNames = ConsoleColor.GetNames(typeof(ConsoleColor));

// cannot print all colors under ConsoleColor, only 10 out of 16
for (int x = 0; x < colorNames.Length; x++)
{

Console.BackgroundColor = (ConsoleColor)Enum.Parse(typeof(ConsoleColor), colorNames[x]);
Console.Clear();

// this is just to ptrint on white background wiht coloured font
if (Console.BackgroundColor == ConsoleColor.White)
{ Console.ForegroundColor = ConsoleColor.Blue; }

Console.Write("This is background color {0}.", colorNames[x]);
Console.WriteLine("\n\nPress Enter to continue...");
Console.Read();
}





// next section shows different combinations of font and font_backgr colors

Console.BackgroundColor = ConsoleColor.White;
Console.Clear();

// increase k for more color options
for (int k = 1; k < 255; k++)
{
SetConsoleTextAttribute(hConsole, k);
Console.WriteLine("{0:d3} coloured font wiht background !", k);
}

// final setting
SetConsoleTextAttribute(hConsole, 236);

}
}
}
vegaseat | Kickbutt Moderator | Jan 4th, 2005
Sorry, finally got it right
4 + 14*16 = 228
Post Comment

Only community members can submit or comment on code snippets. You must register or log in to contribute.

DaniWeb Marketplace (Sponsored Links)
All times are GMT -4. The time now is 5:36 pm.
Forum system based on vBulletin Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
©2003 - 2008 DaniWeb® LLC