Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How does SetWindowPos affect the owner window's Z order?

Tags:

windows

winapi

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?

like image 447
user253751 Avatar asked Jan 25 '23 22:01

user253751


2 Answers

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.

enter image description here

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.

enter image description here

like image 181
Rita Han Avatar answered Jan 27 '23 13:01

Rita Han


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):

    • The targeted window is an owner, or
    • The targeted window is owned and is active, or
    • The call attempts to put an owned window behind its owner
like image 40
user253751 Avatar answered Jan 27 '23 13:01

user253751