How to switch to another application on Windows (using C ++, Qt)?

I would like to enable my GUI users (GI users?) To switch directly to a known friendly application, for example. using the keyboard shortcut. Ideally, my application requested OS / Windows to display the application by the name or title bar of the main "XYZ" window.

The manual action path will be ALT + TAB to open the Windows Task Switcher, and then find and go to the desired application icon in order to finally bring it to the forefront of the program’s active windows. Alternatively, navigation through the taskbar .

AutoHotkey has a WinActivate function that does what I want to achieve.

+4
source share
3 answers

The following code works here seamlessly on Windows 7:

#include <windows.h> [...] // find window handle using the window title HWND hWnd = ::FindWindow(NULL, L"Window Title"); if (hWnd) { // move to foreground ::SetForegroundWindow(hWnd); } 
+3
source

If the applications are really friendly, i.e. both are under one control, a simpler solution can use a communication socket or a shared library, which allows you to get another application.

It seems to be quite difficult to delay the call:

 QTimer::singleShot( 2000, this, SLOT( toForeground() ) ); 

in this slot:

 void MainWindow::toForeground() { qDebug() << SetForegroundWindow( this->winId() ); } 

This will display the taskbar and highlight the application icon as soon as possible. It does not switch to the application.

Qt's own activateWindow() results in a more stable blinking taskbar icon, but does not raise the application.

This has been verified previously:

The latter involves:

 showNormal(); raise(); activateWindow(); 

but this does not work for me on a 64-bit version of Windows 7 with Qt 4.8.1 and MSVC ++ 2010.

Here is the code that I think is also mentioned in other issues:

The author writes

It always brings the window to the forefront, but the focus is somewhere in the system :-( In another application ...

I can confirm this.

<h / "> Edit: Windows behavior may (should not !?) change globally through the registry: fooobar.com/questions/472254 / ... points to http://qt-project.org/faq/answer/qwidget_activatewindow_- _behavior_under_windows

+2
source

The WinActivate search led to an AutoHotkey forum post that links to the WinAPI GetForegroundWindow and SetForegroundWindow .

 BOOL WINAPI SetForegroundWindow( _In_ HWND hWnd ); 

However, this is not a real solution, because it

  • window handle required (how to get it by window name?)
  • has limited permissions (requires a process ownership trick: here and here )

The last link is rather complicated, but the author seems to have refused.

0
source

Source: https://habr.com/ru/post/1489761/


All Articles