DEV Community

David Rickard
David Rickard

Posted on

AllowSetForegroundWindow quirks

At some point when developing for Windows you may have seen your app try to get focus for itself but end up flashing the taskbar icon instead. As Raymond Chen explains, you can't steal foreground focus.

I've come across a couple of additional quirks with AllowSetForegroundInfo:

Issues when launched from Windows notifications

I discussed this problem in my post on Electron notifications. As I mentioned there, there's a wrinkle for activations from Windows Notifications. When your app is launched from the protocol string in a notification, for some reason calling AllowSetForegroundWindow does not work. Chromium worked around this issue by sending a dummy keypress event.

I created a node package to execute the same workaround for Electron, and then also fixed this in Tauri's deep-link plugin.

Ideally Windows should come up with a fix and make sure that any process launched from a notification can call AllowSetForegroundInfo without having to jump through this hoop.

ASFW_ANY

But there is another oddity I came across. AllowSetForegroundWindow's documentation claims that you can pass in ASFW_ANY and it will allow any process to take foreground focus. When originally trying to implement this workaround, I used this because I wanted to see if it would work at all before switching to the more secure approach of passing in the specific process ID.

But it just did not work at all until I passed in the PID. No shortcuts here. 🫤

Top comments (0)