表单应用程序 - 输出到命令提示符而不创建其他控制台

时间:2015-07-20 09:54:46

标签: c# .net winforms cmd output

我有点奇怪的问题。

我已经创建了一个表单应用程序,包括菜单,选项,按钮等。此外,我已经实现了使用参数打开和关闭某些选项以及从命令提示符启动应用程序的可能性。现在我想对其他“帮助”参数实施反应,我希望它显示有关所有可能参数和一些示例的信息。

有没有办法向我正在运行的控制台显示一些输出,而无需创建其他控制台?或者只是显示带有所有参数描述的新MessageBox会更容易吗?

谢谢!

2 个答案:

答案 0 :(得分:1)

如果你没有使用控制台的重要原因 - 我会使用MessageBox。

混合控制台和Windows窗体不是一个好主意。

如果你真的必须这样做 - <div class="field"> <%= f.label :route %><br> <%= f.collection_select(:route_id,Route.all,:id,:as_field) %><br> </div> <div class="field"> <%= f.label :departure_city %><br> <%= f.collection_select(:dep_city_id,Stop.all,:id,:city) %><br> </div> <div class="field"> <%= f.label :arrival_city %><br> <%= f.collection_select(:arr_city_id,Stop.all,:id,:city) %><br> </div> 中有AttachConsole功能。您可以像这样使用它:

Program.cs文件:

kernel32.dll

答案 1 :(得分:1)

这是我用来在需要时将控制台添加到应用程序的地方:

#region Console support
[System.Runtime.InteropServices.DllImport("Kernel32")]
public static extern void AllocConsole();

[System.Runtime.InteropServices.DllImport("Kernel32")]
public static extern void FreeConsole();
#endregion

当我们需要启用此功能时,我们可以在我们想要将其关闭时调用AllocConsole(),同样FreeConsole()

同样,我创建/使用以下内容用颜色写入控制台:

/// <summary>
/// Writes to the Console. Does not terminate line, subsequent write is on right of same line.
/// </summary>
/// <param name="color">The color that you want to write to the line with.</param>
/// <param name="text">The text that you want to write to the console.</param>
public static void ColoredConsoleWrite(ConsoleColor color, string text)
{
    ConsoleColor originalColor = Console.ForegroundColor;
    Console.ForegroundColor = color;
    Console.Write(text);
    Console.ForegroundColor = originalColor;
}

/// <summary>
/// Writes to the Console. Terminates line, subsequent write goes to new line.
/// </summary>
/// <param name="color">The color that you want to write to the line with.</param>
/// <param name="text">The text that you want to write to the console.</param>
public static void ColoredConsoleWriteLine(ConsoleColor color, string text)
{
    ConsoleColor originalColor = Console.ForegroundColor;
    Console.ForegroundColor = color;
    Console.WriteLine(text);
    Console.ForegroundColor = originalColor;
}

使用示例:

#region User Check
Console.Write("User: {0} ... ", Environment.UserName);
if (validUser(Environment.UserName).Equals(false))
{
    ColoredConsoleWrite(ConsoleColor.Red, "BAD!");
    Console.WriteLine(" - Making like a tree, and getting out of here!");
    Environment.Exit(0);
}
ColoredConsoleWrite(ConsoleColor.Green, "GOOD!"); Console.WriteLine(" - Continue on!");
#endregion

“GOOD!”的有效用户输出以绿色文字:

User: Chase.Ring ... GOOD! - Continue on!

“BAD!”无效的用户输出在红色文字中:

User: Not.Chase.Ring ... BAD! - Making like a tree, and getting out of here!