我有一个关于将已运行的表单设置为现有表单进程的问题。 我有这段代码:
[DllImport("user32.dll", SetLastError = true)]
private static extern IntPtr SetParent(IntPtr hWndChild, IntPtr hWndNewParent);
static Form form = null;
public static void ShowForm()
{
form = new Form();
form.Size = new Size(50, 50);
form.FormBorderStyle = FormBorderStyle.FixedSingle;
form.StartPosition = FormStartPosition.Manual;
form.Location = new Point(0, 0);
form.Text = "MY FORM NAME";
SetParent(form.Handle, Process.GetProcessesByName("An exists process's name")[0].MainWindowHandle);
Application.Run(form);
}
public static void Main(string[] args)
{
ShowForm();
}
当我输入以下代码时,它将新表单设置为存在的一个OK:
SetParent(form.Handle, Process.GetProcessesByName("An exists process name")[0].MainWindowHandle);
Application.Run(form);
但是当我改变他们的位置时,新形式仍然在我的屏幕的0,0之外:
Application.Run(form);
SetParent(form.Handle, Process.GetProcessesByName("An exists process name")[0].MainWindowHandle);
错误是无法访问已处置的对象,这意味着我的(表单)表单已经处理。 我尝试了GetForegroundWindow(),FindWindow(null,“MY FORM NAME”)和来自user32.dll的GetFocus()
[DllImport("user32.dll")]
private static extern IntPtr GetForegroundWindow();
[DllImport("user32.dll")]
static extern IntPtr GetFocus();
[DllImport("USER32.DLL", CharSet = CharSet.Unicode)]
public static extern IntPtr FindWindow(String lpClassName, String lpWindowName);
表单的instate.Handle喜欢这个
SetParent(GetForegroundWindow(), Process.GetProcessesByName("An exists process name")[0].MainWindowHandle);
但它仍然不起作用。
问题是:当我找到目标进程时,如果我的表单已经运行,我该怎么做?我将我的表单作为子进程设置为目标进程作为父进程? 感谢您阅读:)