September 19th, 2024

My window has the WS_EX_NOACTIVATE extended style, but it got activated anyway

If you set the WS_EX_NOACTIVATE extended window style, Windows won’t activate the window when it is clicked on, Windows will skip the window when looking for a new window to activate when the active window is destroyed, and the window won’t show up in Alt+Tab or the taskbar by default. You can still activate it programmatically, usually by calling Set­Active­Window or Set­Foreground­Window.

If you override the default and make the window appear in Alt+Tab and the taskbar, then those UI surfaces will call Set­Foreground­Window when the window is selected, so that’s one way an end-user can activate a no-activate window.

But there’s one oft-overlooked way the user can activate a no-activate window: Use the “Activate a window by hovering over it with the mouse” setting (known programmatically as “active window tracking”), and hover over the window.

When this happens, the “active window tracking” code doesn’t check the WS_EX_NOACTIVATE style.¹ It goes directly to sending the window a WM_MOUSE­ACTIVATE message to ask whether the window should be activated, and Def­Window­Proc will say “Sure, activate me!”

If you want to prevent the window from being activated by mouse hover, you need to handle the WM_MOUSE­ACTIVATE message and return MA_NO­ACTIVATE or MA_NO­ACTIVATE­AND­EAT, depending on what you want to happen to the mouse event.

¹ This is arguably an oversight in the activate window tracking feature code when it was originally written.

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.