【问题标题】:Keeping batch console on the screen as active window将屏幕上的批处理控制台作为活动窗口
【发布时间】:2016-04-29 07:29:33
【问题描述】:

我想要实现的是在后台启动一些目录(文件夹)并将批处理控制台保持在屏幕前面作为活动窗口。 例如,我有一个带有选择列表的批处理,当我按下 1) 分配的目录在屏幕上打开并且批处理控制台作为活动窗口保持在顶部,因此我可以选择其他操作来启动并继续...批处理控制台需要保留在顶部,所以我可以先打开几个操作,关闭批处理文件并开始在打开的窗口上工作。

希望清楚:)

选择列表很长,所以下面是一部分。

@echo off  
:List  
cls  
echo select from list  
echo.  
echo 1) Postage  
echo 2) Documents  
echo 3) EXIT  
echo.  
set /p option=select:  
if %option%==1 goto option1  
if %option%==2 goto documents  
:option1  
Start d:\Postage  
goto List  
pause  
:option2  
Start d:\documents  
goto List  
pause

例如,我选择 1),按下回车键,文件夹正在打开,我仍然希望批处理控制台选择为活动状态,因此可以对 2) 执行相同的操作 - 通常文件夹 1) 打开并设置为活动窗口,所以我必须通过鼠标单击或 alt+tab 返回批处理控制台。

谢谢!

【问题讨论】:

  • 如果你使用start /min,你的cmd窗口会保持焦点。您的程序(资源管理器窗口)将打开,但将被最小化(“在后台”)。不过,不确定这是否比您目前的情况更好。
  • @WesLarson:不。在start /min 命令打开资源管理器 后,新窗口开始最小化,但原来的cmd 窗口失去了焦点。你测试了吗?
  • @Aacini 是的,我测试了它,它可以与其他应用程序(记事本、iexplore)一起使用。但显然它与 Windows 资源管理器的行为不同。奇数。

标签: batch-file


【解决方案1】:

以下是此任务的注释批处理代码,基于提供的代码在 Windows XP SP3 x86 上按预期工作,但在 Windows 10 x64 上不正常:

@if (@X)==(@Y) @end /* JScript comment
    @echo off

    rem A window title is needed so the appActivate function is
    rem able to recognize the console window of this batch file.
    title Open Folders

    setlocal EnableExtensions EnableDelayedExpansion

    :List
    cls
    echo Select from list.
    echo.
    echo 1) Postage
    echo 2) Documents
    echo 3) EXIT
    echo.
    set "Option="
    set /p "Option=Enter the number: "

    rem Prompt user once more if nothing entered.
    if "!Option!" == "" goto List

    rem Prompt user once more if entered string is not a positive
    rem integer because it contains characters not being a digit.
    for /F "delims=0123456789" %%N in ("!Option!") do goto List

    rem Trim all leading zeros from number.
    set "Number="
    for /f "tokens=* delims=0" %%N in ("!Option!") do set "Number=%%N"

    rem Prompt user once more if entered string is zero.
    if "%Number%" == "" goto List

    rem Prompt user once more if entered number is too large.
    if %Number% GTR 3 goto List

    rem Jump to appropriate option if not being the exit option.
    if %Number% LSS 3 goto Opt%Number%
    endlocal
    exit /B

    :Opt1
    call :OpenDirectory "D:\Postage"
    goto List

    :Opt2
    call :OpenDirectory "D:\documents"
    goto List


    rem This is a batch subroutine used for opening a folder with
    rem Windows Explorer and getting the command prompt window
    rem again back to top of all windows for next user input.

    :OpenDirectory
    if "%~1" == "" (
        %SystemRoot%\explorer.exe
    ) else (
        rem start "" "%~1"
        %SystemRoot%\explorer.exe /e,"%~1"
    )

    rem The jscript part with the appActivate is called now
    rem to make the command window again the top most window.
    %SystemRoot%\System32\cscript.exe //E:JScript //nologo "%~f0"

    rem This GOTO command ends the subroutine and results in continuing
    rem batch processing on the line below the line called this routine.
    goto :EOF

@if (@X)==(@Y) @end JScript comment */

var sh=new ActiveXObject("WScript.Shell");
WScript.Echo(sh.AppActivate("Open Folders"));

最困难的代码部分 - 打开文件夹后返回到所有窗口顶部后获取命令提示符窗口 - 复制自 Bat file on top of all windows 稍作修改。谢谢npocmaka。

但此方法不适用于 Windows 10 x64。批处理命令窗口的选项卡只是在 Windows 任务栏上闪烁,表示该窗口需要用户注意,但并未再次完全变为活动窗口。所以批处理用户必须单击批处理窗口的选项卡或批处理窗口本身才能使其再次成为活动窗口。

但是,我添加了代码来验证这一点

  1. 批处理用户输入了一些内容,
  2. 批处理用户输入了一个真正为正数的字符串,
  3. 删除前导零并验证输入的数字是否不为零,
  4. 验证输入的数字是否不大于 EXIT 选项数字。

如果小于 EXIT 选项编号,则可以轻松跳转到与该编号匹配的选项的代码块。

打开文件夹的代码被放入子例程中,因为很可能需要多次。

也可以将文件夹路径分配给环境变量,跳转到通过环境变量打开文件夹的代码块,调用脚本并使用 GOTO 跳转回@987654326 @。

请参阅KB314853,了解自 Windows 95 以来所有 Windows 的 Windows 资源管理器命令行选项。

当然也可以使用start "" "%~1" 代替命令行启动窗口资源管理器的参数来打开指定的文件夹并显示树。 "" 在打开包含空格或其他字符的文件夹时是必需的,这需要在文件夹路径周围使用双引号。命令start "%~1" 将导致打开一个新的命令提示符窗口,其中文件夹路径作为窗口标题。因此"" 用于指定一个空的窗口标题。

要了解所使用的命令及其工作原理,请打开命令提示符窗口,在其中执行以下命令,并仔细阅读每个命令显示的所有帮助页面。

  • call /?
  • cls /?
  • echo /?
  • endlocal /?
  • exit /?
  • for /?
  • goto /?
  • if /?
  • rem /?
  • set /?
  • setlocal /?
  • title /?

Wes Larson 提出了另一种方法:

@echo off

rem A window title for better identifying what this cmd window is for.
title Open Folders

setlocal EnableExtensions EnableDelayedExpansion

:List
cls
echo Select from list.
echo.
echo 1) Postage
echo 2) Documents
echo 3) EXIT
echo.
set "Option="
set /p "Option=Enter the number: "

rem Prompt user once more if nothing entered.
if "!Option!" == "" goto List

rem Prompt user once more if entered string is not a positive
rem integer because it contains characters not being a digit.
for /F "delims=0123456789" %%N in ("!Option!") do goto List

rem Trim all leading zeros from number.
set "Number="
for /f "tokens=* delims=0" %%N in ("!Option!") do set "Number=%%N"

rem Prompt user once more if entered string is zero.
if "%Number%" == "" goto List

rem Prompt user once more if entered number is too large.
if %Number% GTR 3 goto List

rem Jump to appropriate option if not being the exit option.
if %Number% LSS 3 goto Opt%Number%
endlocal
exit /B

:Opt1
start "" /min "D:\Postage"
goto List

:Opt2
start "" /min "D:\documents"
goto List

但这仅适用于打开文件夹,因为文件夹窗口是最小化打开的,但仍然是活动窗口。所以批处理用户必须点击仍然在顶部可见的批处理命令处理窗口,因为它不再具有输入焦点。

在启动任何其他 GUI 应用程序时,命令提示符窗口也不再是活动窗口。例如,使用命令行启动 Windows 资源管理器也会导致失去输入焦点,即使使用

start "" /min %SystemRoot%\explorer.exe /e,"%~1"

但是如果批处理文件只是用于打开文件夹,并且批处理用户在输入数字后单击批处理窗口是可以的,那么第二个解决方案肯定比第一个解决方案更好。

【讨论】:

  • Mmmm... 这些方法中没有一种适用于此处(Windows 8.1 64 位)。第一个代码打开文件夹,但新文件夹窗口保持焦点。在第二个代码中,新文件夹窗口在任务栏中最小化,但原来的 cmd.exe 窗口失去了焦点(顺便说一句,这是start 命令的标准行为)。
  • Aacini 是对的。两个批处理文件都不能按预期在所有 Windows 上运行。第一批文件在 Windows XP SP3 x86 上完美运行。但是 Windows 10 x64 上的行为绝对不同。批处理窗口的选项卡只是在第一个批处理代码的 Windows 任务栏上闪烁。我已经相应地更新了答案。因此,Windows 8/8.1/10 的任务没有解决,而且很可能 Windows 7/Vista 也没有。
  • 感谢你们的所有努力。似乎第一种方法有效(Win x64)但仅适用于选项 1),文件夹出现在后台并且焦点返回批处理窗口,选项 2)打开文件夹但焦点在它上面,而不是返回批处理窗口。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2021-02-17
  • 2012-08-20
  • 2012-06-06
  • 1970-01-01
相关资源
最近更新 更多