批处理脚本实现MS Paint图像翻转失败求助
批处理脚本在MS Paint中执行Alt+I后停止的问题修复
问题概述
编写批处理脚本模拟按键Alt+I、Alt+I、F、Enter在Paint中翻转图像,但脚本仅执行到Alt+I就停止,关键代码如下:
powershell -c "Add-Type -AssemblyName System.Windows.Forms; [System.Windows.Forms.SendKeys]::SendWait('%%{i}%')" powershell -c "Add-Type -AssemblyName System.Windows.Forms; [System.Windows.Forms.SendKeys]::SendWait('%%{i}%')" powershell -c "Add-Type -AssemblyName System.Windows.Forms; [System.Windows.Forms.SendKeys]::SendWait('f')"
问题原因
- SendKeys语法错误:代码中
%%{i}%多了末尾的%,正确的Alt+I应该是%%i(批处理中%需要转义为%%,对应SendKeys里的%代表Alt)。 - 窗口焦点丢失:每次调用独立的PowerShell进程,进程切换可能导致Paint窗口失去焦点,后续按键发送到了其他窗口。
- 缺乏延时与窗口激活:打开Paint后未等待窗口加载完成,也未确保Paint处于激活状态,导致菜单命令无法正确响应。
- 分散的PowerShell调用:多次启动PowerShell进程会打断按键序列的连续性,无法保证命令按预期顺序执行。
修正后的完整脚本
@echo off setlocal REM 最小化当前窗口 powershell -c "Add-Type -TypeDefinition 'using System; using System.Runtime.InteropServices; public class WindowState { [DllImport(\"user32.dll\")] public static extern bool ShowWindowAsync(IntPtr hWnd, int nCmdShow); [DllImport(\"user32.dll\")] public static extern IntPtr GetForegroundWindow(); }'; [WindowState]::ShowWindowAsync([WindowState]::GetForegroundWindow(), 2)" REM 截取屏幕 powershell -c "Add-Type -AssemblyName System.Windows.Forms; [System.Windows.Forms.SendKeys]::SendWait('^{PRTSC}')" REM 启动MS Paint并等待窗口加载、激活窗口 start mspaint powershell -c "Start-Sleep -Seconds 2; $paint = Get-Process mspaint | Select-Object -ExpandProperty MainWindowHandle; Add-Type -AssemblyName User32; [User32]::SetForegroundWindow($paint)" REM 粘贴截图 powershell -c "Add-Type -AssemblyName System.Windows.Forms; [System.Windows.Forms.SendKeys]::SendWait('^v'); Start-Sleep -Seconds 1" REM 执行翻转图像的按键序列:Alt+I, Alt+I, F, Enter powershell -c "Add-Type -AssemblyName System.Windows.Forms; [System.Windows.Forms.SendKeys]::SendWait('%%i'); Start-Sleep -Milliseconds 300; [System.Windows.Forms.SendKeys]::SendWait('%%i'); Start-Sleep -Milliseconds 300; [System.Windows.Forms.SendKeys]::SendWait('f'); Start-Sleep -Milliseconds 300; [System.Windows.Forms.SendKeys]::SendWait('{ENTER}'); Start-Sleep -Seconds 1" REM 保存图像 powershell -c "Add-Type -AssemblyName System.Windows.Forms; [System.Windows.Forms.SendKeys]::SendWait('^s'); Start-Sleep -Milliseconds 500; [System.Windows.Forms.SendKeys]::SendWait('{TAB}'); Start-Sleep -Milliseconds 100; [System.Windows.Forms.SendKeys]::SendWait('{TAB}'); Start-Sleep -Milliseconds 100; [System.Windows.Forms.SendKeys]::SendWait('{TAB}'); Start-Sleep -Milliseconds 100; [System.Windows.Forms.SendKeys]::SendWait('{TAB}'); Start-Sleep -Milliseconds 100; [System.Windows.Forms.SendKeys]::SendWait('{TAB}'); Start-Sleep -Milliseconds 100; [System.Windows.Forms.SendKeys]::SendWait(' '); Start-Sleep -Milliseconds 100; [System.Windows.Forms.SendKeys]::SendWait('C:\\Users\\olive\\Pictures'); Start-Sleep -Milliseconds 500; [System.Windows.Forms.SendKeys]::SendWait('{ENTER}'); Start-Sleep -Milliseconds 500; [System.Windows.Forms.SendKeys]::SendWait('{TAB}'); Start-Sleep -Milliseconds 100; [System.Windows.Forms.SendKeys]::SendWait('{TAB}'); Start-Sleep -Milliseconds 100; [System.Windows.Forms.SendKeys]::SendWait('{TAB}'); Start-Sleep -Milliseconds 100; [System.Windows.Forms.SendKeys]::SendWait('{TAB}'); Start-Sleep -Milliseconds 100; [System.Windows.Forms.SendKeys]::SendWait('{TAB}'); Start-Sleep -Milliseconds 100; [System.Windows.Forms.SendKeys]::SendWait('{TAB}'); Start-Sleep -Milliseconds 100; [System.Windows.Forms.SendKeys]::SendWait('{TAB}'); Start-Sleep -Milliseconds 100; [System.Windows.Forms.SendKeys]::SendWait('{TAB}'); Start-Sleep -Milliseconds 100; [System.Windows.Forms.SendKeys]::SendWait('{ENTER}');" timeout /t 10 /nobreak >nul powershell -c "Add-Type -AssemblyName System.Windows.Forms; [System.Windows.Forms.SendKeys]::SendWait('^r')" endlocal pause
关键修复点
- 修正SendKeys语法:将错误的
%%{i}%改为%%i,确保Alt+I正确触发菜单。 - 确保Paint窗口激活:使用
SetForegroundWindow强制将Paint窗口设为前台,保证所有按键都发送到目标窗口。 - 合并PowerShell调用:将连续的按键操作放到同一个PowerShell脚本块中,避免进程切换导致的焦点丢失。
- 添加延时:在菜单操作、窗口切换后添加短延时,确保UI有足够时间响应,避免命令被跳过。
- 简化启动方式:使用
start mspaint直接启动程序,替代原脚本中模拟输入mspaint并回车的繁琐方式。
内容的提问来源于stack exchange,提问作者bob
相关产品推荐
相关产品推荐

