简体   繁体   English

将批处理脚本CMD窗口保留在新打开的CMD窗口之上

[英]Keep batch script CMD window on top of newly opened CMD windows

Probably an easy answer but can't find it online. 可能是一个简单的答案,但无法在网上找到它。

I have a .bat script to open some programs and also wants user input (Y[N]) which it all works fine. 我有一个.bat脚本来打开一些程序,也想要用户输入(Y [N])这一切都正常。

My question. 我的问题。

How can I keep the script window on top of the newly opened windows so the user can see the the script is looking for some input. 如何将脚本窗口保留在新打开的窗口之上,以便用户可以看到脚本正在寻找一些输入。

@ECHO OFF
REM  2016-06-17 GMAN

REM Start Zookeeper
cd zookeeper-3.4.8 
CALL StartZookeeper.bat

REM Give Zookeeper a chance to start
sleep 4

REM Start Kafka
cd ..\kafka_2.11-0.10.0.0 
CALL StartKafka.bat

REM Give Kafka a chance to start
sleep 2

setlocal
:PROMPT
SET /p prodCons="Open a CMD for Producer and Consumer: (Y/[N]"?)
echo You entered: %prodCons%
sleep 1

IF /I "%prodCons%" NEQ "Y" GOTO END

REM Producer Terminal
start cmd.exe /k "TITLE Producer && cd .\kafka_2.11-0.10.0.0\bin\windows"

REM Consumer Terminal
start cmd.exe /k "TITLE Consumer && cd .\kafka_2.11-0.10.0.0\bin\windows"   

:END
endlocal

My script window is hidden behind the new CMD so user can see to enter Y or N. 我的脚本窗口隐藏在新CMD后面,因此用户可以看到输入Y或N.

One way to set the Always On Top flag is to import the SetWindowPos() function from user32.dll and invoke it with arguments which: 设置Always On Top标志的一种方法是从user32.dll导入SetWindowPos()函数并使用以下参数调用它:

  • set the window parent to HWND_TOPMOST (-1) 将父窗口设置为HWND_TOPMOST(-1)
  • skip resizing / moving the window with SWP_NOMOVE (0x01) and SWP_NOSIZE (0x02) flags 使用SWP_NOMOVE(0x01)和SWP_NOSIZE(0x02)标志跳过调整窗口大小/移动窗口

There's no way to do this in pure batch, but you can have PowerShell do the heavy lifting. 在纯批处理中没有办法做到这一点,但是你可以让PowerShell做繁重的工作。 Here's a bat + PowerShell hybrid script template into which you can paste your batch code to make the console always on top: 这是一个bat + PowerShell混合脚本模板,您可以粘贴批处理代码以使控制台始终位于顶部:

<# : AlwaysOnTop.bat -- http://stackoverflow.com/a/37912693/1683264
@echo off & setlocal

rem // Relaunch self in PowerShell to run this window Always On Top
powershell -noprofile "iex (${%~f0} | out-string)"

rem /* ###############################
rem    Your main batch code goes here.
rem    ############################### */

goto :EOF
rem // end batch / begin PowerShell hybrid code #>

# // Solution based on http://stackoverflow.com/a/34703664/1683264
add-type user32_dll @'
    [DllImport("user32.dll")]
    public static extern bool SetWindowPos(IntPtr hWnd, IntPtr hWndInsertAfter,
        int x, int y, int cx, int cy, uint uFlags);
'@ -namespace System

# // Walk up the process tree until we find a window handle
$id = $PID
do {
    $id = (gwmi win32_process -filter "ProcessID='$id'").ParentProcessID
    $hwnd = (ps -id $id).MainWindowHandle
} while (-not $hwnd)

$rootWin = [IntPtr](-1)
$topmostFlags = 0x0003
[void][user32_dll]::SetWindowPos($hwnd, $rootWin, 0, 0, 0, 0, $topmostFlags)

One can also use GetWindowLong() to query extended window styles to determine whether the window is already set always on top -- then toggle it. 也可以使用GetWindowLong()来查询扩展窗口样式,以确定窗口是否已经设置在顶部 - 然后切换它。

<# : AlwaysOnTop2.bat -- http://stackoverflow.com/a/37912693/1683264
@echo off & setlocal

call :toggleAlwaysOnTop

rem /* ###############################
rem    Your main batch code goes here.
rem    ############################### */

goto :EOF

:toggleAlwaysOnTop
powershell -noprofile "iex (${%~f0} | out-string)"
goto :EOF
rem // end batch / begin PowerShell hybrid code #>

add-type user32_dll @'
    [DllImport("user32.dll")]
    public static extern bool SetWindowPos(IntPtr hWnd, IntPtr hWndInsertAfter,
        int x, int y, int cx, int cy, uint uFlags);

    [DllImport("user32.dll")]
    public static extern int GetWindowLong(IntPtr hWnd, int nIndex);
'@ -namespace System

$id = $PID
do {
    $id = (gwmi win32_process -filter "ProcessID='$id'").ParentProcessID
    $hwnd = (ps -id $id).MainWindowHandle
} while (-not $hwnd)

$style = [user32_dll]::GetWindowLong($hwnd, -20)
# // If flag 0x08 is set, make parent HWND -2 to unset it.  Otherwise, HWND -1 to set it.
[IntPtr]$rootWin = ($style -band 0x08) / -8 - 1
[void][user32_dll]::SetWindowPos($hwnd, $rootWin, 0, 0, 0, 0, 0x03)

To clarify I'm trying to make it always on top in the middle of a script, not at the very bottom. 澄清我试图让它始终位于脚本中间,而不是最底层。 Does this change how it may work? 这会改变它的工作方式吗? IE when a user selects a setting in the option menu it switches to Always on top. IE当用户在选项菜单中选择一个设置时,它会切换到Always on top。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM