在什么条件下,允许使用Win32函数SetForegroundWindow设置前景窗口?

发布于 2025-02-13 17:11:01 字数 861 浏览 1 评论 0原文

我想了解一个过程可以使用win32函数 setForegroundWindow 。我尝试阅读其文档,并在下说

一个过程只有在以下条件之一为真时才能设置前景窗口:

  • 该过程是前景过程。
  • 该过程是由前景过程开始的。
  • 该过程收到了最后一个输入事件。
  • 没有前景过程。
  • 该过程正在调试。
  • 前景过程不是现代应用程序或开始屏幕。
  • 前景未锁定(请参阅lockset Foregroundwindow)。
  • 前景锁定超时已过期(请参阅spi_get foregroundlocktimeout in systemparametersinfo)。
  • 没有菜单活跃。

在我看来,这是不准确的,因为这意味着所有这些子弹都必须是错误的才能限制设置前景窗口,但是最后四个似乎通常是正确的(取决于您使用“现代”应用程序)。因此,正如书面的那样,设置前景窗口几乎永远不会受到限制。在实验上,似乎在另一个过程中拥有一个活动菜单“足以阻止设置前景窗口,即使前景过程不是现代应用程序(条件6是正确的)。

什么时候可以根据列出的条件设置前景窗口时表达更准确的方法?

I want to understand under what conditions a process can set the foreground window using the Win32 function SetForegroundWindow. I tried reading its documentation and it says under Remarks:

A process can set the foreground window only if one of the following conditions is true:

  • The process is the foreground process.
  • The process was started by the foreground process.
  • The process received the last input event.
  • There is no foreground process.
  • The process is being debugged.
  • The foreground process is not a Modern Application or the Start Screen.
  • The foreground is not locked (see LockSetForegroundWindow).
  • The foreground lock time-out has expired (see SPI_GETFOREGROUNDLOCKTIMEOUT in SystemParametersInfo).
  • No menus are active.

It seems to me that this cannot be accurate, as it would mean that all of these bullets would have to be false to restrict setting the foreground window, but the last four seem to be normally true (depending on your use of "modern" applications). So as written, setting the foreground window would almost never be restricted. Whereas experimentally it seems that having a menu active in another process' window is sufficient to block setting the foreground window, even if e.g. the foreground process is not a Modern Application (condition 6 is true).

What would be a more accurate way to express when a process can set the foreground window based on the listed conditions?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

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

发布评论

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

评论(1

感情洁癖 2025-02-20 17:11:01

MSDN文档已经迁移了很多次到不同的格式。每次微软这样做时,文档都会遭受损失。

文章更正确:

该系统限制哪些过程可以设置前景窗口。一个过程只有在以下方式时才能设置前景窗口

以下所有条件都是正确的:

  • 调用SetForegroundWindow的过程属于桌面应用程序,而不是UWP应用程序或Windows Store App设计用于Windows 8或8.1的Windows Store应用程序。
  • 前景过程尚未通过以前的locksetForegroundWindow函数来禁用呼叫setForegroundWindow。
  • 前景锁定超时已过期(请参阅SystemParameterSinfo中的Spi_getForegroundlockTimeout)。
  • 没有菜单活跃。

此外,至少以下条件中的一个是正确的:

  • 通话过程是前景过程。
  • 通话过程是由前景过程开始的。
  • 当前没有前景窗口,因此没有前景过程。
  • 呼叫过程收到了最后一个输入事件。
  • 前景过程或呼叫过程正在调试。

即使符合这些条件,也有可能剥夺设置前景窗口的权利。

请特别注意最后一句话。因此,最好不要太努力地考虑它,因为它可能无缘无故地失败。

MSDN docs have been migrated a lot of times to different formats. Every time Microsoft did that, the documentation has suffered.

The Window Features article is more correct:

The system restricts which processes can set the foreground window. A process can set the foreground window only if:

All of the following conditions are true:

  • The process calling SetForegroundWindow belongs to a desktop application, not a UWP app or a Windows Store app designed for Windows 8 or 8.1.
  • The foreground process has not disabled calls to SetForegroundWindow by a previous call to the LockSetForegroundWindow function.
  • The foreground lock time-out has expired (see SPI_GETFOREGROUNDLOCKTIMEOUT in SystemParametersInfo).
  • No menus are active.

Additionally, at least one of the following conditions is true:

  • The calling process is the foreground process.
  • The calling process was started by the foreground process.
  • There is currently no foreground window, and thus no foreground process.
  • The calling process received the last input event.
  • Either the foreground process or the calling process is being debugged.

It is possible for a process to be denied the right to set the foreground window even if it meets these conditions.

Please especially note the last sentence. So, better not think about it too hard, because it may fail anyway for no reason.

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