我想在我的 Windows 窗体应用程序中打开进程。
例如,我希望当用户按下其中一个窗口窗体容器中的按钮时,mstsc.exe 将打开。
如果他按下按钮,它将在另一个容器上打开 IE,
[DllImport("user32.dll", SetLastError = true)]
static extern IntPtr SetParent(IntPtr hWndChild, IntPtr hWndNewParent);
private void button1_Click(object sender, EventArgs e)
{
Process p = Process.Start("mstsc.exe", @"c:\VPN4.rdp");
Thread.Sleep(3000);
p.StartInfo.CreateNoWindow = true; // new
SetParent(p.MainWindowHandle, this.panel1.Handle);
p.StartInfo.WindowStyle = ProcessWindowStyle.Maximized; //new
}
它在windows窗体中打开了进程但是没有,,,
您可以启动另一个进程,并将它的窗口放在您自己的窗口中。
尝试使用表单句柄而不是面板。这是一个简短的例子
private void toolStripButton1_Click(object sender, EventArgs e)
{
ProcessStartInfo info = new ProcessStartInfo();
info.FileName = "notepad";
info.UseShellExecute = true;
var process = Process.Start(info);
Thread.Sleep(2000);
SetParent(process.MainWindowHandle, this.Handle);
}
至于原始问题的具体问题,我猜是由于远程桌面打开其他窗口让应用程序完成它的工作,而 MainWindowHandle 不是你想要的。
尝试使用 Windows API 中的 FindWindowEx 来搜索正确的窗口。如果您按进程 ID 和标题搜索,您应该会找到正确的。 Stackoverflow 和 google 都有大量的资料。只需搜索“C# FindWindowEx”
我是一名优秀的程序员,十分优秀!