Created
September 26, 2015 21:32
-
-
Save prasannavl/effd901e2460a651ad2c to your computer and use it in GitHub Desktop.
Set a given window state for any window using WinAPI.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
function Set-WindowState { | |
<# | |
.SYNOPSIS | |
Set a given window state using WinAPI. | |
.DESCRIPTION | |
Use the ShowWindowAsync function to set the Window state for | |
any given window handle or the current powershell process. | |
.EXAMPLE | |
Set-WindowState -State "MINIMIZE" | |
Minimizes the window where the command is executed. | |
.EXAMPLE | |
Set-WindowState -State "MINIMIZE" -MainWindowHandle (ps chrome | select -First 1).Handle | |
Minimizes the window of the given handle. | |
#> | |
param( | |
[Parameter()] | |
[ValidateSet('FORCEMINIMIZE', 'HIDE', 'MAXIMIZE', 'MINIMIZE', 'RESTORE', | |
'SHOW', 'SHOWDEFAULT', 'SHOWMAXIMIZED', 'SHOWMINIMIZED', | |
'SHOWMINNOACTIVE', 'SHOWNA', 'SHOWNOACTIVATE', 'SHOWNORMAL')] | |
$State = 'SHOW', | |
[Parameter()] | |
$MainWindowHandle = (Get-Process -id $pid).MainWindowHandle | |
) | |
$WindowStates = @{ | |
'FORCEMINIMIZE' = 11 | |
'HIDE' = 0 | |
'MAXIMIZE' = 3 | |
'MINIMIZE' = 6 | |
'RESTORE' = 9 | |
'SHOW' = 5 | |
'SHOWDEFAULT' = 10 | |
'SHOWMAXIMIZED' = 3 | |
'SHOWMINIMIZED' = 2 | |
'SHOWMINNOACTIVE' = 7 | |
'SHOWNA' = 8 | |
'SHOWNOACTIVATE' = 4 | |
'SHOWNORMAL' = 1 | |
} | |
$Win32ShowWindowAsync = Add-Type -name "Win32ShowWindowAsync" -namespace Win32Functions -passThru -memberDefinition ' | |
[DllImport("user32.dll")] | |
public static extern bool ShowWindowAsync(IntPtr hWnd, int nCmdShow); | |
' | |
$Win32ShowWindowAsync::ShowWindowAsync($MainWindowHandle, $WindowStates[($State)]) | Out-Null | |
Write-Verbose ("Set Window Style on $MainWindowHandle to $State") | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment