Qt 设置程序置顶

使用qprocess启动进程后,随着点击主界面,被启动的进程会显示在下层。使用代码的方式将其显示在顶层。

#include <windows.h>

int g_winState = SW_SHOWNORMAL;

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);
        const UINT swpFlags = SWP_NOMOVE | SWP_NOSIZE | SWP_NOOWNERZORDER;
        SetWindowPos(hWnd, HWND_TOP, 0, 0, 0, 0, swpFlags);
        return FALSE;
    }

    return TRUE;  //continue enumeration
}

// 调用
qint64 pid;
pid = process_launchFollow.processId();
EnumWindows(EnumWindowsProc, (LPARAM)pid);

参考:
https://stackoverflow.com/questions/55623311/how-to-bring-a-qprocess-to-the-front
https://cloud.tencent.com/developer/article/2241283

posted @ 2023-09-14 22:02  duapple  阅读(68)  评论(0编辑  收藏  举报  来源