【发布时间】:2010-10-12 17:05:59
【问题描述】:
我想在 Winform 中嵌入一个控制台窗口。有没有办法做到这一点?
【问题讨论】:
我想在 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);
}
}
}
【讨论】:
哦!您希望控制台在窗口中。您可以编写自己的输入和管道输入到标准输出和标准输入。或者你可以嵌入 powershell 但没有控制。 – rerun 2010 年 10 月 12 日 19:49
【讨论】:
您基本上可以通过以下方式做到这一点:
您需要为此直接调用 API(您需要 SetParent 和 SetWindowPos)。这是一篇关于如何通过示例进行操作的文章:
http://www.geekpedia.com/tutorial230_Capturing-Applications-in-a-Form-with-API-Calls.html
【讨论】: