Windows 和 QWidget::activateWindow() 中的活动窗口

发布于 2024-09-11 06:08:46 字数 332 浏览 0 评论 0原文

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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

烦人精 2024-09-18 06:08:46

注意:这特定于 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 of SetForegroundWindow(). No strange hacks, just one line of code to add and no need to modify QtSingleApplication either.

旧伤慢歌 2024-09-18 06:08:46

我认为仅使用 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.

南巷近海 2024-09-18 06:08:46

在 Qt 中使用单一应用程序解决方案

对于某些应用程序来说它很有用或
甚至批评它们是否已启动
任何用户只能使用一次。未来的尝试
启动应用程序应该
激活任何已经运行的实例,
并可能执行请求
动作,例如加载文件,其中
实例。

Use Single Application in Qt Solutions

For some applications it is useful or
even critical that they are started
only once by any user. Future attempts
to start the application should
activate any already running instance,
and possibly perform requested
actions, e.g. loading a file, in that
instance.

兔小萌 2024-09-18 06:08:46

你可以将 setWindowOpacity 设置为从 0 到 1。唯一的事情是你可以一直打开它

you can set the setWindowOpacity from 0 to 1 .the only thing is you may open it all the time

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文