Skip to content

Commit

Permalink
fix code according to pull request review
Browse files Browse the repository at this point in the history
  • Loading branch information
samsonwang committed Mar 14, 2023
1 parent d1e9b77 commit dd0e1b3
Show file tree
Hide file tree
Showing 5 changed files with 46 additions and 49 deletions.
16 changes: 14 additions & 2 deletions .github/workflows/main.yml
Original file line number Diff line number Diff line change
Expand Up @@ -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 }}
Expand All @@ -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

Expand All @@ -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
Expand All @@ -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 changes: 2 additions & 34 deletions Windows.md
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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.
6 changes: 1 addition & 5 deletions examples/windows_raise_widget/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand All @@ -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)
32 changes: 25 additions & 7 deletions examples/windows_raise_widget/main.cpp
Original file line number Diff line number Diff line change
@@ -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
}
5 changes: 4 additions & 1 deletion examples/windows_raise_widget/windows_raise_widget.pro
Original file line number Diff line number Diff line change
Expand Up @@ -4,4 +4,7 @@ DEFINES += QAPPLICATION_CLASS=QApplication

QT += widgets
SOURCES += main.cpp
LIBS += User32.lib

win32{
LIBS += User32.lib
}

0 comments on commit dd0e1b3

Please sign in to comment.