保持批处理脚本CMD窗口上新开的CMD窗口的顶部 [英] Keep batch script CMD window on top of newly opened CMD windows
问题描述
也许一个简单的答案,但不能在网上找到它。
Probably an easy answer but can't find it online.
我有一个蝙蝠脚本打开某些程序,也希望用户输入(Y [N]),这一切工作正常。
I have a .bat script to open some programs and also wants user input (Y[N]) which it all works fine.
我的问题。
如何保持在新开的窗口之上脚本窗口,使用户可以看到该脚本寻找一些输入
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
我的脚本窗口背后却隐藏着新的CMD使用户可以看到输入Y或N
My script window is hidden behind the new CMD so user can see to enter Y or N.
推荐答案
要设置总在最前面标志的一种方法是导入的 SetWindowPos从user32.dll中
()函数并调用它与论据:
One way to set the Always On Top flag is to import the SetWindowPos()
function from user32.dll and invoke it with arguments which:
- 的父窗口设置为HWND_TOPMOST(-1)
- 跳过调整/ SWP_NOMOVE带(0x01)的和SWP_NOSIZE(0X02)标志移动窗口
有没有办法在纯批次做到这一点,但你可以有PowerShell的做繁重。这里有一个蝙蝠+的PowerShell脚本混合模板可以在其中粘贴您的批处理code键使控制台总在最前面:
There's no way to do this in pure batch, but you can have PowerShell do the heavy lifting. Here's a bat + PowerShell hybrid script template into which you can paste your batch code to make the console always on top:
<# : 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)
你也可以使用 GetWindowLong()
查询扩展窗口样式,以确定是否窗口已经始终设置在顶部 - 然后切换它
One can also use GetWindowLong()
to query extended window styles to determine whether the window is already set always on top -- then toggle it.
<# : 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)
这篇关于保持批处理脚本CMD窗口上新开的CMD窗口的顶部的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!