Windows raise widget

This commit is contained in:
Samson Wang 2023-03-13 15:36:59 +08:00
parent f1e15081dc
commit d1e9b77e55
No known key found for this signature in database
GPG Key ID: 07CAAD715D2AFDD2
4 changed files with 94 additions and 15 deletions

View File

@ -13,30 +13,41 @@ details.
The background process (the primary instance) can bring its windows to the
foreground if it is allowed by the current foreground process (the secondary
instance). To bypass this `SingleApplication` must be initialized with the
`allowSecondary` parameter set to `true` and the `options` parameter must
include `Mode::SecondaryNotification`, See `SingleApplication::Mode` for more
details.
`allowSecondary` parameter set to `true` .
Here is an example:
If the widget is minimized to Windows task bar, `QWidget::raise()` or
`QWidget::show()` can not bring it to the front, you have to use Windows API
`ShowWindow()` .
Here is an example, you can find this project in the example directory:
```cpp
if( app.isSecondary() ) {
SingleApplication app(argc, argv, true);
if ( app.isSecondary() ) {
// This API requires LIBS += User32.lib to be added to the project
AllowSetForegroundWindow( DWORD( app.primaryPid() ) );
objApp.sendMessage("SHOW_WINDOW");
return 0;
}
if( app.isPrimary() ) {
QObject::connect(
&app,
&SingleApplication::instanceStarted,
this,
&App::instanceStarted
);
}
QWidget* widget = new QWidget;
QObject::connect(&app, &SingleApplication::receivedMessage,
widget, [widget] () { RaiseWidget(widget); } );
```
```cpp
void App::instanceStarted() {
QApplication::setActiveWindow( [window/widget to set to the foreground] );
void RaiseWidget(QWidget* widget) {
HWND hwnd = (HWND)widget->winId();
// check if widget is minimized
if (::IsIconic(hwnd)) {
::ShowWindow(hwnd, SW_RESTORE);
}
::SetForegroundWindow(hwnd);
}
```

View File

@ -0,0 +1,20 @@
cmake_minimum_required(VERSION 3.7.0)
project(windows_raise_widget LANGUAGES CXX)
set(CMAKE_AUTOMOC ON)
set(CMAKE_WIN32_EXECUTABLE TRUE)
if (NOT DEFINED ENV{CMAKE_PREFIX_PATH})
set (ENV{CMAKE_PREFIX_PATH} "C:/Qt/Qt5.12.10/5.12.10/msvc2017_64")
endif ()
# SingleApplication base class
set(QAPPLICATION_CLASS QApplication)
add_subdirectory(../.. SingleApplication)
find_package(Qt${QT_DEFAULT_MAJOR_VERSION} COMPONENTS Core Widgets REQUIRED)
add_executable(${PROJECT_NAME} main.cpp)
target_link_libraries(${PROJECT_NAME} SingleApplication::SingleApplication user32.lib)

View File

@ -0,0 +1,41 @@
#include <Windows.h>
#include <QWidget>
#include "singleapplication.h"
void RaiseWidget(QWidget* pWidget);
int main(int argc, char *argv[]) {
SingleApplication app(argc, argv, true);
if (app.isSecondary()) {
AllowSetForegroundWindow( DWORD( app.primaryPid() ) );
app.sendMessage("SHOW_WINDOW");
return 0;
}
QWidget* widget = new QWidget;
QObject::connect(&app, &SingleApplication::receivedMessage,
widget, [widget] () { RaiseWidget(widget); } );
widget->show();
return app.exec();
}
void RaiseWidget(QWidget* widget) {
HWND hwnd = (HWND)widget->winId();
if (::IsIconic(hwnd)) {
::ShowWindow(hwnd, SW_RESTORE);
}
::SetForegroundWindow(hwnd);
}

View File

@ -0,0 +1,7 @@
# Single Application implementation
include(../../singleapplication.pri)
DEFINES += QAPPLICATION_CLASS=QApplication
QT += widgets
SOURCES += main.cpp
LIBS += User32.lib