C# 将外部exe程序 嵌入到自己的窗体界面

将别人开发的exe程序,放到自己的窗体里面来运行。

1.基本功能实现

    首先,在自己的窗体后面加上代码:

        [DllImport("User32.dll", EntryPoint = "SetParent")]
        private static extern IntPtr SetParent(IntPtr hWndChild, IntPtr hWndNewParent);

        [DllImport("user32.dll", EntryPoint = "ShowWindow")]
        private static extern int ShowWindow(IntPtr hwnd, int nCmdShow);

    然后在需要的地方,加上代码:

            string fexePath = @"XXX\Files\Debug\VsTest.exe"; // 外部exe位置

            Process p = new Process();
            p.StartInfo.FileName = fexePath;
            p.StartInfo.WindowStyle = ProcessWindowStyle.Normal;
            p.Start();

            while (p.MainWindowHandle.ToInt32() == 0)
            {
                System.Threading.Thread.Sleep(100);
            }
            SetParent(p.MainWindowHandle, this.Handle);
            ShowWindow(p.MainWindowHandle, (int)ProcessWindowStyle.Maximized);

 即可:

 

 

 

 

【http://www.cnblogs.com/CUIT-DX037/】

posted on 2017-07-14 15:31  云梦鸿  阅读(5994)  评论(1编辑  收藏  举报