Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Need to bring application to foreground on Windows

Tags:

windows

qt

winapi

I've got two applications I'm developing using Qt on windows. I want the user to be able to press a button in one application which makes the other application come to the foreground. (The programs communicate using QLocalSocket and named pipes.)

Currently I'm using Qt's QWidget::activateWindow() which occasionally brings the application to the foreground, but most of the time it just highlights the program on the taskbar.

Can someone please tell me how to do this, preferably using Qt although failing that using the WIN32 API would be fine.


Unfortunately, I couldn't find a way to do this only with Qt. I solved it using Chris Becke's suggestion of calling SetForegroundWindow from the currently active application.

like image 333
Steve Ridout Avatar asked Dec 22 '22 14:12

Steve Ridout


1 Answers

Are you sure this is not a debugging issue? The deal is, if an application HAS the foreground, it is allowed to change the foreground.

Clicking a button on window A will give that windows thread foreground activation. If it calls SetForegroundWindow (or equivalent) on the other window, that window WILL be given the foreground.

If, on the other hand, it simply sends a message to the other app, which tries to SetForeground on itself, that will fail. AllowSetForegroundWindow is used in situations where a 'legacy' app needs to be given permission - by a foreground app - to take the foreground. Once again, AllowSet... only works if called from a thread that owns the current active foreground window.

like image 72
Chris Becke Avatar answered Dec 28 '22 08:12

Chris Becke