-
Notifications
You must be signed in to change notification settings - Fork 1
/
focus_window.bat
36 lines (29 loc) · 1.05 KB
/
focus_window.bat
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
<# :
@echo off
powershell /nologo /noprofile /command ^
"&{[ScriptBlock]::Create((cat """%~f0""") -join [Char]10).Invoke(@(&{$args}%1))}"
exit /b
#>
{
param(
[Parameter(Mandatory=$true)]
[ValidateNotNullOrEmpty()]
[String]$ClassName
)
Add-Type -AssemblyName ($a = 'System.Windows.Forms')
$FindWindow = ($unm = ($asm = [AppDomain]::CurrentDomain.GetAssemblies() | ? {
$_.ManifestModule.ScopeName.Equals("$a.dll")
}).GetType("$a.UnsafeNativeMethods")).GetMethod('FindWindow')
$SetForegroundWindow = $unm.GetMethod('SetForegroundWindow')
$ShowWindow = $asm.GetType("$a.SafeNativeMethods").GetMethod('ShowWindow')
$SW_SHOW = 5
if (($ptr = $FindWindow.Invoke($null, @($ClassName, $null))) -eq [IntPtr]::Zero) {
"Could not find $($ClassName) window.`n"
return
}
[Runtime.InteropServices.HandleRef]$href = New-Object Runtime.InteropServices.HandleRef(
(New-Object IntPtr), $ptr
)
[void]$SetForegroundWindow.Invoke($null, @($href))
[void]$ShowWindow.Invoke($null, @($href, $SW_SHOW))
}.Invoke($args)