gpt4 book ai didi

c# - 如何在 Windows 窗体中打开进程

转载 作者:太空宇宙 更新时间:2023-11-03 20:26:22 25 4
gpt4 key购买 nike

我想在我的 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”

关于c# - 如何在 Windows 窗体中打开进程,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/10434658/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com