码迷,mamicode.com
首页 > Windows程序 > 详细

WPF编程,获取句柄将外部程序嵌入到WPF界面。

时间:2019-04-18 15:03:31      阅读:302      评论:0      收藏:0      [点我收藏+]

标签:sem   zed   hpa   显示   info   link   sys   href   details   

原文:WPF编程,获取句柄将外部程序嵌入到WPF界面。

版权声明:我不生产代码,我只是代码的搬运工。 https://blog.csdn.net/qq_43307934/article/details/86648114

1、增加引用

技术图片

2、增加命名空间

? ? ? ? xmlns:wfh="clr-namespace:System.Windows.Forms.Integration;assembly=WindowsFormsIntegration"
? ? ? ? xmlns:wfc="clr-namespace:System.Windows.Forms;assembly=System.Windows.Forms"

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);


        }

?

?

WPF编程,获取句柄将外部程序嵌入到WPF界面。

标签:sem   zed   hpa   显示   info   link   sys   href   details   

原文地址:https://www.cnblogs.com/lonelyxmas/p/10729342.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!