exe程序嵌入Winform窗体

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

private void Form1_Load(object sender, EventArgs e)
        {
            string fexePath = @"C:\Users\ghostwinter\AppData\Local\Google\Chrome\Application\chrome.exe"; // 外部exe位置

            Process p = new Process();
            p.StartInfo.FileName = fexePath;
            p.StartInfo.WindowStyle = ProcessWindowStyle.Normal;
            //p.StartInfo.Arguments = "www.baidu.com";
            //p.StartInfo.WorkingDirectory = @"C:\Users\ghostwinter\Desktop\MyIE\";
            p.Start();

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

Leave a Reply