Windows 和 QWidget::activateWindow() 中的活动窗口
QWidget::activateWindow()
的 Qt 文档指出:
在 Windows 上,如果您调用此 当应用程序当前不存在时 活跃的那么它不会 它是活动窗口。它会改变 任务栏条目的颜色 表明窗口已经改变 以某种方式。这是因为微软 不允许应用程序 打断用户当前的状态 在另一个应用程序中执行操作。
然而,Skype 似乎违反了这一规则。如果 Skype 正在运行但不是活动应用程序,我可以从开始菜单启动它,它将现有实例带到前台,激活它并获取输入焦点。
我怎样才能做到这一点?
The Qt documentation for QWidget::activateWindow()
states:
On Windows, if you are calling this
when the application is not currently
the active one then it will not make
it the active window. It will change
the color of the taskbar entry to
indicate that the window has changed
in some way. This is because Microsoft
does not allow an application to
interrupt what the user is currently
doing in another application.
However, Skype appears to defy this rule. If Skype is running but is not the active application, I can launch it from the start menu and it brings the existing instance to the foreground, activates it and grabs input focus.
And how can I do this?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
(注意:这特定于 QtSingleApplication 的工作方式)
对于我的问题来说,解决方案非常简单。只需在应用程序开始时调用
AllowSetForegroundWindow(ASF_ANY);
,原始进程就可以通过使用SetForegroundWindow()
将自己带到前台。没有奇怪的 hack,只需添加一行代码,也不需要修改 QtSingleApplication。(NOTE: This is specific to how QtSingleApplication works)
The solution is stupidly simple for my issue. Simply call
AllowSetForegroundWindow(ASF_ANY);
at the beginning of the application, and the original process will thus be allowed to bring itself to the foreground by use ofSetForegroundWindow()
. No strange hacks, just one line of code to add and no need to modify QtSingleApplication either.我认为仅使用 Qt API 无法可靠地完成此操作。
Windows 有多种解决方案。例如此处和此处,以及 此处。
我之前使用的方法是声明一个 共享内存部分,并在其中写入应用程序的窗口句柄。稍后,当程序的第二个实例启动时,您可以找到第一个实例的窗口句柄并激活它。
我认为在这种情况下,您不会遇到窗口阻止您执行此操作的问题,因为您的第二个实例是活动应用程序,因此允许它“将焦点传递给”其他窗口。
I don't think you can do it reliably with the Qt API alone.
There are multiple solutions for windows. E.g. here, and here, and here.
The method I've used before is to declare a shared memory section, and write the application's window handle there. Later, when a second instance of your program is started, you can find the window handle of the first and activate it.
I don't think you have the issue of windows preventing you from doing this in this case, because your second instance is the active application, so it is allowed to 'pass focus' to other windows.
在 Qt 中使用单一应用程序解决方案
Use Single Application in Qt Solutions
你可以将 setWindowOpacity 设置为从 0 到 1。唯一的事情是你可以一直打开它
you can set the setWindowOpacity from 0 to 1 .the only thing is you may open it all the time