如何在Winforms中包含控制台?

我想在Winform中嵌入一个控制台窗口。 有没有办法做到这一点?

你需要做的就是调用windows API函数AllocConsloe然后使用普通的控制台类,这里是表单代码

using System; using System.Collections.Generic; using System.ComponentModel; using System.Data; using System.Drawing; using System.Linq; using System.Text; using System.Windows.Forms; using System.Runtime.InteropServices; namespace waTest { public partial class Form1 : Form { [DllImport("Kernel32.dll")] static extern Boolean AllocConsole( ); public Form1( ) { InitializeComponent(); } private void Form1_Load( object sender, EventArgs e ) { if ( !AllocConsole() ) MessageBox.Show("Failed"); Console.WriteLine("test"); string input = Console.ReadLine(); MessageBox.Show(input); } } } 

哦! 你想要窗口中的控制台。 您可以在stdout和stdin中编写自己的和管道输入。 或者你可以嵌入powershell,但没有控制权。 – 于2010年10月12日19:49 重播

你基本上可以这样做:

  1. 创建cmd进程
  2. 将该进程的父级设置为表单(例如某些面板)
  3. 插入事件以在需要时resize
  4. 当主进程不再需要cmd进程时,终止进程。

您需要直接为此调用API(您需要SetParent和SetWindowPos)。 这是一篇关于如何使用示例执行此操作的文章:

http://www.geekpedia.com/tutorial230_Capturing-Applications-in-a-Form-with-API-Calls.html