如何在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 重播
你基本上可以这样做:
- 创建cmd进程
- 将该进程的父级设置为表单(例如某些面板)
- 插入事件以在需要时resize
- 当主进程不再需要cmd进程时,终止进程。
您需要直接为此调用API(您需要SetParent和SetWindowPos)。 这是一篇关于如何使用示例执行此操作的文章:
https://www.geekpedia.com/tutorial230_Capturing-Applications-in-a-Form-with-API-Calls.html
上述就是C#学习教程:如何在Winforms中包含控制台?分享的全部内容,如果对大家有所用处且需要了解更多关于C#学习教程,希望大家多多关注—计算机技术网(www.ctvol.com)!
本文来自网络收集,不代表计算机技术网立场,如涉及侵权请联系管理员删除。
ctvol管理联系方式QQ:251552304
本文章地址:https://www.ctvol.com/cdevelopment/1003941.html