batch file - How to close window with .bat script? -
i have ran slight problem while configuring keybinds mouse.
the thing want achieve open program backround(open or closed window) , close again, not kill task(eqivalent x-button, keeps running in backround)
i have set mouse start application called (which able of fast file system searches)
i have figured out code:
taskkill /f /im everything.exe
which can run macro key can kill application. dont want kill application window, becase takes while index important files once have restart it.
so heres question there way bring window/task front of screen or trigger x button event, without having kill entire process ?
also mouse(g600) supports lua scripting, proabbly more harder way go it.
minimizing window can done activating window (bringing foreground) using sendkeys()
send alt+space, whatever letter locale has underlined "minimize" (n in english windows). here's one of possibly dozens of examples of using vbscript.
that sequence overused, inelegant, , boring in opinion -- , doesn't work windows (the cmd console, example). possible minimize window without bringing foreground, , without simulating key presses. 1 needs import function user32.dll. , long we're going through trouble, might import second function detect current window state can toggle minimized / restored.
we can import functions using powershell this. save batch / powershell hybrid script .bat extension , give run.
<# : minimize.bat :: toggles minimized state of window filename :: minimize.bat /? usage :: https://stackoverflow.com/a/34834953/1683264 @echo off & setlocal if "%~1"=="" goto usage set "prog=%~n1" tasklist | findstr /i "\<%prog%\>" >nul || goto usage set /p "=toggling minimized state of %prog%... " <nul powershell -noprofile -noninteractive "iex ((gc \"%~f0\") -join \"`n\")" goto :eof :usage echo syntax: %~nx0 progname[.exe] echo; echo if program visible, minimize it. if minimized, restore it. goto :eof :: end batch / begin powershell hybrid chimera #> add-type user32_dll @' [dllimport("user32.dll")] public static extern bool showwindowasync(intptr hwnd, int ncmdshow); [dllimport("user32.dll")] public static extern int getwindowlong(intptr hwnd, int nindex); '@ -namespace system $hwnd = @(get-process $env:prog)[0].mainwindowhandle $state = [user32_dll]::getwindowlong($hwnd, -16) # mask of 0x20000000 = minimized; 2 = minimize; 4 = restore if ($state -band 0x20000000) { $action = 4 } else { $action = 2 } if ([user32_dll]::showwindowasync($hwnd, $action)) { write-host "success" -f green } else { write-host "fail" -f red }
now, if program minimizes systray, previous script fine minimizing, won't able find window handle when minimized. fail when trying restore. finding hwnd in situation tricky. if 1 knows window title, 1 can find hwnd using user32.dll findwindow()
or findwindowex()
functions.
i haven't had luck using wmi or built-in powershell commands finding title of window minimized tray. neither get-process
nor gwmi win32_process
nor [diagnostics.process]::getprocessbyname()
yielded success. did, however, discover final line of tasklist /v /fo list
show window title. that's enough invoke findwindow()
, hwnd.
this might more complicated needs be, again might not. know found 1,000 ways fail, 1 way succeed. thanks jpblanc helping me figure out how pass null arguments findwindow()
, findwindowex()
.
<# : minimize.bat :: toggles minimized state of window filename :: minimize.bat /? usage @echo off & setlocal if "%~1"=="" goto usage set "prog=%~n1" tasklist | findstr /i "\<%prog%\>" >nul || goto usage set /p "=toggling minimized state of %prog%... " <nul powershell -noprofile -noninteractive "iex ((gc \"%~f0\") -join \"`n\")" goto :eof :usage echo syntax: %~nx0 progname[.exe] echo; echo if program visible, minimize it. if minimized, restore it. goto :eof end batch / begin powershell hybrid chimera #> add-type user32_dll @' [dllimport("user32.dll")] public static extern bool showwindowasync(intptr hwnd, int ncmdshow); [dllimport("user32.dll")] public static extern int getwindowlong(intptr hwnd, int nindex); [dllimport("user32.dll")] public static extern intptr findwindow(intptr lpclassname, string lpwindowname); [dllimport("user32.dll")] public static extern intptr findwindowex(intptr parenthandle, intptr childafter, intptr lclassname, string windowtitle); '@ -namespace system $hwnd = (ps $env:prog)[0].mainwindowhandle if ($hwnd -eq 0) { tasklist /v /fi "imagename eq $env:prog*" /fo list | %{ $title = $_ -replace '^[^:]+:\s+' } $zero = [intptr]::zero $hwnd = [user32_dll]::findwindow($zero, $title) if ($hwnd -eq 0) { $hwnd = [user32_dll]::findwindowex($zero, $zero, $zero, $title) } } $state = [user32_dll]::getwindowlong($hwnd, -16) # mask of 0x20000000 = minimized; 2 = minimize; 4 = restore if ($state -band 0x20000000) { $action = 4 } else { $action = 2 } if ([user32_dll]::showwindowasync($hwnd, $action)) { write-host "success" -f green } else { write-host "fail" -f red }
Comments
Post a Comment