寫一個BAT,按下後即可截取當前活動窗口屏幕,並保存成圖片文件
时间: 2023-08-16 07:09:21 浏览: 39
您可以使用Windows自带的`PrtScn`键来截屏,并使用`Paint`应用程序来保存截屏为图片文件。以下是一个简单的BAT脚本示例:
```batch
@echo off
:: 按下PrtScn键
echo Press the PrtScn key...
pause>nul
:: 启动Paint应用程序
start mspaint.exe
:: 等待Paint启动并获取焦点
timeout /t 2 /nobreak >nul
set "wshShell=CreateObject("WScript.Shell")"
set "paintTitle=Untitled - Paint""
:waitLoop
set "paintPid="
for /f "tokens=2 delims= " %%a in ('tasklist /v /fo list /fi "imagename eq mspaint.exe" /fi "windowtitle eq %paintTitle%" /nh') do set "paintPid=%%a"
if defined paintPid (
for /f "tokens=1,*" %%a in ('tasklist /v /fo csv /fi "pid eq %paintPid%" /nh') do (
set "windowTitle=%%~b"
set "windowTitle=!windowTitle:~1,-1!"
set "windowTitle=!windowTitle:*Paint=Paint!"
if "!windowTitle!"=="%paintTitle%" (
set "appActivateCommand=wshShell.AppActivate(%paintPid%)"
goto activatePaint
)
)
)
goto waitLoop
:activatePaint
:: 获取剪贴板中的截屏
echo Getting screenshot from clipboard...
powershell -command "Add-Type -AssemblyName System.Windows.Forms;[System.Windows.Forms.SendKeys]::SendWait('%{PRTSC}');Start-Sleep -Milliseconds 500;[System.Windows.Forms.SendKeys]::SendWait('^v');Start-Sleep -Milliseconds 500"
:: 保存截屏为PNG格式文件
echo Saving screenshot as PNG file...
powershell -command "Add-Type -AssemblyName System.Windows.Forms;$img=[System.Windows.Forms.Clipboard]::GetImage();$img.Save('screenshot.png',[System.Drawing.Imaging.ImageFormat]::Png)"
:: 关闭Paint应用程序
echo Closing Paint...
taskkill /f /im mspaint.exe >nul
echo Done.
pause>nul
```
这个脚本将会在按下`PrtScn`键后启动`Paint`应用程序,并等待其获取焦点。然后,使用PowerShell脚本从剪贴板中获取截屏,并将其保存为PNG格式文件。最后,关闭`Paint`应用程序。注意,这个脚本需要Windows系统自带的PowerShell支持。