C# - get handle of window right beneath the active window?

Is it possible somehow to get the handle of the window below the current active window retrieved by GetForegroundWindow, z-order wise? In other words, the window that is beneath the current one, no matter its size and position.

Upvotes: 0

Views: 2109

Answers (2)

huysentruitw
huysentruitw

Reputation: 28091

You can get the next or previous window (z-order wise) with the GetNextWindow function.

EDIT: I just read on pinvoke.net that GetNextWindow is a macro of GetWindow. So you might as well call GetWindow directly:

Code from pinvoke.net:

[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
}

Upvotes: 0

Florian
Florian

Reputation: 5994

You may try this pinvoke calls:

[DllImport("User32")] extern IntPtr GetTopWindow(IntPtr hWnd); 
[DllImport("User32")] extern IntPtr GetNextWindow(IntPtr hWnd, uint wCmd); 

and use this as parameter

uint GW_HWNDNEXT = 2; 

So first get the top window (or yours). After that call GetNextWindow and for the result handle again and again,... so you will get all windows

Upvotes: 1

Related Questions