windows 聚焦批处理启动的应用程序

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/8266840/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-15 18:31:38  来源:igfitidea点击:

Focus a batch-started application

windowsbatch-filewindows-xpfocuswindow

提问by Milla Well

I am running a sequence of applications from a batch scriptand I want to make sure, that the opened program will always be in focus.
We need to ensure this because it's an experimental setup and we want to minimise such hassles as having to change focus to a fullscreen window.

我正在从批处理脚本运行一系列应用程序,我想确保打开的程序将始终处于焦点。
我们需要确保这一点,因为这是一个实验设置,我们希望尽量减少必须将焦点更改为全屏窗口等麻烦。

This problem already occurred infrequently when the earlier program exits and for a moment the desktop is visible and the user clicks on some icon on the desktop, and right after that, the next program in the sequence is being processed, the new window is not in focus.

这个问题已经很少发生了,当早期的程序退出时,有一段时间桌面可见,用户点击桌面上的某个图标,紧接着正在处理序列中的下一个程序,新窗口不在重点。

The problem has become much more frequent now that I hid the command window from view.

由于我隐藏了命令窗口,因此问题变得更加频繁。

Any way to force focus for the next program in the sequence, be it a batch command, some settings for the OS (we're on Win XP) or a helper app could be helpful.

任何强制聚焦序列中下一个程序的方法,无论是批处理命令、操作系统的某些设置(我们使用的是 Win XP)还是辅助应用程序都可能会有所帮助。

回答by Yaron

If you want to focus another program you can do this.

如果你想关注另一个程序,你可以这样做。

call focus WindowTitle
exit /b

:focus
setlocal EnableDelayedExpansion 

    if ["%~1"] equ [""] (
        echo Please give the window's title.
        exit /b
    )

    set pr=%~1
    set pr=!pr:"=!

    echo CreateObject("wscript.shell").appactivate "!pr!" > "%tmp%\focus.vbs"
    call "%tmp%\focus.vbs"
    del "%tmp%\focus.vbs"

goto :eof 
endlocal 

I am using vbscript to focus the application. You need to pass the window's title, notthe window's name(whatever.bat). To make sure you get the right window focused you can set its title. example:

我正在使用 vbscript 来聚焦应用程序。您需要传递窗口的标题而不是窗口的名称(whatever.bat)。为了确保您获得正确的窗口焦点,您可以设置其标题。例子:

title WindowTitle

回答by denolk

if i got it right, start /f yourapp.exewould start the application in foreground.

如果我做对了,start /f yourapp.exe将在前台启动应用程序。