WPF编程,获取句柄将外部程序嵌入到WPF界面。
2021-06-07 17:06
标签:sem zed hpa 显示 info link sys href details
? ? ? ? xmlns:wfh="clr-namespace:System.Windows.Forms.Integration;assembly=WindowsFormsIntegration" 先增加如下: 在事件中增加代码?,比如在按钮事件中: ? ? WPF编程,获取句柄将外部程序嵌入到WPF界面。 标签:sem zed hpa 显示 info link sys href details 原文地址:https://www.cnblogs.com/lonelyxmas/p/10729342.html1、增加引用
2、增加命名空间
? ? ? ? xmlns:wfc="clr-namespace:System.Windows.Forms;assembly=System.Windows.Forms"3、前台
?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);
}