需要在 Windows 上将应用程序置于前台 [英] Need to bring application to foreground on Windows

查看:25
本文介绍了需要在 Windows 上将应用程序置于前台的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有两个在 Windows 上使用 Qt 开发的应用程序.我希望用户能够按下一个应用程序中的按钮,使另一个应用程序进入前台.(程序使用 QLocalSocket 和命名管道进行通信.)

I've got two applications I'm developing using Qt on windows. I want the user to be able to press a button in one application which makes the other application come to the foreground. (The programs communicate using QLocalSocket and named pipes.)

目前我正在使用 Qt 的 QWidget::activateWindow(),它偶尔会将应用程序带到前台,但大多数时候它只是突出显示任务栏上的程序.

Currently I'm using Qt's QWidget::activateWindow() which occasionally brings the application to the foreground, but most of the time it just highlights the program on the taskbar.

有人可以告诉我如何做到这一点,最好使用 Qt,尽管使用 WIN32 API 失败也没关系.

Can someone please tell me how to do this, preferably using Qt although failing that using the WIN32 API would be fine.

不幸的是,我找不到仅使用 Qt 的方法.我使用 Chris Becke 的建议解决了这个问题,即从当前活动的应用程序调用 SetForegroundWindow.

Unfortunately, I couldn't find a way to do this only with Qt. I solved it using Chris Becke's suggestion of calling SetForegroundWindow from the currently active application.

推荐答案

您确定这不是调试问题吗?交易是,如果应用程序拥有前台,则允许更改前台.

Are you sure this is not a debugging issue? The deal is, if an application HAS the foreground, it is allowed to change the foreground.

单击窗口 A 上的按钮将使该窗口线程前台激活.如果它在另一个窗口上调用 SetForegroundWindow(或等效的),则该窗口将被赋予前景.

Clicking a button on window A will give that windows thread foreground activation. If it calls SetForegroundWindow (or equivalent) on the other window, that window WILL be given the foreground.

另一方面,如果它只是向其他应用程序发送一条消息,该应用程序尝试在自身上设置 Foreground,则会失败.AllowSetForegroundWindow 用于遗留"应用程序需要获得许可(由前台应用程序)才能进入前台的情况.再次强调,AllowSet... 仅在从拥有当前活动前景窗口的线程调用时才有效.

If, on the other hand, it simply sends a message to the other app, which tries to SetForeground on itself, that will fail. AllowSetForegroundWindow is used in situations where a 'legacy' app needs to be given permission - by a foreground app - to take the foreground. Once again, AllowSet... only works if called from a thread that owns the current active foreground window.

这篇关于需要在 Windows 上将应用程序置于前台的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆