Advertisement
Guest User

Untitled

a guest
Aug 21st, 2019
94
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 6.71 KB | None | 0 0
  1. using namespace System.Collections.Generic; using namespace System.Text
  2.  
  3. Add-Type -TypeDefinition '
  4. using System;
  5. using System.Runtime.InteropServices;
  6. using System.Text;
  7.  
  8. public class WindowTools
  9. {
  10. public delegate bool EnumWindowsProc(IntPtr hWnd, int lParam);
  11.  
  12. [DllImport("User32.dll")]
  13. public static extern bool EnumWindows(EnumWindowsProc enumFunc, int lParam);
  14.  
  15. [DllImport("User32.dll")]
  16. public static extern IntPtr FindWindow(string lpClassName, string lpWindowName);
  17.  
  18. [DllImport("User32.dll")]
  19. public static extern int GetWindowText(IntPtr hWnd, StringBuilder lpString, int nMaxCount);
  20.  
  21. [DllImport("User32.dll")]
  22. public static extern int GetWindowTextLength(IntPtr hWnd);
  23.  
  24. [DllImport("User32.dll")]
  25. public static extern uint GetWindowThreadProcessId(IntPtr hWnd, out uint lpdwProcessId);
  26.  
  27. [DllImport("User32.dll")]
  28. public static extern bool IsWindowVisible(IntPtr hWnd);
  29.  
  30. [DllImport("User32.dll")]
  31. public static extern IntPtr GetShellWindow();
  32.  
  33. [DllImport("User32.dll")]
  34. public static extern bool CloseWindow(IntPtr hWnd);
  35.  
  36. [DllImport("User32.dll")]
  37. public static extern int SendMessage(IntPtr hWnd, uint Msg, uint wParam, uint lParam);
  38. }
  39. '
  40.  
  41. enum WindowMessage {
  42. WM_SYSCOMMAND = 0x0112
  43. }
  44.  
  45. enum WindowCommand {
  46. SC_CLOSE = 0xF060
  47. SC_CONTEXTHELP = 0xF180
  48. SC_DEFAULT = 0xF160
  49. SC_HOTKEY = 0xF150
  50. SC_HSCROLL = 0xF080
  51. SCF_ISSECURE = 0x00000001
  52. SC_KEYMENU = 0xF100
  53. SC_MAXIMIZE = 0xF030
  54. SC_MINIMIZE = 0xF020
  55. SC_MOVE = 0xF010
  56. SC_RESTORE = 0xF120
  57. }
  58.  
  59. class OpenWindow {
  60. [string] $Title
  61. [IntPtr] $Handle
  62. [UInt32] $ProcessId
  63. }
  64.  
  65. function Get-OpenWindow {
  66. [CmdletBinding()]
  67. param (
  68. [String]$Name = '*'
  69. )
  70.  
  71. $handles = [List[IntPtr]]::new()
  72. $shellWindowhWnd = [WindowTools]::GetShellWindow()
  73.  
  74. $null = [WindowTools]::EnumWindows(
  75. {
  76. param (
  77. [IntPtr] $handle,
  78. [int] $lParam
  79. )
  80.  
  81. if ($handle -eq $shellWindowhWnd) {
  82. return $true
  83. }
  84.  
  85. if (-not [WindowTools]::IsWindowVisible($handle)) {
  86. return $true
  87. }
  88.  
  89. $handles.Add($handle)
  90.  
  91. return $true
  92. },
  93. 0
  94. )
  95.  
  96. foreach ($handle in $handles) {
  97. $titleLength = [WindowTools]::GetWindowTextLength($handle)
  98. if ($titleLength -gt 0) {
  99. $titleBuilder = [StringBuilder]::new($titleLength)
  100. $null = [WindowTools]::GetWindowText(
  101. $handle,
  102. $titleBuilder,
  103. $titleLength + 1
  104. )
  105. $title = $titleBuilder.ToString()
  106.  
  107. if ($title -like $Name) {
  108. $processId = 0
  109. $null = [WindowTools]::GetWindowThreadProcessId(
  110. $handle,
  111. [Ref]$processId
  112. )
  113.  
  114. [OpenWindow]@{
  115. Title = $title
  116. Handle = $handle
  117. ProcessId = $processId
  118. }
  119. }
  120. }
  121. }
  122. }
  123.  
  124. function Close-Window {
  125. [CmdletBinding(SupportsShouldProcess, ConfirmImpact = 'High', DefaultParameterSetName = 'ByName')]
  126. param (
  127. [Parameter(Mandatory, Position = 1, ParameterSetName = 'ByName')]
  128. [String]$Name,
  129.  
  130. [Parameter(Mandatory, ValueFromPipeline, ParameterSetName = 'FromInputObject')]
  131. [OpenWindow]$InputObject
  132. )
  133.  
  134. begin {
  135. if ($Name) {
  136. Get-OpenWindow -Name $Name | Close-Window
  137. }
  138. }
  139.  
  140. process {
  141. if ($pscmdlet.ParameterSetName -eq 'FromInputObject') {
  142. if ($pscmdlet.ShouldProcess("Closing window {0}" -f $InputObject.Title)) {
  143. $null = [WindowTools]::SendMessage(
  144. $InputObject.Handle,
  145. [UInt32][WindowMessage]::WM_SYSCOMMAND,
  146. [UINt32][WindowCommand]::SC_CLOSE,
  147. 0
  148. )
  149. }
  150. }
  151. }
  152. }
  153.  
  154. function Minimize-Window {
  155. [CmdletBinding(SupportsShouldProcess, DefaultParameterSetName = 'ByName')]
  156. param (
  157. [Parameter(Mandatory, Position = 1, ParameterSetName = 'ByName')]
  158. [String]$Name,
  159.  
  160. [Parameter(Mandatory, ValueFromPipeline, ParameterSetName = 'FromInputObject')]
  161. [OpenWindow]$InputObject
  162. )
  163.  
  164. begin {
  165. if ($Name) {
  166. Get-OpenWindow -Name $Name | Minimize-Window
  167. }
  168. }
  169.  
  170. process {
  171. if ($pscmdlet.ParameterSetName -eq 'FromInputObject') {
  172. if ($pscmdlet.ShouldProcess("Minimising window {0}" -f $InputObject.Title)) {
  173. $null = [WindowTools]::SendMessage(
  174. $InputObject.Handle,
  175. [UInt32][WindowMessage]::WM_SYSCOMMAND,
  176. [UINt32][WindowCommand]::SC_MINIMIZE,
  177. 0
  178. )
  179. }
  180. }
  181. }
  182. }
  183.  
  184. function Restore-Window {
  185. [CmdletBinding(SupportsShouldProcess, DefaultParameterSetName = 'ByName')]
  186. param (
  187. [Parameter(Mandatory, Position = 1, ParameterSetName = 'ByName')]
  188. [String]$Name,
  189.  
  190. [Parameter(Mandatory, ValueFromPipeline, ParameterSetName = 'FromInputObject')]
  191. [OpenWindow]$InputObject
  192. )
  193.  
  194. begin {
  195. if ($Name) {
  196. Get-OpenWindow -Name $Name | Restore-Window
  197. }
  198. }
  199.  
  200. process {
  201. if ($pscmdlet.ParameterSetName -eq 'FromInputObject') {
  202. if ($pscmdlet.ShouldProcess("Restoring window {0}" -f $InputObject.Title)) {
  203. $null = [WindowTools]::SendMessage(
  204. $InputObject.Handle,
  205. [UInt32][WindowMessage]::WM_SYSCOMMAND,
  206. [UINt32][WindowCommand]::SC_RESTORE,
  207. 0
  208. )
  209. }
  210. }
  211. }
  212. }
  213.  
  214. function Maximize-Window {
  215. [CmdletBinding(SupportsShouldProcess, DefaultParameterSetName = 'ByName')]
  216. param (
  217. [Parameter(Mandatory, Position = 1, ParameterSetName = 'ByName')]
  218. [String]$Name,
  219.  
  220. [Parameter(Mandatory, ValueFromPipeline, ParameterSetName = 'FromInputObject')]
  221. [OpenWindow]$InputObject
  222. )
  223.  
  224. begin {
  225. if ($Name) {
  226. Get-OpenWindow -Name $Name | Maximize-Window
  227. }
  228. }
  229.  
  230. process {
  231. if ($pscmdlet.ParameterSetName -eq 'FromInputObject') {
  232. if ($pscmdlet.ShouldProcess("Restoring window {0}" -f $InputObject.Title)) {
  233. $null = [WindowTools]::SendMessage(
  234. $InputObject.Handle,
  235. [UInt32][WindowMessage]::WM_SYSCOMMAND,
  236. [UINt32][WindowCommand]::SC_MAXIMIZE,
  237. 0
  238. )
  239. }
  240. }
  241. }
  242. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement