日期:2014-05-17  浏览次数:20983 次

C#窗体如何嵌套外部程序(cmd.exe)在窗体里显示
[b][/b]
如题:!!!

------解决方案--------------------
这个问题很多人提过了,目前为止没有一个好的解决方案!
------解决方案--------------------
帮顶 关注一下
------解决方案--------------------
关注,帮顶。
------解决方案--------------------
C# code

  [DllImport("user32.dll")]
       private static extern int SetParent(IntPtr hWndChild, IntPtr hWndParent);


              //设置被绑架程序的父窗口
               Process process = Process.Start("cmd.exe");
               IntPtr ParenthWnd= process.MainWindowHandle;
               SetParent(ParenthWnd, panel.Handle);

------解决方案--------------------
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.Diagnostics;

namespace Sinner_Cmd
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}

private void button1_Click(object sender, EventArgs e)
{
string run;
run = txt_input.Text.Trim();
//txt_OUT.Text = "";
Process cmd = new Process();//实例化
cmd.StartInfo.FileName = "cmd.exe";
cmd.StartInfo.RedirectStandardInput = true;
cmd.StartInfo.RedirectStandardOutput = true;
cmd.StartInfo.UseShellExecute = false;
cmd.StartInfo.CreateNoWindow = false;
cmd.Start();
cmd.StandardInput.WriteLine(run);
cmd.StandardInput.WriteLine("exit");
txt_input.Text = "";
string info = cmd.StandardOutput.ReadToEnd();
cmd.WaitForExit();
cmd.Close();
txt_OUT.AppendText(info);
}
}
}


献丑了。。。
------解决方案--------------------
C# code
        [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 button1_Click(object sender, EventArgs e)
        {
            Process p = new Process();
            p.StartInfo.FileName = "cmd.exe ";
            p.Start();

            System.Threading.Thread.Sleep(100);

            SetParent(p.MainWindowHandle, this.Handle);
            ShowWindow(p.MainWindowHandle, 3);    
        }

------解决方案--------------------
C# code
[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 button1_Click(object sender, EventArgs e)
{
    Process p = new Process();
    p.StartInfo.FileName = "cmd.exe ";
    p.StartInfo.WindowStyle = System.Diagnostics.ProcessWindowStyle.Minimized;//加上这句效果更好 
    p.Start();
    System.Threading.Thread.Sleep(100);//加上,100如果效果没有就继续加大

    SetParent(p.MainWindowHandle, this.Handle);
    ShowWindow(p.MainWindowHandle, 3);

}

------解决方案--------------------