Run program and make window active

How do I run a program with win32 context as active? Currently it is always shown as inactive.

I already used SetFocus(handle) and still inactive.
Last edited on
are you asking about always-on-top type behavior? or something else?
What are you trying to accomplish? There are several WIN32 APIs:
BringTofront()
Activate()
SetActiveWindow()
SetForegroundWindow()
BringWindowToTop()
SetWindowPos()
Last edited on
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...
Last edited on
Thanks guys. I think this has something to do with parenting a child window to a main window which is sort of a hack or maybe there's a right way to do it?

I tried again without the child window and the window is active and focus when starting as it should be.
Last edited on
Topic archived. No new replies allowed.