How to Get the Z-Order in Windows

How to get the z-order in windows?

You can use the GetTopWindow function to search all child windows of a parent window and return a handle to the child window that is highest in z-order. The GetNextWindow function retrieves a handle to the next or previous window in z-order.

GetTopWindow: http://msdn.microsoft.com/en-us/library/ms633514(VS.85).aspx

GetNextWindow: http://msdn.microsoft.com/en-us/library/ms633509(VS.85).aspx

Child Window Z-Order

The documentation is accurate. You are being tripped up by another problem, you allow the child windows to draw themselves across other child windows. So now the painting order matters.

You fix that by adding the WS_CLIPSIBLINGS style flag to your CreateWindowEx call. You'll now see that the OK button is on top. Fix:

btn1 = ::CreateWindow(L"button", L"OK", 
WS_TABSTOP|BS_DEFPUSHBUTTON|WS_VISIBLE|WS_CHILD|WS_CLIPSIBLINGS,
10, 10, 50, 30, hWnd, (HMENU)51, hInst, NULL);
// etc, use it as well on other child windows

How to get the second active window in z order?

Call GetWindow passing GW_HWNDNEXT.

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

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


Related Topics



Leave a reply



Submit