zoukankan      html  css  js  c++  java
  • C#中winform窗体如何嵌入cmd命令窗口

    解决方法一:

      自己放一个文本框,改成黑色,然后输入命令,执行时,你Process.Start cmd ,此时CMD窗口不显示,然后,将CMD的返回值,再取出来,设回文本框。

      如何用这种方法实时获取cmd返回的数据,简单实现如下

     1     private void OutPutForm_Shown(object sender, EventArgs e)
     2      {
     3          Control.CheckForIllegalCrossThreadCalls = false;
     4          process = new Process();
     5          p.StartInfo.FileName = "cmd.exe";
     6          p.StartInfo.UseShellExecute = false;    //是否使用操作系统shell启动
     7          p.StartInfo.RedirectStandardInput = true;//接受来自调用程序的输入信息
     8          p.StartInfo.RedirectStandardOutput = true;//由调用程序获取输出信息
     9          p.StartInfo.RedirectStandardError = true;//重定向标准错误输出
    10          p.StartInfo.CreateNoWindow = true;//不显示程序窗口
    11          process.OutputDataReceived += new DataReceivedEventHandler(OutputHandler);
    12          process.Start();//启动程序
    13          process.BeginOutputReadLine();
    14      }
    15      private void OutputHandler(object sendingProcess,DataReceivedEventArgs outLine)
    16      {
    17          if (!String.IsNullOrEmpty(outLine.Data))
    18          {
    19              StringBuilder sb = new StringBuilder(this.textBox1.Text);
    20              this.textBox1.Text = sb.AppendLine(outLine.Data).ToString();
    21              this.textBox1.SelectionStart =this.textBox1.Text.Length;
    22              this.textBox1.ScrollToCaret();
    23          }
    24      }

    解决方法二:

      直接上代码

     1    [DllImport("User32.dll ", EntryPoint = "SetParent")]
     2   private static extern IntPtr SetParent(IntPtr hWndChild, IntPtr hWndNewParent);
     3   [DllImport("user32.dll ", EntryPoint = "ShowWindow")]
     4   public static extern int ShowWindow(IntPtr hwnd, int nCmdShow);
     5   private void button3_Click(object sender, EventArgs e)
     6   {
     7     Process p = new Process();
     8       p.StartInfo.FileName = "cmd.exe ";
     9       p.StartInfo.WindowStyle = System.Diagnostics.ProcessWindowStyle.Minimized;//加上这句效果更好
    10       p.Start();
    11       System.Threading.Thread.Sleep(100);//加上,100如果效果没有就继续加大
    12   
    13       SetParent(p.MainWindowHandle, panel1.Handle); //panel1.Handle为要显示外部程序的容器
    14       ShowWindow(p.MainWindowHandle, 3);
    15   }

      备注:记得引用 using System.Runtime.InteropServices;

  • 相关阅读:
    NGUI 3.5课程(五岁以下儿童)button-图片切换
    跑openstack命令错误【You must provide a username via either -...】
    angular cors跨域资源共享设置 和formdata设定
    PHP 如何获取客户端ip地址
    JavaScript如何生成思维导图(mindmap)
    百度ueditor上传图片时如何设置默认宽高度
    英语发音规则---E字母常见的发音组合有哪些
    google搜索引擎爬虫爬网站原理
    legend2---开发日志10(ajax请求的方法是否同样会执行base控制器里面的方法)
    JS中如何判断对象是对象还是数组
  • 原文地址:https://www.cnblogs.com/xuliangxing/p/5993297.html
Copyright © 2011-2022 走看看