mirror of
https://github.com/itay-grudev/SingleApplication.git
synced 2024-11-15 12:15:43 +08:00
fix code according to pull request review
This commit is contained in:
parent
d1e9b77e55
commit
dd0e1b3eef
16
.github/workflows/main.yml
vendored
16
.github/workflows/main.yml
vendored
@ -59,7 +59,7 @@ jobs:
|
||||
cmake . ${{ matrix.additional_arguments }}
|
||||
cmake --build .
|
||||
|
||||
- name: Build calculator example CMake
|
||||
- name: Build calculator example with CMake
|
||||
working-directory: examples/calculator/
|
||||
run: |
|
||||
cmake . ${{ matrix.additional_arguments }}
|
||||
@ -71,6 +71,12 @@ jobs:
|
||||
cmake . ${{ matrix.additional_arguments }}
|
||||
cmake --build .
|
||||
|
||||
- name: Build windows_raise_widget example with CMake
|
||||
working-directory: examples/windows_raise_widget/
|
||||
run: |
|
||||
cmake . ${{ matrix.additional_arguments }}
|
||||
cmake --build .
|
||||
|
||||
- name: Setup MSVC environment for QMake
|
||||
uses: ilammy/msvc-dev-cmd@v1
|
||||
|
||||
@ -80,7 +86,7 @@ jobs:
|
||||
qmake
|
||||
${{ matrix.make }}
|
||||
|
||||
- name: Build calculator example QMake
|
||||
- name: Build calculator example with QMake
|
||||
working-directory: examples/calculator/
|
||||
run: |
|
||||
qmake
|
||||
@ -91,3 +97,9 @@ jobs:
|
||||
run: |
|
||||
qmake
|
||||
${{ matrix.make }}
|
||||
|
||||
- name: Build windows_raise_widget example with QMake
|
||||
working-directory: examples/windows_raise_widget/
|
||||
run: |
|
||||
qmake
|
||||
${{ matrix.make }}
|
||||
|
36
Windows.md
36
Windows.md
@ -7,8 +7,7 @@ application can bring it's primary instance window whenever a second copy
|
||||
of the application is started.
|
||||
|
||||
On Windows the ability to bring the application windows to the foreground is
|
||||
restricted, see [AllowSetForegroundWindow()][https://msdn.microsoft.com/en-us/library/windows/desktop/ms632668.aspx] for more
|
||||
details.
|
||||
restricted, see [AllowSetForegroundWindow()](https://msdn.microsoft.com/en-us/library/windows/desktop/ms632668.aspx) for more 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
|
||||
@ -19,35 +18,4 @@ 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;
|
||||
}
|
||||
|
||||
QWidget* widget = new QWidget;
|
||||
|
||||
QObject::connect(&app, &SingleApplication::receivedMessage,
|
||||
widget, [widget] () { RaiseWidget(widget); } );
|
||||
```
|
||||
|
||||
```cpp
|
||||
void RaiseWidget(QWidget* widget) {
|
||||
|
||||
HWND hwnd = (HWND)widget->winId();
|
||||
|
||||
// check if widget is minimized
|
||||
if (::IsIconic(hwnd)) {
|
||||
::ShowWindow(hwnd, SW_RESTORE);
|
||||
}
|
||||
|
||||
::SetForegroundWindow(hwnd);
|
||||
}
|
||||
```
|
||||
You can find demo code in the [examples](examples/windows_raise_widget/main.cpp) directory.
|
||||
|
@ -5,10 +5,6 @@ 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)
|
||||
@ -17,4 +13,4 @@ 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)
|
||||
target_link_libraries(${PROJECT_NAME} SingleApplication::SingleApplication)
|
||||
|
@ -1,41 +1,59 @@
|
||||
|
||||
#include <Windows.h>
|
||||
|
||||
#include <QWidget>
|
||||
|
||||
#include "singleapplication.h"
|
||||
|
||||
void RaiseWidget(QWidget* pWidget);
|
||||
#ifdef Q_OS_WINDOWS
|
||||
#include <Windows.h>
|
||||
#endif
|
||||
|
||||
void raiseWidget(QWidget* widget);
|
||||
|
||||
int main(int argc, char *argv[]) {
|
||||
|
||||
#ifdef Q_OS_WINDOWS
|
||||
SingleApplication app(argc, argv, true);
|
||||
|
||||
if (app.isSecondary()) {
|
||||
|
||||
AllowSetForegroundWindow( DWORD( app.primaryPid() ) );
|
||||
|
||||
app.sendMessage("SHOW_WINDOW");
|
||||
app.sendMessage("RAISE_WIDGET");
|
||||
|
||||
return 0;
|
||||
}
|
||||
#else
|
||||
SingleApplication app(argc, argv);
|
||||
#endif
|
||||
|
||||
QWidget* widget = new QWidget;
|
||||
|
||||
#ifdef Q_OS_WINDOWS
|
||||
QObject::connect(&app, &SingleApplication::receivedMessage,
|
||||
widget, [widget] () { RaiseWidget(widget); } );
|
||||
widget, [widget] () { raiseWidget(widget); } );
|
||||
#else
|
||||
QObject::connect(&app, &SingleApplication::instanceStarted,
|
||||
widget, [widget] () { raiseWidget(widget); } );
|
||||
#endif
|
||||
|
||||
widget->show();
|
||||
|
||||
return app.exec();
|
||||
}
|
||||
|
||||
void RaiseWidget(QWidget* widget) {
|
||||
|
||||
void raiseWidget(QWidget* widget) {
|
||||
#ifdef Q_OS_WINDOWS
|
||||
HWND hwnd = (HWND)widget->winId();
|
||||
|
||||
// check if widget is minimized to Windows task bar
|
||||
if (::IsIconic(hwnd)) {
|
||||
::ShowWindow(hwnd, SW_RESTORE);
|
||||
}
|
||||
|
||||
::SetForegroundWindow(hwnd);
|
||||
#else
|
||||
widget->show();
|
||||
widget->raise();
|
||||
widget->activateWindow();
|
||||
#endif
|
||||
}
|
||||
|
@ -4,4 +4,7 @@ DEFINES += QAPPLICATION_CLASS=QApplication
|
||||
|
||||
QT += widgets
|
||||
SOURCES += main.cpp
|
||||
LIBS += User32.lib
|
||||
|
||||
win32{
|
||||
LIBS += User32.lib
|
||||
}
|
||||
|
Loading…
Reference in New Issue
Block a user