【问题标题】:How to bring a qprocess to the front?如何将 qprocess 带到最前面?
【发布时间】:2019-04-11 01:11:00
【问题描述】:

工作环境:C++,窗口 我使用 qprocess 打开了一个 matlab 独立应用程序(xx.exe)。当用户按下按钮时,我想把 xx.exe 带到前面。如何使用 Qpr​​ocess 将 xx.exe 置于最前面?

【问题讨论】:

    标签: c++ windows qt qprocess


    【解决方案1】:

    也许QProcess::startDetached() 会帮助您(在我的情况下,此方法会激活窗口)。但我认为窗口操作(如 activateminimizehide)是操作系统问题。因此,在大多数情况下,您必须请求操作系统进行窗口操作。

    这是一个 Windows 的小例子,你可以试试

    WindowsUtils.h

    class WindowsUtils
    {
    public:
        WindowsUtils();
    
        static bool ShowWindow(const qint64& pidQt);
        static bool MinimizeWindow(const qint64& pidQt);
        static bool RestoreWindow(const qint64& pidQt);
    };
    

    WindowsUtils.cpp

    #include "WindowsUtils.h"
    #include <windows.h>
    
    int g_winState = SW_SHOW;
    
    BOOL CALLBACK EnumWindowsProc(HWND hWnd, LPARAM lParam)
    {
        // get the window process ID
        DWORD searchedProcessId = (DWORD)lParam;
        DWORD windowProcessId = 0;
        GetWindowThreadProcessId(hWnd,&windowProcessId);
    
        // check the process id match
        if (windowProcessId == searchedProcessId){
            ShowWindow(hWnd, g_winState);
            return FALSE;
        }
    
        return TRUE;  //continue enumeration
    }
    
    WindowsUtils::WindowsUtils()
    {
    
    }
    
    bool WindowsUtils::ShowWindow(const qint64 &pidQt)
    {
        g_winState = SW_SHOW;
        return EnumWindows(EnumWindowsProc, (LPARAM)pidQt);
    }
    
    bool WindowsUtils::MinimizeWindow(const qint64 &pidQt)
    {
        g_winState = SW_MINIMIZE;
        return EnumWindows(EnumWindowsProc, (LPARAM)pidQt);
    }
    
    bool WindowsUtils::RestoreWindow(const qint64 &pidQt)
    {
        g_winState = SW_RESTORE;
        return EnumWindows(EnumWindowsProc, (LPARAM)pidQt);
    }
    

    使用

    class MainWindow : public QMainWindow
    {
        Q_OBJECT
    
    public:
        explicit MainWindow(QWidget *parent = 0);
        ~MainWindow();
    
    private slots:
    
        void on_pushButton_3_clicked();
    
        void on_pushButton_4_clicked();
    
        void on_pushButton_5_clicked();
    
    private:
        Ui::MainWindow *ui;
        qint64 m_pid;
    };
    
    void MainWindow::on_pushButton_3_clicked()
    {
        m_pid = 0;
        QProcess::startDetached("notepad.exe", QStringList(), QString(), &m_pid);
    }
    
    void MainWindow::on_pushButton_4_clicked()
    {
        WindowsUtils::RestoreWindow(m_pid);
    }
    
    void MainWindow::on_pushButton_5_clicked()
    {
        WindowsUtils::MinimizeWindow(m_pid);
    }
    

    int g_winState; 的另一个值是 here

    【讨论】:

      猜你喜欢
      • 2020-12-10
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多