版权声明:我不生产代码,我只是代码的搬运工。https://blog.csdn.net/qq_43307934/article/details/86648114
1、增加引用
2、增加命名空间
3、前台
<Grid Grid.Row=" 1" Name="ddddd">
<WindowsFormsHost x:Name="form1"
HorizontalAlignment="Stretch"
VerticalAlignment="Stretch"
>
<wfc:FlowLayoutPanel x:Name="flowLayoutPanel1">
</wfc:FlowLayoutPanel>
</WindowsFormsHost>
</Grid>
4、后台
先增加如下:
[DllImport("User32.dll ", SetLastError = true, EntryPoint = "SetParent")]
private static extern IntPtr SetParent(IntPtr hWndChild, IntPtr hWndNewParent);
[DllImport("user32.dll", EntryPoint = "SendMessage", SetLastError = true, CharSet = CharSet.Auto)]
private static extern int SendMessage(IntPtr hwnd, uint wMsg, int wParam, int lParam); //对外部软件窗口发送一些消息(如 窗口最大化、最小化等)
[DllImport("user32.dll ", EntryPoint = "ShowWindow")]
public static extern int ShowWindow(IntPtr hwnd, int nCmdShow);
在事件中增加代码 ,比如在按钮事件中:
private void Button_Click_1(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(200);//加上,100如果效果没有就继续加大
// IntPtr hwnd = new WindowInteropHelper(this).Handle;
// IntPtr hwnd2 = ((HwndSource)PresentationSource.FromVisual(ddddd)).Handle;
// IntPtr hPlayWnd = flowLayoutPanel1.Handle;
System.Windows.Forms.PictureBox pp = new System.Windows.Forms.PictureBox
{
Width = 500,
Height = 400,
Bounds = new System.Drawing.Rectangle(0, 0, 500, 400),
Name = "PictureBox"
};
form1.Child = pp;
IntPtr hpanel1 = pp.Handle;
SetParent(p.MainWindowHandle, hpanel1); //panel1.Handle为要显示外部程序的容器
ShowWindow(p.MainWindowHandle, 3);
}