Minimize a window via its pid

2019-08-30 01:43发布

问题:

My problem is that I want to minimize a window via batch file after window-start.

How to minimize a application-window from cmd? The pid is known. My OS is win7.

回答1:

You can do this with NirCmd:

nircmd win min process /pid

where pid is the process id of the window to minimize.


Source NirCmd Command Reference - win

win [action] [find] [window to find] [Additional Parameters]

...

[action]: The action you want to do on the specified window:

...

min: Minimizes the specified windows.

...

[find]: The method to find the window

...

process: Finds the desired window by specifying process ID (for example: /3412) or process name (for example: firefox.exe).


Disclaimer: I am not affiliated with NirSoft in any way, I am just an end user of the software.



回答2:

Here is a Batch/VBScript solution which requires no additional software:

   @echo Off
   if \{%1\}==\{\} @echo Syntax: MinimizePID PID & goto :EOF
   if exist "%TEMP%\MinimizePID.vbs" del /f %TEMP%\MinimizePID.vbs
   @echo dim  objArguments, pid>"%TEMP%\MinimizePID.vbs"
   @echo Set WshShell = CreateObject("WScript.Shell")>>"%TEMP%\MinimizePID.vbs"
   @echo Set objArguments = Wscript.Arguments>>"%TEMP%\MinimizePID.vbs"
   @echo pid = objArguments(0)>>"%TEMP%\MinimizePID.vbs"
   @echo WshShell.AppActivate pid>>"%TEMP%\MinimizePID.vbs"
   @set "line=%%+ n"
   setlocal EnableDelayedExpansion
   (
     @echo WshShell.SendKeys "!line!">>"%TEMP%\MinimizePID.vbs"
   )
   :doit
   cscript //nologo "%TEMP%\MinimizePID.vbs" %1

Assume pid.txt has the PID of the process whose window you want to minimize. Save this batch file as MinimizePID.bat and invoke it in this way:

for /f %i in (pid.txt) do call MinimizePID %I

The only tricky part here was getting the SendKeys to work to send the Minimize window command. You probably could code this as purely VBScript. Also note this will work only on US Windows - key shortcuts are different in localized versions.

Thanks to How can I maximize, restore, or minimize a window with a vb script? for the approach.



标签: windows cmd