|
|||||||||||||||||||||||||||||||
|
|||||||||||||||||||||||||||||||
|
Announcements
Want a new Job?
Chapters
Services
Feature Zones
|
IntroductionHere is a good question for the CodeProject poll. How many of us have wanted to write an interpreter for a language of our own? I'm sure that would include most of us. All of us who actually tried to write one would have written it as a command line application. What if I wanted to embed it in a Windows application? Out of that question was born this control, which I call the What it is?The
How to use it?Pretty simple. Add the control like any normal User Control (Right click on the ToolBox, click Add/Remove Items and Browse to ShellControl.dll). Subscribe to the public Form1()
{
//
// Required for Windows Form Designer support
//
InitializeComponent();
shellControl1.CommandEntered += new
UILibrary.EventCommandEntered(shellControl1_CommandEntered);
}void shellControl1_CommandEntered(object sender,
UILibrary.CommandEnteredEventArgs e)
{
string command = e.Command;
if (!ProcessInternalCommand(command))
{
ProcessStartInfo startInfo = new ProcessStartInfo("cmd.exe");
startInfo.Arguments = "/C " + e.Command;
startInfo.RedirectStandardError = true;
startInfo.RedirectStandardOutput = true;
startInfo.UseShellExecute = false;
startInfo.CreateNoWindow = true;
Process p = Process.Start(startInfo);
string output = p.StandardOutput.ReadToEnd();
string error = p.StandardError.ReadToEnd();
p.WaitForExit();
if (output.Length != 0)
shellControl1.WriteText(output);
else if (error.Length != 0)
shellControl1.WriteText(error);
}
}
The code above gets the command text and executes it by starting a new cmd.exe process. It then writes back the result to the control. It can't get any simpler, can it? Other Properties And MethodsBesides the event, the control exposes the following properties and methods: Properties
Methods
How it works?The
The overridden protected override void WndProc(ref Message m)
{
switch(m.Msg)
{
case 0x0302: //WM_PASTE
case 0x0300: //WM_CUT
case 0x000C: //WM_SETTEXT
if (!IsCaretAtWritablePosition())
MoveCaretToEndOfText();
break;
case 0x0303: //WM_CLEAR
return;
}
base.WndProc(ref m);
}
As you can see, For controlling key presses, the private void ShellControl_KeyDown(object sender, KeyEventArgs e)
{
// If the caret is anywhere else, set it back
// when a key is pressed.
if (!IsCaretAtWritablePosition() &&
!(e.Control || IsTerminatorKey(e.KeyCode)))
{
MoveCaretToEndOfText();
}
// Prevent caret from moving before the prompt
if (e.KeyCode == Keys.Left && IsCaretJustBeforePrompt())
{
e.Handled = true;
}
else if (e.KeyCode == Keys.Down)
{
if (commandHistory.DoesNextCommandExist())
{
ReplaceTextAtPrompt(commandHistory.GetNextCommand());
}
e.Handled = true;
}
else if (e.KeyCode == Keys.Up )
{
if (commandHistory.DoesPreviousCommandExist())
{
ReplaceTextAtPrompt(commandHistory.GetPreviousCommand());
}
e.Handled = true;
}
else
{
// Some more code here..
}
}
When the enter key is pressed, the ConclusionAlthough I'm fairly comfortable in C#, this is my first attempt at creating UserControls. I know this is not going to be the most downloaded control in CodeProject, but I feel it's still useful for certain situations where you want to emulate the command line. Feedback, comments are welcome. HistoryFeb 17 2005 - Initial submission.
|
||||||||||||||||||||||||||||||