Note that there are some explanatory texts on larger screens.

plurals
  1. POWindows 7: how to bring a window to the front no matter what other window has focus?
    primarykey
    data
    text
    <p>I'm implementing a task-bar replacement, dock-like application-switcher style program. It's doing some unique stuff with OpenGL, and with keyboard shortcuts, so the way it's set up, the window doesn't always have focus. I'd like to implement it such that I can bring an arbitrary window to the foreground, much like a taskbar or an ALT-TAB program would. </p> <p>However, my code simply causes the application icon to flash in the taskbar. The Windows API documentation says that this is what is supposed to happen, but I'm looking for a way to work around this.</p> <p>I've adapted my code from the following examples, which say that attaching to the foreground thread should allow you to set the foreground window. Here are the sites:</p> <p><a href="http://www.voidnish.com/Articles/ShowArticle.aspx?code=dlgboxtricks" rel="noreferrer">http://www.voidnish.com/Articles/ShowArticle.aspx?code=dlgboxtricks</a></p> <p><a href="http://invers2008.blogspot.com/2008/10/mfc-how-to-steal-focus-on-2kxp.html" rel="noreferrer">http://invers2008.blogspot.com/2008/10/mfc-how-to-steal-focus-on-2kxp.html</a></p> <p>My code looks like this. Note that it's using the win32 wrappers for python (self.hwnd is the handle of the window I want to bring to the front):</p> <pre><code>fgwin = win32gui.GetForegroundWindow() fg = win32process.GetWindowThreadProcessId(fgwin)[0] current = win32api.GetCurrentThreadId() if current != fg: win32process.AttachThreadInput(fg, current, True) win32gui.SetForegroundWindow(self.hwnd) win32process.AttachThreadInput(fg, win32api.GetCurrentThreadId(), False) </code></pre> <p>However, unless my window is the foreground window (which it isn't usually), this just causes the program's icon to flash.</p> <p>Am I doing the thread attaching wrong? Is there another way to work around this? I figure there must be, as there are lots of application switchers out there that seem to be able to do this just fine.</p> <p>I'm writing this in python, but if there is a solution in another language I will use wrappers or do whatever is necessarry to get this up and running.</p> <p>Thanks in advance!</p> <p><strong>EDIT:</strong> I'd be open to a way to make it work only on my particular computer, i.e. a way to enable, on my machine, a way for any application to take focus.</p> <p><strong>SOLVED:</strong> What you have to do is disable the foreground lock. Turns out it was as easy as this:</p> <pre><code>win32gui.SystemParametersInfo(win32con.SPI_SETFOREGROUNDLOCKTIMEOUT, 0, win32con.SPIF_SENDWININICHANGE | win32con.SPIF_UPDATEINIFILE) </code></pre>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload