SetActiveWindow()
and
BringWindowToTop()
only changes the Z-order of your windows and sets the "active" window
within the calling application. It does
not bring your application to the foreground, if another application is currently in the foreground!
SetForegroundWindow()
is used to actually bring a window to the foreground. This can be a window of the calling application, or a window belonging to another application.
But: The call to
SetForegroundWindow()
will only succeed, if the
current foreground window belongs to the calling application, or if the calling application was created by the application owning the
current foreground window.
The actual rules are a bit more complex:
https://learn.microsoft.com/en-us/windows/win32/api/winuser/nf-winuser-setforegroundwindow
In other words: Stealing the focus from the current "foreground" application by calling
SetForegroundWindow()
from a "background" application is
not possible – in "modern" versions of Windows. And that is for good reasons 😏
BTW: You can use
SetWindowLongW()
with
GWL_EXSTYLE
and
WS_EX_TOPMOST
to enable the "topmost" style for your window, so that it will appear on top of all other windows, regardless of whether your application is currently active or not...