The SetWindowPos
function accepts the following flag:
SWP_NOOWNERZORDER (0x0200)
Does not change the owner window's position in the Z order.
What does the SetWindowPos
function do to the owner window's position in the Z order, if you do not pass this flag?
What does the SetWindowPos function do to the owner window's position in the Z order, if you do not pass this flag?
Take placing the owned window at the bottom of the Z order for an example. There are three windows: Owned, TestWindowPos (owner) and New Tab Chrome window (as a reference).
Test code piece:
HWND hWnd = CreateWindowW(szWindowClass, szTitle, WS_OVERLAPPEDWINDOW,
CW_USEDEFAULT, 0, CW_USEDEFAULT, 0, nullptr, nullptr, hInstance, nullptr);
ShowWindow(hWnd, nCmdShow);
UpdateWindow(hWnd);
HWND hWndOwned = CreateWindowW(szWindowClass, L"Owned", WS_OVERLAPPEDWINDOW,
0, 0, 500, 500, hWnd, nullptr, hInstance, nullptr);
ShowWindow(hWndOwned, nCmdShow);
UpdateWindow(hWndOwned);
SetWindowPos(hWndOwned, HWND_BOTTOM, 0,0,0,0, SWP_NOMOVE | SWP_NOSIZE | SWP_NOOWNERZORDER);
When SWP_NOOWNERZORDER
flag is set you can see from the following snapshot that it puts the owned window at bottom but keep the owner unchanged.
When SWP_NOOWNERZORDER
flag is not set you can see from the following snapshot that it changes the owned window's z-order together with the owner window's.
I wrote a test program which tries all SetWindowPos operations in each possible starting order of 4 windows (owner, 2 owned windows, and an unrelated window). I found the following:
Windows prefers to put owned windows directly in front of their owner, without any other windows between them.
When SWP_NOACTIVATE
and SWP_NOOWNERZORDER
are used together, you can put windows in any order you like. The previous "rule" is not enforced, ever.
When SWP_NOACTIVATE
is not used, the targeted window becomes active. The targeted window is always moved to the front of the Z-order, as the documentation states in "Remarks" - hWndInsertAfter
is ignored except for special values.
If the targeted window is an owned window or an owner, the preferred Z-order is enforced for that owner and all its owned windows. SWP_NOOWNERZORDER
is ignored.
When SWP_NOACTIVATE
is used and SWP_NOOWNERZORDER
is not used, and the targeted window is an owned window or an owner, Windows may decide to enforce the preferred order for that owner and all its owned windows. The criteria for this decision seem to be as follows (though I would not rely on them):
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With