From c3e0c0099fe6f130f3c2c2938bfa5da07479d0b4 Mon Sep 17 00:00:00 2001 From: Morteza Jamshidi Date: Thu, 30 Jan 2025 16:41:35 +0100 Subject: [PATCH] Use WS_EX_NOACTIVATE to prevent window from getting focused On top of handling ActivateWindowEvent,MouseActivateWindowEvent and PointerActivateWindowEvent events and returning MA_NOACTIVATE we can also set the window extended style to prevent windows from stealing focus in special cases. Also on top all those mentioned events, when the window is requested to get keyboard focus, it should check if the window accepts focus or not. If it does not, then it should inform the underlying system that the window does not accept the focusIn event. [ChangeLog][Windows] Windows with flag Qt::WindowDoesNotAcceptFocus no longer have a taskbar entry. Fixes: QTBUG-131714 Change-Id: I79f767b1622449ba05b41f8b80bf390d8cecfff8 Reviewed-by: Oliver Wolff Reviewed-by: Zhao Yuhang <2546789017@qq.com> (cherry picked from commit c2bdf7636eb46e56760f3e2f6d457c8f14627576) Reviewed-by: Qt Cherry-pick Bot --- src/corelib/global/qnamespace.qdoc | 2 ++ src/plugins/platforms/windows/qwindowscontext.cpp | 2 ++ src/plugins/platforms/windows/qwindowswindow.cpp | 3 +++ 3 files changed, 7 insertions(+) diff --git a/src/corelib/global/qnamespace.qdoc b/src/corelib/global/qnamespace.qdoc index b2eefbe5aaa..e55ffdc2290 100644 --- a/src/corelib/global/qnamespace.qdoc +++ b/src/corelib/global/qnamespace.qdoc @@ -2345,6 +2345,8 @@ \value WindowDoesNotAcceptFocus Informs the window system that this window should not receive the input focus. + \note On Windows, this prevents the window from appearing in the taskbar. + \value MaximizeUsingFullscreenGeometryHint Deprecated alias for Qt::ExpandedClientAreaHint \value [since 6.9] ExpandedClientAreaHint Requests that the window's client area is diff --git a/src/plugins/platforms/windows/qwindowscontext.cpp b/src/plugins/platforms/windows/qwindowscontext.cpp index 58514c96e1d..a91d2369475 100644 --- a/src/plugins/platforms/windows/qwindowscontext.cpp +++ b/src/plugins/platforms/windows/qwindowscontext.cpp @@ -1217,6 +1217,8 @@ bool QWindowsContext::windowsProc(HWND hwnd, UINT message, case QtWindows::PointerEvent: return sessionManagerInteractionBlocked() || d->m_pointerHandler.translatePointerEvent(platformWindow->window(), hwnd, et, msg, result); case QtWindows::FocusInEvent: // see QWindowsWindow::requestActivateWindow(). + if (platformWindow->window()->flags() & Qt::WindowDoesNotAcceptFocus) + return false; case QtWindows::FocusOutEvent: handleFocusEvent(et, platformWindow); return true; diff --git a/src/plugins/platforms/windows/qwindowswindow.cpp b/src/plugins/platforms/windows/qwindowswindow.cpp index d7be8bbf4b1..78447ba9211 100644 --- a/src/plugins/platforms/windows/qwindowswindow.cpp +++ b/src/plugins/platforms/windows/qwindowswindow.cpp @@ -819,6 +819,9 @@ void WindowCreationData::fromWindow(const QWindow *w, const Qt::WindowFlags flag style |= WS_CLIPSIBLINGS | WS_CLIPCHILDREN ; + if (flags & Qt::WindowDoesNotAcceptFocus) + exStyle |= WS_EX_NOACTIVATE; + if (topLevel) { if ((type == Qt::Window || dialog || tool)) { if (!(flags & Qt::FramelessWindowHint)) {