Click here to Skip to main content
Click here to Skip to main content

WPF Command Prompt

By , 29 Aug 2011
Rate this:
Please Sign up or sign in to vote.
Sample Image - maximum width is 600 pixels

Introduction

WPF Command Prompt is a command line console that comes with a number of features including:

  • Save/Load different console settings (or use internal defaults)
  • Save/Load different command histories
  • Save/Load style themes (or use internal defaults)
  • Message area background color, fonts/font sizes/font colors, border size/color and padding size manually or using themes
  • Command prompt background color, font/font size/font colors, border size/color and padding size manually or using themes
  • Internal or external command parsing
  • Command history function

The basic layout has a message area (RichTextBox) and a command prompt area (TextBox) in a user control. I kept the command prompt at the bottom of the window as there is a resizing issue I have yet to work out. But, after using it for a while, I find I prefer the command prompt at the bottom of the window anyway. The prompt text (e.g. "C:\MyStuff\>") in the command prompt area is protected for being changed or deleted by the end user.

Sample Image - maximum width is 600 pixels

The message area consists of two areas, the message prompt area and the message text area:

Sample Image - maximum width is 600 pixels

The message prompt area contains the text entered from the command prompt. The message text contains text written to the console.

For brevity, not all properties/methods are shown here. The complete MSDN style documentation for WPFCommandPrompt can be found at http://cgdn.blogdns.net:8080/. See the project demo for examples of most of the functionality. This project can also be downloaded from my web site at http://codegravy.dyndns.org:8080/.

Background

WPF Command Prompt is the end result of a project I started a few months ago when I needed a console app for a project I was working on.

Using the Code

Creating a new command prompt is as simple as:

using WPFCommandPrompt;

WPFPrompt commandPrompt = new WPFPrompt();
commandPrompt.ReadLine += new ReadLineEventHandler(ProcessCommand);
commandPrompt.Show();

private void ProcessCommand(object sender, ConsoleReadLineEventArgs e)
{
    // Process commands sent from the console here
}

public void WriteLine(string output)
{
    commandPrompt.WriteLine(output);
}

There are two constructors to choose between:

public WPFPrompt() 	// Uses default style theme: styleThemeIndex = 0
public WPFPrompt(int styleThemeIndex) // The index of the style theme to use

Writing to the console:

// Sends a string to the console.
public void WriteLine(string output)

// Sends a string with specified brush color to the console.
public void WriteLine(string output, Brush foreground)

// Sends a FlowDocument paragraph to the console.
public void WriteLine(Paragraph paragraph)

// Sends a ConsoleWriteLineEventArgs object to the console.
public void WriteLine(object sender, ConsoleWriteLineEventArgs e)

The following basic properties can be set without creating the console window and can be saved/loaded from disk if not setting or using defaults:

// Allow an empty command to be written to the message area. (Default: false).
public bool AllowEmptyCommand

// The window title
public string ConsoleTitle 

// The assembly version of the console
public string ConsoleVersion 

// Gets or sets the default console height
public double DefaultConsoleHeight 

// Gets or sets the default console width
public double DefaultConsoleWidth

// Gets or sets the default prompt string (Default: >)
public string DefaultPrompt

// Gets or sets the delimiter regular expression string. 
// Default: ((""((?<token>.*?)"")|(?<token>[\w]+))(\s)*)
public string Delimiters

// Enable or disable the command history (Default: true = enabled)
public bool EnableCommandHistory

// Automatically try to load the style themes on console startup
public bool EnableLoadStyleThemes

// Is the command history managed by the console(auto) 
// or by the user(manual) (Default: false = auto) 
public bool ManualCommandHistory

// Gets or sets the maximum console height. Default is 0, no max size.
public double MaxConsoleHeight

// Gets or sets the maximum console width. Default is 0, no max size.
public double MaxConsoleWidth

// Gets or sets the maximum allowed font size of the console.
public double MaxFontSize

// Gets or sets the minimum console height. Default is 400.
public double MinConsoleHeight

// Gets or sets the minimum console width. Default is 600.
public double MinConsoleWidth

// Gets or sets the minimum allowed font size of the console.

// Gets or sets the current prompt string
public string Prompt

// Gets or sets the default style theme index number.
public int StyleThemeIndex

// Gets or sets the welcome message. Displayed upon console startup.
public string WelcomeMessage

// Should the console parse commands internally. Default: true.
// If false, the user is responsible for parsing the returned command string.
public bool UserInternalCommandParsing

Style Themes - Any changes to the theme are not changed in the original theme. Call UpdateStyleTheme() to copy any changes to the current theme to the original, or CurrentThemeToNew() to create a new theme from the current theme and any changes. Call SaveStyleThemes() to save to disk after calling UpdateStyleTheme() or CurrentThemeToNew(). Note that the provided style themes look ok but are far from great as I'm far better at making a UI functional than aesthetically pleasing. If anyone creates some nice looking themes, please share them with everyone here!

Points of Interest

Although I think there's far too much code in some of the classes, it works just fine. I'll think about re-doing things later Smile | :) .

History

  • Current version 1.0

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

About the Author

Clavius Maximus
Software Developer
United States United States
I started out with VBA, then moved to C# and haven't looked back Smile | :)

Comments and Discussions

 
GeneralMy vote of 5 Pinmemberibrahim_ragab24-Aug-13 21:26 
GeneralMy vote of 5 Pinmemberbartolo5-Sep-11 4:16 
GeneralRe: My vote of 5 PinmemberClavius Maximus6-Sep-11 13:02 
GeneralMy vote of 5 PinmemberXiaoChuan Yu31-Aug-11 12:56 
GeneralRe: My vote of 5 PinmemberClavius Maximus6-Sep-11 12:53 
GeneralMy vote of 5 PinmemberAshley Davis29-Aug-11 17:04 
GeneralRe: My vote of 5 PinmemberClavius Maximus29-Aug-11 19:22 
QuestionMy vote of 5 PinmemberFilip D'haene29-Aug-11 13:00 
AnswerRe: My vote of 5 PinmemberClavius Maximus29-Aug-11 15:42 

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

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

| Advertise | Privacy | Mobile
Web03 | 2.8.140421.2 | Last Updated 29 Aug 2011
Article Copyright 2011 by Clavius Maximus
Everything else Copyright © CodeProject, 1999-2014
Terms of Use
Layout: fixed | fluid