admin管理员组

文章数量:1125939

I am working on messaging application that uses Qt5. When new message is received is shows a Windows notification. When I click the notification it supposed to open the application window, which it actually does. But the opened window is not foreground, so when I start typing the window is out of focus and nothing happens. I need to clikc on the window first and then start typing.

I currently use the following code to open the application window:

appWindow->setWindowState(static_cast<Qt::WindowState>(appWindow->windowState() & ~Qt::WindowMinimized));

    const auto winId = appWindow->winId();

#ifdef Q_OS_WIN
    QWindowsWindowFunctions::setWindowActivationBehavior(QWindowsWindowFunctions::AlwaysActivateWindow);
    appWindow->requestActivate();

    // Setting rights for the process to move window to foreground
    // They get reset after user input
    AllowSetForegroundWindow(ASFW_ANY);

    // Getting window handles for QApp and currently active process
    HWND  appWinHandle   = HWND(winId);
    HWND  curWinHandle   = GetForegroundWindow();
    DWORD appWinThreadID = GetCurrentThreadId();
    DWORD curWinThreadID = GetWindowThreadProcessId(curWinHandle, NULL);

    // Forcing qWindow raise by setting it to be topmost and releasing right after
    SetWindowPos(appWinHandle, HWND_TOPMOST, 0, 0, 0, 0, SWP_NOSIZE | SWP_NOMOVE);
    SetWindowPos(appWinHandle, HWND_NOTOPMOST, 0, 0, 0, 0, SWP_NOSIZE | SWP_NOMOVE);

    AllowSetForegroundWindow(curWinThreadID);


    // Moving input thread from current process to qApp
    // Simulate Alt press and release to ensure qApp process got focus
    keybd_event(VK_MENU, 0, 0, 0);
    SetForegroundWindow(appWinHandle);
    keybd_event(VK_MENU, 0, KEYEVENTF_KEYUP, 0);
    AttachThreadInput(curWinThreadID, appWinThreadID, FALSE);
    SetFocus(appWinHandle);
    SetActiveWindow(appWinHandle);

But it still does't open the application window in foreground. Are there any ways to open it foreground. Btw, I've heard that Windows 10 has some restrions on opening windows in foreground, but some applications (for example OutLook) work the way I want my application do.

I am working on messaging application that uses Qt5. When new message is received is shows a Windows notification. When I click the notification it supposed to open the application window, which it actually does. But the opened window is not foreground, so when I start typing the window is out of focus and nothing happens. I need to clikc on the window first and then start typing.

I currently use the following code to open the application window:

appWindow->setWindowState(static_cast<Qt::WindowState>(appWindow->windowState() & ~Qt::WindowMinimized));

    const auto winId = appWindow->winId();

#ifdef Q_OS_WIN
    QWindowsWindowFunctions::setWindowActivationBehavior(QWindowsWindowFunctions::AlwaysActivateWindow);
    appWindow->requestActivate();

    // Setting rights for the process to move window to foreground
    // They get reset after user input
    AllowSetForegroundWindow(ASFW_ANY);

    // Getting window handles for QApp and currently active process
    HWND  appWinHandle   = HWND(winId);
    HWND  curWinHandle   = GetForegroundWindow();
    DWORD appWinThreadID = GetCurrentThreadId();
    DWORD curWinThreadID = GetWindowThreadProcessId(curWinHandle, NULL);

    // Forcing qWindow raise by setting it to be topmost and releasing right after
    SetWindowPos(appWinHandle, HWND_TOPMOST, 0, 0, 0, 0, SWP_NOSIZE | SWP_NOMOVE);
    SetWindowPos(appWinHandle, HWND_NOTOPMOST, 0, 0, 0, 0, SWP_NOSIZE | SWP_NOMOVE);

    AllowSetForegroundWindow(curWinThreadID);


    // Moving input thread from current process to qApp
    // Simulate Alt press and release to ensure qApp process got focus
    keybd_event(VK_MENU, 0, 0, 0);
    SetForegroundWindow(appWinHandle);
    keybd_event(VK_MENU, 0, KEYEVENTF_KEYUP, 0);
    AttachThreadInput(curWinThreadID, appWinThreadID, FALSE);
    SetFocus(appWinHandle);
    SetActiveWindow(appWinHandle);

But it still does't open the application window in foreground. Are there any ways to open it foreground. Btw, I've heard that Windows 10 has some restrions on opening windows in foreground, but some applications (for example OutLook) work the way I want my application do.

Share Improve this question edited 14 hours ago musicamante 48.1k8 gold badges40 silver badges71 bronze badges asked yesterday Витя НовиковВитя Новиков 211 bronze badge New contributor Витя Новиков is a new contributor to this site. Take care in asking for clarification, commenting, and answering. Check out our Code of Conduct. 3
  • 1 Does calling activateWindow() on your Qt window help? doc.qt.io/qt-6/qwidget.html#activateWindow – Jeremy Friesner Commented yesterday
  • I suggest you check the return values ​​of each function, especially SetForegroundWindow. And you look like you're doing something very fishy. stackoverflow.com/a/8081858/15639 – 許恩嘉 Commented yesterday
  • You cannot force a window to always be on top. Just think about 2 different programs trying to do this - they can't both win all the time. You can ask to be put on top, but it will not always happen. – Jesper Juhl Commented 13 hours ago
Add a comment  | 

1 Answer 1

Reset to default 1

I use the following code in my apps for Windows:

void forceSetForegroundWindow(HWND hWnd)
{
    if(!hWnd || !::IsWindow(hWnd)) return;

    HWND  hCurrWnd = ::GetForegroundWindow();
    DWORD dwThisTID = ::GetCurrentThreadId(),
          dwCurrTID = ::GetWindowThreadProcessId(hCurrWnd, NULL);

    if(dwThisTID != dwCurrTID)
        ::AttachThreadInput(dwThisTID, dwCurrTID, TRUE);

    ::BringWindowToTop(hWnd);
    ::SetForegroundWindow(hWnd);

    if(dwThisTID != dwCurrTID)
        ::AttachThreadInput(dwThisTID, dwCurrTID, FALSE);
}

本文标签: cHow to force a Qt Window on foreground after clicking notificationStack Overflow