Click here to Skip to main content
15,879,348 members
Articles / Programming Languages / C#
Tip/Trick

xConsole Project

Rate me:
Please Sign up or sign in to vote.
4.74/5 (13 votes)
23 May 2014GPL32 min read 33.4K   223   20   14
Coloring the window console

Introduction

Hi, it`s my project called xConsole, in few words this DLL gives you the ability to color, animate comfortably the console window, parse input, modify fonts and more.

It has been written in C# .NET 4.5.

Using the Library

  1. Add Reference > xConsole.dll
  2. using xConsoleProject;

Example of coloring:

C#
xConsole.WriteLine("^yLorem ipsum dolor sit amet^r, consectetur adipiscing elit. " + 
     "^.Nullam porttitor lectus justo, vel viverra est pellentesque non.^!"); 

^y Set Yellow Color
^r Set Red Color
^. Set a Random Color (different from the background)
^! Restore default font color

<code>*y *r *m Sets Colors
*. Random background Color
*! Restore default bg.

C#
xConsole.CoolWriteLine("I like write like a boss.");
 
// OR

xConsole.CoolWriting = true;
xConsole.WriteLine("^600I like write ^F00like a boss"); // Or 6 digits (^FF00FF)..
xConsole.WriteLine("Yes ^ycolors is ^msupported^!");
xConsole.CoolWriting = false;

[Updated] List of Colors

Using enum (now same of Microsoft):

C#
public enum ConsoleColor
{
    Black = 0,
    DarkBlue = 1,
    DarkGreen = 2,
    DarkCyan = 3,
    DarkRed = 4,
    DarkMagenta = 5,
    DarkYellow = 6,
    Gray = 7,
    DarkGray = 8,
    Blue = 9,
    Green = 10,
    Cyan = 11,
    Red = 12,
    Magenta = 13,
    Yellow = 14,
    White = 15,
} 

HEX to ConsoleColor (the HEX code must be uppercase):

C#
xConsole.WriteLine("^600I like to paint ^F00like a boss");

With letters:

C#
xConsole.WriteLine("^yI like to paint ^rlike a boss");
w:    White
z:    Black
y:    Yellow
g:    Green
r:    Red
b:    Blue
c:    Cyan
m:    Magenta

Read Line

C#
var args = xConsole.ReadLine();

Will give back a List<string> (never null) contains the arguments parsed, entered by the user, Like:

Hi "this is a string" then 9999

C#
[0] = Hi
[1] = This is a string
[2] = then
[3] = 9999

An empty input: (list size 1, value empty string)

C#
[0] =

So you need to check if its string is Empty. Yeah, i don't like null values..

Spinner

This class allows you to create an animated spinner.

C#
var spinner = new xConsole.Spinner();
                
new Thread(delegate()
{
    //Spin until user interruption
    Console.ReadKey(true);

    spinner.Break();
    xConsole.Write("^11OK!^!");
}).Start();

xConsole.Write("^2STATUS: ^7"); 
// Waiting the thread is done.
while (spinner.Turn());

Move window

C#
SetWindowPos(int x, int y); 

Other

I also added console helper.

C#
xConsole.SetFont(int);  

Set font, list font, change icon...

Next Update

  • Sound management?

History

23/05/2014
  • Now check updates every 30 days (only in debug)
  • No more create registry key in release
  • Removed Stats sending
  • Removed Settings
  • Fixed ClearColorsAtEnd function (restoring previous colors)
  • Added Test program (Both sources)
21/01/2014
  • Released source code (v 0.3)
  • It's not a spywere see above
18/01/2014
  • [NEW] Optional separate-thread writer (default ON)
  • [NEW] Random color always != from background
  • [NEW] Background colors using * tag
  • [NEW] Update Checking (only in debug)
  • [NEW] New Functions like WaitQueue (main program wait until the queue is resolved)
  • [UPDATED] Fixed a bug in the spinning
  • [UPDATED] Spinning now supports colors
  • [???] A lot of emoji in the code
  • [Source Code] will be available, I need to make it beautiful, btw you can reflect it
  • [It would] be nice to do a port to C++
27/07/2013
  • Improved method of color selection
    Now you can use ^RBG colors (3bytes), like ^F00 = red
    ^00F = Blue, ^0F0 = green, ^600 = darkred ...etc... (RBG colors is rounded to ConsoleColors)
    and ^y = yellow
    ^r = red
    ^m = magenta .. etc
  • Random color moved to ^.

License

This article, along with any associated source code and files, is licensed under The GNU General Public License (GPLv3)


Written By
Italy Italy
This member has not yet provided a Biography. Assume it's interesting and varied, and probably something to do with programming.

Comments and Discussions

 
GeneralMy vote of 5 Pin
Volynsky Alex23-May-14 12:18
professionalVolynsky Alex23-May-14 12:18 
GeneralMy vote of 1 Pin
KenL4cp20-Jan-14 10:03
KenL4cp20-Jan-14 10:03 
Question5 Pin
Assil20-Jan-14 5:15
professionalAssil20-Jan-14 5:15 
GeneralMy vote of 1 Pin
Spirch20-Jan-14 5:10
Spirch20-Jan-14 5:10 
GeneralRe: My vote of 1 Pin
TheTrigger21-Jan-14 6:15
TheTrigger21-Jan-14 6:15 
Bugwarning this code send information to third party Pin
Spirch20-Jan-14 5:05
Spirch20-Jan-14 5:05 
GeneralRe: warning this code send information to third party Pin
KenL4cp20-Jan-14 9:28
KenL4cp20-Jan-14 9:28 
GeneralRe: warning this code send information to third party - Updated and Fixed Pin
DaveyM6921-Jan-14 1:37
professionalDaveyM6921-Jan-14 1:37 
QuestionCould you please add a version of this code here on CodeProject.? Pin
Pete O'Hanlon29-Jul-13 22:21
mvePete O'Hanlon29-Jul-13 22:21 
GeneralMy vote of 4 Pin
Assil27-Jul-13 11:33
professionalAssil27-Jul-13 11:33 
Cool one, but where is the code?
GeneralMy vote of 4 Pin
Carlos190726-Jul-13 6:36
professionalCarlos190726-Jul-13 6:36 
GeneralRe: My vote of 4 Pin
TheTrigger26-Jul-13 21:43
TheTrigger26-Jul-13 21:43 
SuggestionRe: My vote of 4 Pin
Carlos19073-Aug-13 5:27
professionalCarlos19073-Aug-13 5:27 
GeneralMy vote of 4 Pin
fredatcodeproject26-Jul-13 4:56
professionalfredatcodeproject26-Jul-13 4:56 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.