[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); } } } 献丑了
[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); }
[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);}
然后使用Windows api“ SetParent”
放在指定得容器上
[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.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);
}
}
}
献丑了
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);
}
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);}
这位仁兄的代码可以实现内嵌cmd功能