最近,我尝试在一个旧的项目上工作--我无法让setparent工作--它一直给我"InvalidOperationException“错误,下面是代码:
private void button1_Click(object sender, EventArgs e)
{
Process proc = Process.Start("calc.exe");
proc.WaitForInputIdle();
Thread.Sleep(500);
SetParent(proc.MainWindowHandle, this.Handle);
}它是用一个按钮调用的,当它试图设置父程序时,它会出错。我能在网上找到的一切都表明我的代码是正确的。
发布于 2016-06-30 05:05:35
下面的代码在我这边运行良好(请检查Windows函数SetParent的声明):
[DllImport("user32.dll", SetLastError = true)]
private static extern IntPtr SetParent(IntPtr hWndChild, IntPtr hWndNewParent);
private void button1_Click(object sender, EventArgs e)
{
Process proc = Process.Start("calc.exe");
proc.WaitForInputIdle();
Thread.Sleep(500);
SetParent(proc.MainWindowHandle, this.Handle);
}结果:

希望有帮助:)
https://stackoverflow.com/questions/38114163
复制相似问题