Home > front end >  Under what conditions is a process allowed to set the foreground window using the Win32 function Set
Under what conditions is a process allowed to set the foreground window using the Win32 function Set

Time:07-07

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?

CodePudding user response:

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.

  • Related