Guest User

Untitled

a guest
Dec 3rd, 2022
401
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 5.49 KB | None | 0 0
  1. Function Set-Window {
  2. <#
  3. .SYNOPSIS
  4. Retrieve/Set the window size and coordinates of a process window.
  5.  
  6. .DESCRIPTION
  7. Retrieve/Set the size (height,width) and coordinates (x,y)
  8. of a process window.
  9.  
  10. .PARAMETER ProcessName
  11. Name of the process to determine the window characteristics.
  12. (All processes if omitted).
  13.  
  14. .PARAMETER Id
  15. Id of the process to determine the window characteristics.
  16.  
  17. .PARAMETER X
  18. Set the position of the window in pixels from the left.
  19.  
  20. .PARAMETER Y
  21. Set the position of the window in pixels from the top.
  22.  
  23. .PARAMETER Width
  24. Set the width of the window.
  25.  
  26. .PARAMETER Height
  27. Set the height of the window.
  28.  
  29. .PARAMETER Passthru
  30. Returns the output object of the window.
  31.  
  32. .NOTES
  33. Name: Set-Window
  34. Author: Boe Prox
  35. Version History:
  36. 1.0//Boe Prox - 11/24/2015 - Initial build
  37. 1.1//JosefZ - 19.05.2018 - Treats more process instances
  38. of supplied process name properly
  39. 1.2//JosefZ - 21.02.2019 - Parameter Id
  40. #>
  41. [cmdletbinding(DefaultParameterSetName='Name')]
  42. Param (
  43. [parameter(Mandatory=$False,
  44. ValueFromPipelineByPropertyName=$True, ParameterSetName='Name')]
  45. [string]$ProcessName='*',
  46. [parameter(Mandatory=$True,
  47. ValueFromPipeline=$False, ParameterSetName='Id')]
  48. [int]$Id,
  49. [int]$X,
  50. [int]$Y,
  51. [int]$Width,
  52. [int]$Height,
  53. [switch]$Passthru
  54. )
  55. Begin {
  56. Try {
  57. [void][Window]
  58. } Catch {
  59. Add-Type @"
  60. using System;
  61. using System.Runtime.InteropServices;
  62. public class Window {
  63. [DllImport("user32.dll")]
  64. [return: MarshalAs(UnmanagedType.Bool)]
  65. public static extern bool GetWindowRect(
  66. IntPtr hWnd, out RECT lpRect);
  67.  
  68. [DllImport("user32.dll")]
  69. [return: MarshalAs(UnmanagedType.Bool)]
  70. public extern static bool MoveWindow(
  71. IntPtr handle, int x, int y, int width, int height, bool redraw);
  72.  
  73. [DllImport("user32.dll")]
  74. [return: MarshalAs(UnmanagedType.Bool)]
  75. public static extern bool ShowWindow(
  76. IntPtr handle, int state);
  77. }
  78. public struct RECT
  79. {
  80. public int Left; // x position of upper-left corner
  81. public int Top; // y position of upper-left corner
  82. public int Right; // x position of lower-right corner
  83. public int Bottom; // y position of lower-right corner
  84. }
  85. "@
  86. }
  87. }
  88. Process {
  89. $Rectangle = New-Object RECT
  90. If ( $PSBoundParameters.ContainsKey('Id') ) {
  91. $Processes = Get-Process -Id $Id -ErrorAction SilentlyContinue
  92. } else {
  93. $Processes = Get-Process -Name "$ProcessName" -ErrorAction SilentlyContinue
  94. }
  95. if ( $null -eq $Processes ) {
  96. If ( $PSBoundParameters['Passthru'] ) {
  97. Write-Warning 'No process match criteria specified'
  98. }
  99. } else {
  100. $Processes | ForEach-Object {
  101. $Handle = $_.MainWindowHandle
  102. Write-Verbose "$($_.ProcessName) `(Id=$($_.Id), Handle=$Handle`)"
  103. if ( $Handle -eq [System.IntPtr]::Zero ) { return }
  104. $Return = [Window]::GetWindowRect($Handle,[ref]$Rectangle)
  105. If (-NOT $PSBoundParameters.ContainsKey('X')) {
  106. $X = $Rectangle.Left
  107. }
  108. If (-NOT $PSBoundParameters.ContainsKey('Y')) {
  109. $Y = $Rectangle.Top
  110. }
  111. If (-NOT $PSBoundParameters.ContainsKey('Width')) {
  112. $Width = $Rectangle.Right - $Rectangle.Left
  113. }
  114. If (-NOT $PSBoundParameters.ContainsKey('Height')) {
  115. $Height = $Rectangle.Bottom - $Rectangle.Top
  116. }
  117. If ( $Return ) {
  118. $Return = [Window]::MoveWindow($Handle, $x, $y, $Width, $Height,$True)
  119. }
  120. If ( $PSBoundParameters['Passthru'] ) {
  121. $Rectangle = New-Object RECT
  122. $Return = [Window]::GetWindowRect($Handle,[ref]$Rectangle)
  123. If ( $Return ) {
  124. $Height = $Rectangle.Bottom - $Rectangle.Top
  125. $Width = $Rectangle.Right - $Rectangle.Left
  126. $Size = New-Object System.Management.Automation.Host.Size -ArgumentList $Width, $Height
  127. $TopLeft = New-Object System.Management.Automation.Host.Coordinates -ArgumentList $Rectangle.Left , $Rectangle.Top
  128. $BottomRight = New-Object System.Management.Automation.Host.Coordinates -ArgumentList $Rectangle.Right, $Rectangle.Bottom
  129. If ($Rectangle.Top -lt 0 -AND
  130. $Rectangle.Bottom -lt 0 -AND
  131. $Rectangle.Left -lt 0 -AND
  132. $Rectangle.Right -lt 0) {
  133. Write-Warning "$($_.ProcessName) `($($_.Id)`) is minimized! Coordinates will not be accurate."
  134. }
  135. $Object = [PSCustomObject]@{
  136. Id = $_.Id
  137. ProcessName = $_.ProcessName
  138. Size = $Size
  139. TopLeft = $TopLeft
  140. BottomRight = $BottomRight
  141. }
  142. $Object
  143. }
  144. }
  145. }
  146. }
  147. }
  148. }
  149.  
  150. if ($env:PROC_ID) {
  151. Set-Window -ID $env:PROC_ID -Width $env:colss -Height $env:lines
  152. exit
  153. }
  154. if ($env:terminalFound) {
  155. Set-Window -ProcessName windowsterminal -Width $env:colss -Height $env:lines
  156. exit
  157. }
Advertisement
Add Comment
Please, Sign In to add comment