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 Answer
Reset to default 1I 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
版权声明:本文标题:c++ - How to force a Qt Window on foreground after clicking notification - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1736598576a1945178.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
activateWindow()
on your Qt window help? doc.qt.io/qt-6/qwidget.html#activateWindow – Jeremy Friesner Commented yesterdaySetForegroundWindow
. And you look like you're doing something very fishy. stackoverflow.com/a/8081858/15639 – 許恩嘉 Commented yesterday