【问题标题】:Creating a terminal in C# WinForms program [closed]在 C# WinForms 程序中创建终端 [关闭]
【发布时间】:2016-01-30 02:11:13
【问题描述】:

我想在 C# WinForms 应用程序中构建一个终端,用于通过串行端口、网络终端或任何其他类型的命令/响应机制进行通信。

我认为最简单的方法是打开 Windows 控制台并将 stdin、stdout 和 stderr 流重定向到我程序中的 TextStreams,但我找不到明显的方法来执行此操作。我不想使用 System.Console 并将其与其他错误/调试消息混在一起。

如果我可以将控制台作为控件嵌入到 WinForm 上,那就更好了。

在没有任何第三方库或大量编码的情况下,这些解决方案是否可行?

谢谢!

【问题讨论】:

  • 不想使用System.Console是什么意思? System.Console 在 C# 中与控制台交互的方式...
  • System.Console 与我的应用程序有着内在的联系——你只能拥有一个,而且看起来一旦打开就无法关闭它。我想要一些独立的东西,而且我可以创造不止一个。
  • 啊,感谢您澄清这一点。在这种情况下,有几种方法可以解决这个问题 - 可能最好的方法是您建议的 WinForm 窗口。您实际上可以创建控制台的多个实例(请参阅this 问题),但我不建议这样做,因为当控制台窗口打开其他控制台窗口时,用户可能会感到有些困惑。我想如果你有 WinForms 解决方案,它会更清晰
  • 已经有这样的开源项目了,如果不想自己写代码,何不直接去看看呢?

标签: c# winforms console command-line-interface pty


【解决方案1】:

使用此类从 WinForms 访问控制台。 您将需要分配一个控制台并将其保存到一个变量中以供以后操作。我在下面包含了一些我在过去项目中使用过的示例代码。然后,您可以调用 Console.WriteLine("") 方法将文本发送到窗口。希望这会有所帮助!

public class ConsoleHelper
{
    public const int MF_BYCOMMAND = 0x00000000;
    public const int SC_CLOSE = 0xF060;

    /// <summary>
    /// Allocates a new console for current process.
    /// </summary>
    [DllImport("kernel32.dll")]
    public static extern Boolean AllocConsole();

    /// <summary>
    /// Frees the console.
    /// </summary>
    [DllImport("kernel32.dll")]
    public static extern Boolean FreeConsole();

    [DllImport("kernel32.dll")]
    public static extern IntPtr GetConsoleWindow();

    [DllImport("user32.dll")]
    public static extern bool ShowWindow(IntPtr hWnd, int nCmdShow);

    [DllImport("user32.dll")]
    public static extern int DeleteMenu(IntPtr hMenu, int nPosition, int wFlags);

    [DllImport("user32.dll")]
    public static extern IntPtr GetSystemMenu(IntPtr hWnd, bool bRevert);

    public const int SW_HIDE = 0;
    public const int SW_SHOW = 5;
}

这是一个快速演示:

    void OpenSerialConnection(int portnumber, System.Windows.Forms.NotifyIcon TrayIcon)
    {
        //Get console window
        handle = ConsoleHelper.GetConsoleWindow();
        if (handle.ToInt32() == 0)
        {
            //Create the console window
            ConsoleHelper.AllocConsole();
        }
        handle = ConsoleHelper.GetConsoleWindow();

        //Show console window
        ConsoleHelper.ShowWindow(handle, ConsoleHelper.SW_SHOW);

        //Remove close button from console window (can only be closed from code now)
        ConsoleHelper.DeleteMenu(ConsoleHelper.GetSystemMenu(ConsoleHelper.GetConsoleWindow(), false), ConsoleHelper.SC_CLOSE, ConsoleHelper.MF_BYCOMMAND);
        if (!serialPort1.IsOpen)
        {
            try
            {
                serialPort1.PortName = "COM" + portnumber.ToString();
                serialPort1.BaudRate = Form1.BaudRate;
                serialPort1.Open();
            }
            catch (Exception error)
            {
                TrayIcon.ShowBalloonTip(3000, "Error in " + error.Source.ToString(), "The program encountered an error while trying to open the serial connection. This may be due to closing the program prematurely. Try unplugging your board and restarting the program!", System.Windows.Forms.ToolTipIcon.Error);
                //Hide console window
                ConsoleHelper.ShowWindow(handle, ConsoleHelper.SW_HIDE);
            }
        }
        Console.Clear();
        Console.BackgroundColor = ConsoleColor.Gray;
        Console.ForegroundColor = ConsoleColor.Black;
        Console.WriteLine("====CONSOLE OPENED ON (COM PORT " + portnumber.ToString() + ")=======");
        Console.WriteLine("===========BAUD RATE: " + Form1.BaudRate.ToString() + "=============");
        Console.BackgroundColor = ConsoleColor.Black;
        Console.ForegroundColor = ConsoleColor.White;
    }

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2012-12-11
    • 1970-01-01
    • 1970-01-01
    • 2021-09-08
    • 1970-01-01
    • 2014-11-14
    • 1970-01-01
    相关资源
    最近更新 更多