Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Child Window Z-Order

Tags:

winapi

I see in MSDN, it says:

http://msdn.microsoft.com/en-us/library/windows/desktop/ms632680(v=vs.85).aspx

If the created window is a child window, its default position is at the bottom of the Z-order. If the created window is a top-level window, its default position is at the top of the Z-order (but beneath all topmost windows unless the created window is itself topmost).

However, another documentation says:

http://msdn.microsoft.com/en-us/library/windows/desktop/ms632599(v=vs.85).aspx

When an application creates a window, the system puts it at the top of the z-order for windows of the same type

I tested it like this:

btn1 = ::CreateWindow(L"button", L"OK", WS_TABSTOP|BS_DEFPUSHBUTTON|WS_VISIBLE|WS_CHILD
                    , 10, 10, 50, 30, hWnd, (HMENU)51, hInst, NULL);
btn2 = ::CreateWindow(L"button", L"Cancel", WS_TABSTOP|WS_CHILD|BS_PUSHBUTTON|WS_VISIBLE
                    , 20, 20, 70, 30, hWnd, (HMENU)52, hInst, NULL);

I created two buttons in a window and they overlapped, I can see that the button created later is covering the first button created.

Is the first statement in MSDN contradictory to my testing?

like image 603
IcyBrk Avatar asked Dec 20 '22 02:12

IcyBrk


2 Answers

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
like image 168
Hans Passant Avatar answered Mar 13 '23 01:03

Hans Passant


You should not rely to much on the how the child windows are displayed and which one is drawn last. If I run your sample code I get an OK button which is overlapped by the Cancel button. If I move the mouse over the buttons then the OK button comes into foreground and draws over the Cancel button.

I once had similar trouble with overlapping child controls. Then I found out that Microsoft says Overlapping Controls Are Not Supported by Windows.

BTW, if you really want to see the Z-order, then use GetTopWindow and GetNextWindow. Or the simpler way: run Microsoft Spy++.

like image 45
Werner Henze Avatar answered Mar 13 '23 01:03

Werner Henze