如何从另一个进程获取 child windows 而不是 child 控件?

How do I get child windows from another process and not child controls?

我的任务是寻找一种方法让另一个应用程序显示在其他应用程序之上 windows(始终在最前面)。我能够使用 RetrieveProcesses() 函数获取具有 Window 标题的进程。一旦用户选择了他们想要修改的进程,我的应用程序将调用 MakeProcessOnTop 或 MakeProcessNormal。这两个函数都会修改主应用程序的 window。在我添加修改它的 children 之前,这工作正常。

然后我发现这对 child windows 不起作用(就像 outlook 中的电子邮件)所以我开始寻找一种方法来处理 child windows.下面的代码这样写,最后会乱成childwindows。如何获取 child windows 而不是 child 控件的句柄指针?

public static class ProcessManagement
{
    [DllImport("user32.dll", SetLastError = true)]
    static extern bool SetWindowPos(IntPtr hWnd, IntPtr hWndInsertAfter, int X, int Y, int cx, int cy, SetWindowPosFlags uFlags);

    [DllImport("user32.dll", SetLastError = true)]
    static extern IntPtr SetFocus(IntPtr hWnd);

    [DllImport("user32.dll")]
    [return: MarshalAs(UnmanagedType.Bool)]
    static extern bool ShowWindow(IntPtr hWnd, ShowWindowCommands nCmdShow);

    [DllImport("user32.dll")]
    [return: MarshalAs(UnmanagedType.Bool)]
    public static extern bool EnumChildWindows(IntPtr hwndParent, EnumWindowsProc lpEnumFunc, IntPtr lParam);

    static readonly IntPtr HWND_TOPMOST = new IntPtr(-1);
    static readonly IntPtr HWND_NOTOPMOST = new IntPtr(-2);
    static readonly IntPtr HWND_TOP = new IntPtr(0);
    static readonly IntPtr HWND_BOTTOM = new IntPtr(1);

    public static IEnumerable<Process> RetrieveProcesses()
    {
        List<Process> returnList = new List<Process>();

        Process[] processArray = Process.GetProcesses();

        foreach (Process p in processArray)
        {
            if (!String.IsNullOrEmpty(p.MainWindowTitle))
            {
                returnList.Add(p);
            }
        }

        return returnList;
    }

    public static IntPtr GetProcessWindowHandle(int processId)
    {
        Process p = Process.GetProcessById(processId: processId);
        return p.MainWindowHandle;
    }

    public static List<IntPtr> GetProcessChildWindowHandles(IntPtr parent)
    {
        List<IntPtr> result = new List<IntPtr>();
        GCHandle listHandle = GCHandle.Alloc(result);
        try
        {
            EnumWindowsProc childProc = new EnumWindowsProc(EnumWindow);
            EnumChildWindows(parent, childProc, GCHandle.ToIntPtr(listHandle));
        }
        finally
        {
            if (listHandle.IsAllocated)
                listHandle.Free();
        }
        return result;
    }

    private static bool EnumWindow(IntPtr handle, IntPtr pointer)
    {
        GCHandle gch = GCHandle.FromIntPtr(pointer);
        List<IntPtr> list = gch.Target as List<IntPtr>;
        if (list == null)
        {
            throw new InvalidCastException("GCHandle Target could not be cast as List<IntPtr>");
        }
        list.Add(handle);
        //  You can modify this to check to see if you want to cancel the operation, then return a null here
        return true;
    }

    public static bool MakeProcessOnTop(IntPtr targetWindowHandle, bool targetChildren = true)
    {
        bool bReturn = true;

        if (!ShowWindow(targetWindowHandle, ShowWindowCommands.Minimize))
        {
            bReturn = false;
        }

        if (!ShowWindow(targetWindowHandle, ShowWindowCommands.Restore))
        {
            bReturn = false;
        }

        if (!ShowWindow(targetWindowHandle, ShowWindowCommands.ShowNoActivate))
        {
            bReturn = false;
        }

        if (!SetWindowPos(targetWindowHandle, HWND_TOPMOST, 0, 0, 0, 0, SetWindowPosFlags.SWP_NOACTIVATE | SetWindowPosFlags.SWP_NOMOVE | SetWindowPosFlags.SWP_NOSIZE))
        {
            bReturn = false;
        }

        if (targetChildren)
        {
            List<IntPtr> childProcesses = GetProcessChildWindowHandles(targetWindowHandle);

            foreach(IntPtr iPtr in childProcesses)
            {
                MakeProcessOnTop(iPtr, false);
            }
        }

        return bReturn;
    }
    public static bool MakeProcessNormal(IntPtr targetWindowHandle, bool targetChildren = true)
    {
        bool bReturn = true;

        if (!ShowWindow(targetWindowHandle, ShowWindowCommands.Minimize))
        {
            bReturn = false;
        }

        if (!ShowWindow(targetWindowHandle, ShowWindowCommands.Restore))
        {
            bReturn = false;
        }

        if (!ShowWindow(targetWindowHandle, ShowWindowCommands.ShowNoActivate))
        {
            bReturn = false;
        }

        if (!SetWindowPos(targetWindowHandle, HWND_NOTOPMOST, 0, 0, 0, 0, SetWindowPosFlags.SWP_NOACTIVATE | SetWindowPosFlags.SWP_NOMOVE | SetWindowPosFlags.SWP_NOSIZE))
        {
            bReturn = false;
        }

        if (targetChildren)
        {
            List<IntPtr> childProcesses = GetProcessChildWindowHandles(targetWindowHandle);

            foreach (IntPtr iPtr in childProcesses)
            {
                MakeProcessNormal(iPtr, false);
            }
        }

        return bReturn;
    }
}

Always On Top 仅对顶级 windows 或 MDI children.

有意义

您可以通过操纵 Z 顺序来提高 child window,但没有明确定义如何将其放回原位。