欢迎来到Introzo百科
Introzo百科
C#中如何以winform形式嵌入cmd命令窗口
-->
解决方案1:
自己放一个文本框,改成黑色,然后输入命令。执行时,你使用Process.Start cmd。此时不会显示CMD窗口。然后,取出CMD的返回值,设置回文本框。
如何使用该方法实时获取cmd返回的数据,简单实现如下
private void OutPutForm_Shown(对象发送者, EventArgs e)
{
Control.CheckForIllegalCrossThreadCalls = false;
进程 = new Process();
p.StartInfo.FileName = "cmd.exe";
p.StartInfo.UseShellExecute = false; //是否使用操作系统shell启动
p.StartInfo.RedirectStandardInput = true;//接受调用程序的输入信息
p.StartInfo.RedirectStandardOutput = true;//获取调用程序的输出信息
p.StartInfo.RedirectStandardError = true;//重定向标准错误输出
p.StartInfo.CreateNoWindow = true;//不显示程序窗口
process.OutputDataReceived += new DataReceivedEventHandler(OutputHandler);
process.Start();//启动程序
process.BeginOutputReadLine();
}
私有无效OutputHandler(对象发送进程,DataReceivedEventArgs outLine)
{
if (!String.IsNullOrEmpty(www.introzo.com))
{
StringBuilder sb = new StringBuilder(this.textBox1.Text);
this.textBox1.Text = sb.AppendLine(www.introzo.com).ToString();
www.introzo.comionStart =this.textBox1.Text.Length;
this.textBox1.ScrollToCaret();
}
}
{
Control.CheckForIllegalCrossThreadCalls = false;
进程 = new Process();
p.StartInfo.FileName = "cmd.exe";
p.StartInfo.UseShellExecute = false; //是否使用操作系统shell启动
p.StartInfo.RedirectStandardInput = true;//接受调用程序的输入信息
p.StartInfo.RedirectStandardOutput = true;//获取调用程序的输出信息
p.StartInfo.RedirectStandardError = true;//重定向标准错误输出
p.StartInfo.CreateNoWindow = true;//不显示程序窗口
process.OutputDataReceived += new DataReceivedEventHandler(OutputHandler);
process.Start();//启动程序
process.BeginOutputReadLine();
}
私有无效OutputHandler(对象发送进程,DataReceivedEventArgs outLine)
{
if (!String.IsNullOrEmpty(www.introzo.com))
{
StringBuilder sb = new StringBuilder(this.textBox1.Text);
this.textBox1.Text = sb.AppendLine(www.introzo.com).ToString();
www.introzo.comionStart =this.textBox1.Text.Length;
this.textBox1.ScrollToCaret();
}
}
解决方法二:
直接上代码
[DllImport("User32.dll", EntryPoint = "SetParent")]
private static extern IntPtr SetParent(IntPtr hWndChild, IntPtr hWndNewParent);
[DllImport("user32.dll", EntryPoint = "ShowWindow")]
public static extern int ShowWindow(IntPtr hwnd, int nCmdShow);
private void button3_Click(对象发送者,EventArgs e)
{
进程 p = new Process();
p.StartInfo.FileName = "cmd.exe";
p.StartInfo.WindowStyle = System.Diagnostics.ProcessWindowStyle.Minimized;//添加这句话效果更好
p.Start();
System.Threading.Thread.Sleep();//添加,100,如果效果不好,继续增加
SetParent(p.MainWindowHandle, panel1.Handle); //panel1.Handle是显示外部程序的容器
ShowWindow(p.MainWindowHandle, );
}
private static extern IntPtr SetParent(IntPtr hWndChild, IntPtr hWndNewParent);
[DllImport("user32.dll", EntryPoint = "ShowWindow")]
public static extern int ShowWindow(IntPtr hwnd, int nCmdShow);
private void button3_Click(对象发送者,EventArgs e)
{
进程 p = new Process();
p.StartInfo.FileName = "cmd.exe";
p.StartInfo.WindowStyle = System.Diagnostics.ProcessWindowStyle.Minimized;//添加这句话效果更好
p.Start();
System.Threading.Thread.Sleep();//添加,100,如果效果不好,继续增加
SetParent(p.MainWindowHandle, panel1.Handle); //panel1.Handle是显示外部程序的容器
ShowWindow(p.MainWindowHandle, );
}
注意:记得引用 using System.Runtime.InteropServices;
-->
相关文章
- 10-07 Java多线程系列
- 10-07 Java多线程系列
- 10-07 Java多线程系列
- 10-07 对应tensorflow+keras+python
- 10-07 AD层切换操作技巧分享及注意事项
- 10-07 “需要Ninja加载C++扩展”解决方案
- 10-07 未来!微软推送Windows 11 Build 2
- 10-07 Windows 11 最新预览版来了!我们来看看更
- 10-07 Windows 11/Windows 10更新更过
- 10-07 模块七:微服务监控报警Prometheus架构与实
- 10-07 实际使用Prometheus搭建监控系统
- 10-07 golang prometheus包的使用
- 10-07 让微信如虎添翼的神器!这即将起飞!
- 10-07 梦回Windows XP,微软Win10/Win1
- 10-07 微软Win8多个早期概念设计曝光:全新开始屏幕、任
- 10-07 Excel单元格内容不完整?教你如何一键完成
- 10-07 为什么有HTTP协议,还有websocket协议
- 10-07 由于核心隔离(VBS),微软Win11最新更新将禁
- 10-07 在线文档服务商SurDoc获得IDG 400万美元
- 10-07 多玩人人等网站用户数据泄露可能会造成连锁反应
- 最近发表