如何在Winforms中包含控制台?

Arl*_*ler 7 c# console winforms

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

rer*_*run 8

您需要做的就是调用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);
        }
    }
}
Run Code Online (Sandbox Code Playgroud)

  • "我希望在Winform中有一个嵌入式控制台窗口" - 非常清楚. (3认同)

Arl*_*ler 3

\n

哦!您希望控制台位于窗口中。您可以编写自己的内容,并通过管道将输入传入和传出 stdout 和 stdin。或者你可以嵌入powershell,但没有内置控件。\xe2\x80\x93于 10 年 10 月 12 日 19:49重新运行

\n
\n