运行 C# 程序中的另一个应用程序和他的孩子

run another application within a C# program with his childs

我使用示例代码从特定列表

中搜索我的 windows 表单应用程序
void processStartEvent_EventArrived(object sender, EventArrivedEventArgs e)
{
    string processName = e.NewEvent.Properties["ProcessName"].Value.ToString();
    int processID = Convert.ToInt32(e.NewEvent.Properties["ProcessID"].Value);

    if (_processNames.Contains(processName))
    {
        Process proc = Process.GetProcessById(processID);
        if (GlobalVar.SourceWinForm.InvokeRequired)
        {

            GlobalVar.SourceWinForm.Invoke(new MethodInvoker(delegate { ProcessHandler.SetParent(proc.MainWindowHandle, GlobalVar.SourceWinForm.Handle); }));
        }
        else
        {
            ProcessHandler.SetParent(proc.MainWindowHandle, GlobalVar.SourceWinForm.Handle);
        }
    }

}

如你所见,我使用了函数:

[DllImport("user32.dll")]
public static extern IntPtr SetParent(IntPtr hwc, IntPtr hwp);

除一件事外,一切都很好。 例如,我将记事本应用程序搜索到我的应用程序中。 所以它真的给了我记事本到我的应用程序 window 但问题开始于 例如,我在记事本 "Format -> Font" 中按它打开一个记事本的新子 window,这个子 window,我的应用程序不是这个子 window.[=13 的父亲=]

我怎样才能找到完整的过程?包括他的 child(潜艇)windows ?

解决方法: 首先我想说评论是正确的,可以帮助别人所以请先阅读我的主要问题的评论。

但是如果你有特定的场景并且你没有任何其他选择并且你必须使用 SetParent 或其他类似的功能。 (确保您首先阅读了有关 SetParent 的内容并了解它的作用)

解决方案是将整个表格置于前台:

bool SetForegroundWindow(IntPtr hWnd);

这使得从您加载的主窗体继承的所有其他窗体都可以在您的主应用程序中看到它(以防您的应用程序阻塞和拉伸整个屏幕)

您可以使用:

    [DllImport("user32.dll")]
    private static extern
        bool SetForegroundWindow(IntPtr hWnd);
    [DllImport("user32.dll")]
    private static extern
        bool ShowWindowAsync(IntPtr hWnd, int nCmdShow);
    [DllImport("user32.dll")]
    private static extern
        bool IsIconic(IntPtr hWnd);

然后是

            // bring it to the foreground
            if (IsIconic(proc.MainWindowHandle))
                ShowWindowAsync(proc.MainWindowHandle, SW_RESTORE);
            SetForegroundWindow(proc.MainWindowHandle);