Get handle of top window (Sort windows by Z index)

北战南征 提交于 2019-12-23 05:00:25

问题


I am trying to write a method which takes List of window handles and returns handle of the window which has highest z index among others. But in vain. Can anybody give me a suggestion how to do that?


回答1:


I'll help you out:

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

enum GetWindow_Cmd : uint
{
    GW_HWNDFIRST = 0,
    GW_HWNDLAST = 1,
    GW_HWNDNEXT = 2,
    GW_HWNDPREV = 3,
    GW_OWNER = 4,
    GW_CHILD = 5,
    GW_ENABLEDPOPUP = 6
}

private IntPtr GetTopmostHwnd(List<IntPtr> hwnds)
{
    var topmostHwnd = IntPtr.Zero;

    if (hwnds != null && hwnds.Count > 0)
    {
        var hwnd = hwnds[0];

        while (hwnd != IntPtr.Zero)
        {
            if (hwnds.Contains(hwnd))
            {
                topmostHwnd = hwnd;
            }

            hwnd = GetWindow(hwnd, GetWindow_Cmd.GW_HWNDPREV);
        }
    }

    return topmostHwnd;
}


来源:https://stackoverflow.com/questions/27066612/get-handle-of-top-window-sort-windows-by-z-index

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!