What's the difference between HWND_TOP and HWND_TOPMOST?

本文探讨了Windows操作系统中窗口Z-Order的概念及其与顶级(topmost)窗口的关系。介绍了HWND_TOP与HWND_TOPMOST参数在DeferWindowPos及SetWindowPos函数中的作用,并解释了这些参数如何影响窗口的堆叠顺序。

http://blogs.msdn.com/b/oldnewthing/archive/2005/11/21/495246.aspx


The special values HWND_TOP and HWND_TOPMOST have similar names but do completely different things when passed as the hWndInsertAfter parameter to the DeferWindowPos function (or its moral equivalents such as SetWindowPos). As a backgrounder, you should start off by reading the MSDN discussion, which is perfectly accurate as far as it goes. Here, I'll discuss the issue from a historical perspective in the hopes that looking at it from a different direction may improve understanding.

Sibling windows are maintained in an order called the Z-order. (For the purpose of this discussion, top-level windows are also treated as siblings. In fact, it is the Z-order of top-level windows that most people think of when they say "Z-order".)

The Z-order should be visualized as a vertical stack, with windows "above" or "below" siblings.

Before Windows 3.0, the behavior was simple: HWND_TOP brings the window to the top of the Z-order.

Windows 3.0 added the concept of "topmost" windows. These are top-level windows that always remain "above" non-topmost windows. To make a window topmost, call DeferWindowPos(or one of its moral equivalents) with HWND_TOPMOST as the hWndInsertAfter. To make a window non-topmost, use HWND_NOTOPMOST.

As a result of the introduction of "topmost" windows, HWND_TOP now brings the window "as high in the Z-order as possible without violating the rule that topmost windows always appear above non-topmost windows". What does this mean in practice?

  • If a window is topmost, then HWND_TOP puts it at the very top of the Z-order.
  • If a window is not topmost, then HWND_TOP puts it at the top of all non-topmost windows (i.e., just below the lowest topmost window, if any).

Note: The above discussion completely ignores the issue of owner and owned windows. I left them out because they would add a layer of complication that distracts from the main topic.




在 Windows 编程中,`HWND_TOPMOST` 和 `HWND_TOP` 是用于控制窗口 Z 顺序(堆叠顺序)的两个常量,它们在行为和用途上有显著区别。 `HWND_TOP` 表示将窗口放置在其 Z 顺序中的顶部,但不会超越其他标记为“最顶层”的窗口。这意味着即使该窗口被置顶,它仍会被 `HWND_TOPMOST` 窗口覆盖。这种行为适用于希望窗口在普通窗口之上,但不影响最顶层窗口优先级的场景[^1]。 相比之下,`HWND_TOPMOST` 会将窗口设置为始终位于所有非最顶层窗口之上,甚至包括那些使用 `HWND_TOP` 置顶的窗口。该行为通过将窗口标记为“最顶层”,使其在系统中具有较高的 Z 顺序优先级。如果多个窗口都被设置为 `HWND_TOPMOST`,它们之间的堆叠顺序则由其他因素决定,例如最近激活的窗口通常会位于最上方[^1]。 以下是一个使用 `SetWindowPos` 函数将窗口分别设置为 `HWND_TOP` 和 `HWND_TOPMOST` 的示例: ```cpp #include <windows.h> void SetWindowToTop(HWND hWnd) { SetWindowPos(hWnd, HWND_TOP, 0, 0, 0, 0, SWP_NOMOVE | SWP_NOSIZE); } void SetWindowToTopMost(HWND hWnd) { SetWindowPos(hWnd, HWND_TOPMOST, 0, 0, 0, 0, SWP_NOMOVE | SWP_NOSIZE); } int main() { HWND hWnd = FindWindow(NULL, L"Example Window Title"); if (hWnd != NULL) { // 设置为普通顶层 SetWindowToTop(hWnd); // 设置为最顶层 SetWindowToTopMost(hWnd); } return 0; } ``` 若希望取消最顶层状态,可使用 `HWND_NOTOPMOST` 常量再次调用 `SetWindowPos`,以恢复窗口的普通 Z 顺序行为[^1]。 ###
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值