September 14th, 2017

Why is my window unexpectedly becoming topmost?

A customer had a problem where one of their program’s windows was somehow receiving the WS_EX_TOP­MOST extended window style, thereby becoming topmost. The scenario was that they created a popup window with the WS_EX_TOP­MOST extended style, and subsequently opened a document window. If they destroyed the popup window before creating the document window, then everything was fine. But if they created the document window before destroying the popup window, then their main app magically gained the WS_EX_TOP­MOST extended style. Their investigation revealed that nobody was calling Set­Window­Long with GWL_EXSTYLE and WS_EX_TOP­MOST.¹ Are there other ways that a window can become topmost?

One way that a window can become topmost is if it is created with the WS_EX_TOP­MOST extended style.

Another way that a window can become topmost is if you call Set­Window­Pos and pass HWND_TOP­MOST as the hwnd­Insert­After.

Yet another way that a window can become topmost is if you pass a topmost window as the hwnd­Insert­After.

Armed with this information, the customer did some more investigation and reported back: They found a call to Set­Window­Pos that was making the window topmost.

Mystery solved!

¹ Not that anybody should be doing that anyway. The documentation for the WS_EX_TOP­MOST extended style says that you shouldn’t be manipulating the extended style bit directly. “To add or remove this style, use the Set­Window­Pos function.”

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.

0 comments

Discussion are closed.

Feedback