mirror of
https://github.com/itay-grudev/SingleApplication.git
synced 2024-11-15 12:15:43 +08:00
Windows raise widget
This commit is contained in:
parent
f1e15081dc
commit
d1e9b77e55
39
Windows.md
39
Windows.md
@ -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
|
||||
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);
|
||||
}
|
||||
```
|
||||
|
20
examples/windows_raise_widget/CMakeLists.txt
Normal file
20
examples/windows_raise_widget/CMakeLists.txt
Normal 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)
|
41
examples/windows_raise_widget/main.cpp
Normal file
41
examples/windows_raise_widget/main.cpp
Normal 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);
|
||||
}
|
7
examples/windows_raise_widget/windows_raise_widget.pro
Normal file
7
examples/windows_raise_widget/windows_raise_widget.pro
Normal file
@ -0,0 +1,7 @@
|
||||
# Single Application implementation
|
||||
include(../../singleapplication.pri)
|
||||
DEFINES += QAPPLICATION_CLASS=QApplication
|
||||
|
||||
QT += widgets
|
||||
SOURCES += main.cpp
|
||||
LIBS += User32.lib
|
Loading…
Reference in New Issue
Block a user