Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- using namespace System.Collections.Generic; using namespace System.Text
- Add-Type -TypeDefinition '
- using System;
- using System.Runtime.InteropServices;
- using System.Text;
- public class WindowTools
- {
- public delegate bool EnumWindowsProc(IntPtr hWnd, int lParam);
- [DllImport("User32.dll")]
- public static extern bool EnumWindows(EnumWindowsProc enumFunc, int lParam);
- [DllImport("User32.dll")]
- public static extern IntPtr FindWindow(string lpClassName, string lpWindowName);
- [DllImport("User32.dll")]
- public static extern int GetWindowText(IntPtr hWnd, StringBuilder lpString, int nMaxCount);
- [DllImport("User32.dll")]
- public static extern int GetWindowTextLength(IntPtr hWnd);
- [DllImport("User32.dll")]
- public static extern uint GetWindowThreadProcessId(IntPtr hWnd, out uint lpdwProcessId);
- [DllImport("User32.dll")]
- public static extern bool IsWindowVisible(IntPtr hWnd);
- [DllImport("User32.dll")]
- public static extern IntPtr GetShellWindow();
- [DllImport("User32.dll")]
- public static extern bool CloseWindow(IntPtr hWnd);
- [DllImport("User32.dll")]
- public static extern int SendMessage(IntPtr hWnd, uint Msg, uint wParam, uint lParam);
- }
- '
- enum WindowMessage {
- WM_SYSCOMMAND = 0x0112
- }
- enum WindowCommand {
- SC_CLOSE = 0xF060
- SC_CONTEXTHELP = 0xF180
- SC_DEFAULT = 0xF160
- SC_HOTKEY = 0xF150
- SC_HSCROLL = 0xF080
- SCF_ISSECURE = 0x00000001
- SC_KEYMENU = 0xF100
- SC_MAXIMIZE = 0xF030
- SC_MINIMIZE = 0xF020
- SC_MOVE = 0xF010
- SC_RESTORE = 0xF120
- }
- class OpenWindow {
- [string] $Title
- [IntPtr] $Handle
- [UInt32] $ProcessId
- }
- function Get-OpenWindow {
- [CmdletBinding()]
- param (
- [String]$Name = '*'
- )
- $handles = [List[IntPtr]]::new()
- $shellWindowhWnd = [WindowTools]::GetShellWindow()
- $null = [WindowTools]::EnumWindows(
- {
- param (
- [IntPtr] $handle,
- [int] $lParam
- )
- if ($handle -eq $shellWindowhWnd) {
- return $true
- }
- if (-not [WindowTools]::IsWindowVisible($handle)) {
- return $true
- }
- $handles.Add($handle)
- return $true
- },
- 0
- )
- foreach ($handle in $handles) {
- $titleLength = [WindowTools]::GetWindowTextLength($handle)
- if ($titleLength -gt 0) {
- $titleBuilder = [StringBuilder]::new($titleLength)
- $null = [WindowTools]::GetWindowText(
- $handle,
- $titleBuilder,
- $titleLength + 1
- )
- $title = $titleBuilder.ToString()
- if ($title -like $Name) {
- $processId = 0
- $null = [WindowTools]::GetWindowThreadProcessId(
- $handle,
- [Ref]$processId
- )
- [OpenWindow]@{
- Title = $title
- Handle = $handle
- ProcessId = $processId
- }
- }
- }
- }
- }
- function Close-Window {
- [CmdletBinding(SupportsShouldProcess, ConfirmImpact = 'High', DefaultParameterSetName = 'ByName')]
- param (
- [Parameter(Mandatory, Position = 1, ParameterSetName = 'ByName')]
- [String]$Name,
- [Parameter(Mandatory, ValueFromPipeline, ParameterSetName = 'FromInputObject')]
- [OpenWindow]$InputObject
- )
- begin {
- if ($Name) {
- Get-OpenWindow -Name $Name | Close-Window
- }
- }
- process {
- if ($pscmdlet.ParameterSetName -eq 'FromInputObject') {
- if ($pscmdlet.ShouldProcess("Closing window {0}" -f $InputObject.Title)) {
- $null = [WindowTools]::SendMessage(
- $InputObject.Handle,
- [UInt32][WindowMessage]::WM_SYSCOMMAND,
- [UINt32][WindowCommand]::SC_CLOSE,
- 0
- )
- }
- }
- }
- }
- function Minimize-Window {
- [CmdletBinding(SupportsShouldProcess, DefaultParameterSetName = 'ByName')]
- param (
- [Parameter(Mandatory, Position = 1, ParameterSetName = 'ByName')]
- [String]$Name,
- [Parameter(Mandatory, ValueFromPipeline, ParameterSetName = 'FromInputObject')]
- [OpenWindow]$InputObject
- )
- begin {
- if ($Name) {
- Get-OpenWindow -Name $Name | Minimize-Window
- }
- }
- process {
- if ($pscmdlet.ParameterSetName -eq 'FromInputObject') {
- if ($pscmdlet.ShouldProcess("Minimising window {0}" -f $InputObject.Title)) {
- $null = [WindowTools]::SendMessage(
- $InputObject.Handle,
- [UInt32][WindowMessage]::WM_SYSCOMMAND,
- [UINt32][WindowCommand]::SC_MINIMIZE,
- 0
- )
- }
- }
- }
- }
- function Restore-Window {
- [CmdletBinding(SupportsShouldProcess, DefaultParameterSetName = 'ByName')]
- param (
- [Parameter(Mandatory, Position = 1, ParameterSetName = 'ByName')]
- [String]$Name,
- [Parameter(Mandatory, ValueFromPipeline, ParameterSetName = 'FromInputObject')]
- [OpenWindow]$InputObject
- )
- begin {
- if ($Name) {
- Get-OpenWindow -Name $Name | Restore-Window
- }
- }
- process {
- if ($pscmdlet.ParameterSetName -eq 'FromInputObject') {
- if ($pscmdlet.ShouldProcess("Restoring window {0}" -f $InputObject.Title)) {
- $null = [WindowTools]::SendMessage(
- $InputObject.Handle,
- [UInt32][WindowMessage]::WM_SYSCOMMAND,
- [UINt32][WindowCommand]::SC_RESTORE,
- 0
- )
- }
- }
- }
- }
- function Maximize-Window {
- [CmdletBinding(SupportsShouldProcess, DefaultParameterSetName = 'ByName')]
- param (
- [Parameter(Mandatory, Position = 1, ParameterSetName = 'ByName')]
- [String]$Name,
- [Parameter(Mandatory, ValueFromPipeline, ParameterSetName = 'FromInputObject')]
- [OpenWindow]$InputObject
- )
- begin {
- if ($Name) {
- Get-OpenWindow -Name $Name | Maximize-Window
- }
- }
- process {
- if ($pscmdlet.ParameterSetName -eq 'FromInputObject') {
- if ($pscmdlet.ShouldProcess("Restoring window {0}" -f $InputObject.Title)) {
- $null = [WindowTools]::SendMessage(
- $InputObject.Handle,
- [UInt32][WindowMessage]::WM_SYSCOMMAND,
- [UINt32][WindowCommand]::SC_MAXIMIZE,
- 0
- )
- }
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement