表单应用程序 - 输出到命令提示符而不创建额外的控制台
Form Application - Output to Command Prompt without creation of additional consoles
我有个奇怪的问题。
我创建了一个带有菜单、选项、按钮等的表单应用程序。我还实现了使用参数打开和关闭某些选项的可能性,并从命令提示符启动应用程序。现在我想实现对附加 "help" 参数的反应,我希望它显示有关所有可能参数和一些示例的信息。
是否可以在不创建额外控制台的情况下向我当前 运行 的控制台显示一些输出?或者只显示带有所有参数描述的新 MessageBox 会更容易?
谢谢!
如果没有重要的理由让您使用控制台 - 我只会使用 MessageBox。
混合使用控制台和 windows 表单不是个好主意。
如果您真的必须这样做 - kernel32.dll
中有 AttachConsole 函数。你可以这样使用它:
Program.cs 文件:
using System;
using System.Windows.Forms;
using System.Runtime.InteropServices;
namespace Test
{
static class Program
{
[DllImport("kernel32.dll")]
static extern bool AttachConsole(int dwProcessId);
private const int ATTACH_PARENT_PROCESS = -1;
[STAThread]
static void Main()
{
AttachConsole(ATTACH_PARENT_PROCESS);
Console.WriteLine("This will show on console.");
Application.EnableVisualStyles();
Application.SetCompatibleTextRenderingDefault(false);
Application.Run(new Form1());
}
}
}
这是我在需要时将控制台添加到应用程序的方法:
#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()
当我们想关闭它时。
同样,我 created/use 将以下内容写入带有颜色的控制台:
/// <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!
我有个奇怪的问题。
我创建了一个带有菜单、选项、按钮等的表单应用程序。我还实现了使用参数打开和关闭某些选项的可能性,并从命令提示符启动应用程序。现在我想实现对附加 "help" 参数的反应,我希望它显示有关所有可能参数和一些示例的信息。
是否可以在不创建额外控制台的情况下向我当前 运行 的控制台显示一些输出?或者只显示带有所有参数描述的新 MessageBox 会更容易?
谢谢!
如果没有重要的理由让您使用控制台 - 我只会使用 MessageBox。
混合使用控制台和 windows 表单不是个好主意。
如果您真的必须这样做 - kernel32.dll
中有 AttachConsole 函数。你可以这样使用它:
Program.cs 文件:
using System;
using System.Windows.Forms;
using System.Runtime.InteropServices;
namespace Test
{
static class Program
{
[DllImport("kernel32.dll")]
static extern bool AttachConsole(int dwProcessId);
private const int ATTACH_PARENT_PROCESS = -1;
[STAThread]
static void Main()
{
AttachConsole(ATTACH_PARENT_PROCESS);
Console.WriteLine("This will show on console.");
Application.EnableVisualStyles();
Application.SetCompatibleTextRenderingDefault(false);
Application.Run(new Form1());
}
}
}
这是我在需要时将控制台添加到应用程序的方法:
#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()
当我们想关闭它时。
同样,我 created/use 将以下内容写入带有颜色的控制台:
/// <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!