总是放在最前面的批处理脚本 [英] Always on top batch script

查看:62
本文介绍了总是放在最前面的批处理脚本的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我创建了一个简单的批处理脚本,使用以下代码伪锁定计算机:

I have created a simple batch script to pseudo-lock a computer using the following code:

@ECHO OFF & setlocal ENABLEDELAYEDEXPANSION
setlocal EnableDelayedExpansion
color a
TITLE Lock
if not "%1" == "max" (
powershell -command "& { $x = New-Object -ComObject Shell.Application; $x.minimizeall() }"
start /MAX cmd /c %0 max & exit/b
)
:Lock 
echo Please enter a password to lock your computer . . .
powershell -Command $pword = read-host "Enter password" -AsSecureString ; $BSTR=[System.Runtime.InteropServices.Marshal]::SecureStringToBSTR($pword) ; [System.Runtime.InteropServices.Marshal]::PtrToStringAuto($BSTR) > EOFlock.txt & set /p Pass1=<EOFlock.txt & del EOFlock.txt
TITLE Lock
taskkill /IM explorer.exe /F >nul
cls
echo Please type the password to unlock the computer . . .
:Locked 
set /p Pass2=
:Unlock 
if !Pass1! == !Pass2! (goto End)
goto Locked 
:End
start explorer.exe
echo This Computer is unlocked.

我希望此窗口保持在顶部,并且最好在它到达文件末尾之前是无法关闭的.但是,我还没有找到一种方法.

I want this window to stay on top, and preferably be unclosable until it has reached the end of the file. However, I did not find a way to do this yet.

推荐答案

您可以调用PowerShell,而PowerShell至少可以在Windows 8+上调用WinAPI(7可能也可以,以前的版本也不行)

You can call into PowerShell which in turn can call into the WinAPI... at least on Windows 8+ (7 might work too, previous versions probably not).

这是相对简单的:

  1. 致电PowerShell
  2. 告诉它独立于上下文运行
  3. 使用 SetWindowPos 将窗口置于最前面
  4. 使用 GetConsoleWindow 找出要对哪个窗口执行操作
  1. Call PowerShell
  2. Tell it to run independent of context
  3. Use SetWindowPos to bring a window to the front
  4. Use GetConsoleWindow to find out which window to act on

所有内容都非常整齐地放在一个命令中:

It all fits pretty neatly into a single command:

@powershell -ExecutionPolicy UnRestricted -Command "(Add-Type -memberDefinition \"[DllImport(\"\"user32.dll\"\")] public static extern bool SetWindowPos(IntPtr hWnd, IntPtr hWndInsertAfter, int x,int y,int cx, int xy, uint flagsw);\" -name \"Win32SetWindowPos\" -passThru )::SetWindowPos((Add-Type -memberDefinition \"[DllImport(\"\"Kernel32.dll\"\")] public static extern IntPtr GetConsoleWindow();\" -name \"Win32GetConsoleWindow\" -passThru )::GetConsoleWindow(),-1,0,0,0,0,67)"

这篇关于总是放在最前面的批处理脚本的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆