An application that currently has foreground focus is the only one that is allowed to change foreground focus. You need to use the SetForegroundWindow function to realize this right.
The Notes section of the documentation contains an applicable list of restrictions:
The system limits which processes can set the foreground window. A process can set a foreground window only if one of the following conditions is true:
- The process is a foreground process.
- The process was started by the foreground process.
- The process received the last input event.
- There is no foreground process.
- The foreground process is being debugged.
- The foreground is not locked (see
LockSetForegroundWindow ). SPI_GETFOREGROUNDLOCKTIMEOUT (see SPI_GETFOREGROUNDLOCKTIMEOUT in SystemParametersInfo ).- There is no menu.
An application cannot force a window in the foreground while the user is working with another window. Instead, Windows flashes a window taskbar button to notify the user.
The real question is why you should do this at all. Changing the foreground application is likely to cause you problems with all the restrictions that Windows places on it, or with your users. This is a very dangerous action for the user, which is one of the reasons why Windows has tightened its restrictions in recent years.
source share