January 1st, 2020

Nasty gotcha: Positioning your window beneath a topmost window makes it topmost

There’s a nasty gotcha with the Defer­Window­Pos function, and therefore any functions built on top of it, such as Set­Window­Pos: If you specify a hwndInsertAfter, and the insert-after window is topmost, then the window being positioned will be placed immediately after that window.

So far so good.

The hidden gotcha is that the window will be placed in the same zone as the insert-after window, which is the topmost zone.

In other words, the window will be made topmost, even if it wasn’t topmost previously.

Which is probably not what you were expecting.

Topics
Code

Author

Raymond has been involved in the evolution of Windows for more than 30 years. In 2003, he began a Web site known as The Old New Thing which has grown in popularity far beyond his wildest imagination, a development which still gives him the heebie-jeebies. The Web site spawned a book, coincidentally also titled The Old New Thing (Addison Wesley 2007). He occasionally appears on the Windows Dev Docs Twitter account to tell stories which convey no useful information.

2 comments

Discussion is closed. Login to edit/delete existing comments.

  • Alex Martin

    I hear about these kinds of weird things often enough that I’m starting to feel like all software teams should have a dedicated person whose job is entirely nothing but to just tell them when they’re doing something the user (whether said user is a programmer or an end-user) is going to find unexpected or confusing.

    • Brian MacKay

      But, is it really unexpected? It's the behavior I would expect. Consider an airplane waiting to board, with two classes, "first" and "coach". If the gate agent says "go to the back of the first class line", it's not the same as "go to the front of the coach line" (particularly if more first class passengers arrive after you).

      f you are positioning your window immediately behind the topmost window, then...

      Read more