WScript.Shell AppActivate doesn't work every time

前端 未结 1 1704
余生分开走
余生分开走 2020-12-22 04:25

I have a few scripts to help running a program in CMD. The first script that starts the sequence is run on startup. In the process, I\'m running a VBScript with which I\'m t

相关标签:
1条回答
  • 2020-12-22 04:57

    Wscript.sleep will make it worse. You don't want to sleep.

    There are rules. Your program MUST obey one of these rules to be allowed to set the foreground window.

    From https://docs.microsoft.com/en-us/windows/desktop/api/winuser/nf-winuser-setforegroundwindow

    The system restricts which processes can set the foreground window. 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.

    An application cannot force a window to the foreground while the user is working with another window. Instead, Windows flashes the taskbar button of the window to notify the user.

    What this means is you have two seconds (FOREGROUNDLOCKTIMEOUT) from when your script starts to set another window if your script doesn't have a user interface (as it can't be the foreground window). A message box as the first thing you do will comply (till something else happens). Also Wscript has a message box that times out - WshShell.Popup.

    This is to prevent slow starting programs, when the user has got bored and is now working in another program, from then stealing the focus.

    Preventive comment Normally people want to argue with me about this. Complain to Microsoft not me. And you can't argue with rules. You can just comply with them.

    0 讨论(0)
提交回复
热议问题