Advertisement
Cogger

VMRC-Set-Window_x3840.ps1

Sep 22nd, 2021 (edited)
167
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. # Define Funtion to Set Window Size And Position
  2. Function Set-Window {
  3.     <#
  4.     .SYNOPSIS
  5.     Retrieve/Set the window size and coordinates of a process window.
  6.    
  7.     .DESCRIPTION
  8.     Retrieve/Set the size (height,width) and coordinates (x,y)
  9.     of a process window.
  10.    
  11.     .PARAMETER ProcessName
  12.     Name of the process to determine the window characteristics.
  13.     (All processes if omitted).
  14.    
  15.     .PARAMETER Id
  16.     Id of the process to determine the window characteristics.
  17.    
  18.     .PARAMETER X
  19.     Set the position of the window in pixels from the left.
  20.    
  21.     .PARAMETER Y
  22.     Set the position of the window in pixels from the top.
  23.    
  24.     .PARAMETER Width
  25.     Set the width of the window.
  26.    
  27.     .PARAMETER Height
  28.     Set the height of the window.
  29.    
  30.     .PARAMETER Passthru
  31.     Returns the output object of the window.
  32.    
  33.     .NOTES
  34.     Name:   Set-Window
  35.     Author: Boe Prox
  36.     Version History:
  37.         1.0//Boe Prox - 11/24/2015 - Initial build
  38.         1.1//JosefZ   - 19.05.2018 - Treats more process instances
  39.                                      of supplied process name properly
  40.         1.2//JosefZ   - 21.02.2019 - Parameter Id
  41.    
  42.     .OUTPUTS
  43.     None
  44.     System.Management.Automation.PSCustomObject
  45.     System.Object
  46.    
  47.     .EXAMPLE
  48.     Get-Process powershell | Set-Window -X 20 -Y 40 -Passthru -Verbose
  49.     VERBOSE: powershell (Id=11140, Handle=132410)
  50.    
  51.     Id          : 11140
  52.     ProcessName : powershell
  53.     Size        : 1134,781
  54.     TopLeft     : 20,40
  55.     BottomRight : 1154,821
  56.    
  57.     Description: Set the coordinates on the window for the process PowerShell.exe
  58.    
  59.     .EXAMPLE
  60.     $windowArray = Set-Window -Passthru
  61.     WARNING: cmd (1096) is minimized! Coordinates will not be accurate.
  62.    
  63.         PS C:\>$windowArray | Format-Table -AutoSize
  64.    
  65.       Id ProcessName    Size     TopLeft       BottomRight  
  66.       -- -----------    ----     -------       -----------  
  67.     1096 cmd            199,34   -32000,-32000 -31801,-31966
  68.     4088 explorer       1280,50  0,974         1280,1024    
  69.     6880 powershell     1280,974 0,0           1280,974    
  70.    
  71.     Description: Get the coordinates of all visible windows and save them into the
  72.                  $windowArray variable. Then, display them in a table view.
  73.    
  74.     .EXAMPLE
  75.     Set-Window -Id $PID -Passthru | Format-Table
  76.     ​‌‍
  77.       Id ProcessName Size     TopLeft BottomRight
  78.       -- ----------- ----     ------- -----------
  79.     7840 pwsh        1024,638 0,0     1024,638
  80.    
  81.     Description: Display the coordinates of the window for the current
  82.                  PowerShell session in a table view.
  83.    
  84.    
  85.    
  86.     #>
  87.     [cmdletbinding(DefaultParameterSetName='Name')]
  88.     Param (
  89.         [parameter(Mandatory=$False,
  90.             ValueFromPipelineByPropertyName=$True, ParameterSetName='Name')]
  91.         [string]$ProcessName='*',
  92.         [parameter(Mandatory=$True,
  93.             ValueFromPipeline=$False,              ParameterSetName='Id')]
  94.         [int]$Id,
  95.         [int]$X,
  96.         [int]$Y,
  97.         [int]$Width,
  98.         [int]$Height,
  99.         [switch]$Passthru
  100.     )
  101.     Begin {
  102.         Try {
  103.             [void][Window]
  104.         } Catch {
  105.         Add-Type @"
  106.            using System;
  107.            using System.Runtime.InteropServices;
  108.            public class Window {
  109.            [DllImport("user32.dll")]
  110.            [return: MarshalAs(UnmanagedType.Bool)]
  111.            public static extern bool GetWindowRect(
  112.                IntPtr hWnd, out RECT lpRect);
  113.    
  114.            [DllImport("user32.dll")]
  115.            [return: MarshalAs(UnmanagedType.Bool)]
  116.            public extern static bool MoveWindow(
  117.                IntPtr handle, int x, int y, int width, int height, bool redraw);
  118.    
  119.            [DllImport("user32.dll")]
  120.            [return: MarshalAs(UnmanagedType.Bool)]
  121.            public static extern bool ShowWindow(
  122.                IntPtr handle, int state);
  123.            }
  124.            public struct RECT
  125.            {
  126.            public int Left;        // x position of upper-left corner
  127.            public int Top;         // y position of upper-left corner
  128.            public int Right;       // x position of lower-right corner
  129.            public int Bottom;      // y position of lower-right corner
  130.            }
  131. "@
  132.         }
  133.     }
  134.     Process {
  135.         $Rectangle = New-Object RECT
  136.         If ( $PSBoundParameters.ContainsKey('Id') ) {
  137.             $Processes = Get-Process -Id $Id -ErrorAction SilentlyContinue
  138.         } else {
  139.             $Processes = Get-Process -Name "$ProcessName" -ErrorAction SilentlyContinue
  140.         }
  141.         if ( $null -eq $Processes ) {
  142.             If ( $PSBoundParameters['Passthru'] ) {
  143.                 Write-Warning 'No process match criteria specified'
  144.             }
  145.         } else {
  146.             $Processes | ForEach-Object {
  147.                 $Handle = $_.MainWindowHandle
  148.                 Write-Verbose "$($_.ProcessName) `(Id=$($_.Id), Handle=$Handle`)"
  149.                 if ( $Handle -eq [System.IntPtr]::Zero ) { return }
  150.                 $Return = [Window]::GetWindowRect($Handle,[ref]$Rectangle)
  151.                 If (-NOT $PSBoundParameters.ContainsKey('X')) {
  152.                     $X = $Rectangle.Left            
  153.                 }
  154.                 If (-NOT $PSBoundParameters.ContainsKey('Y')) {
  155.                     $Y = $Rectangle.Top
  156.                 }
  157.                 If (-NOT $PSBoundParameters.ContainsKey('Width')) {
  158.                     $Width = $Rectangle.Right - $Rectangle.Left
  159.                 }
  160.                 If (-NOT $PSBoundParameters.ContainsKey('Height')) {
  161.                     $Height = $Rectangle.Bottom - $Rectangle.Top
  162.                 }
  163.                 If ( $Return ) {
  164.                     $Return = [Window]::MoveWindow($Handle, $x, $y, $Width, $Height,$True)
  165.                 }
  166.                 If ( $PSBoundParameters['Passthru'] ) {
  167.                     $Rectangle = New-Object RECT
  168.                     $Return = [Window]::GetWindowRect($Handle,[ref]$Rectangle)
  169.                     If ( $Return ) {
  170.                         $Height      = $Rectangle.Bottom - $Rectangle.Top
  171.                         $Width       = $Rectangle.Right  - $Rectangle.Left
  172.                         $Size        = New-Object System.Management.Automation.Host.Size        -ArgumentList $Width, $Height
  173.                         $TopLeft     = New-Object System.Management.Automation.Host.Coordinates -ArgumentList $Rectangle.Left , $Rectangle.Top
  174.                         $BottomRight = New-Object System.Management.Automation.Host.Coordinates -ArgumentList $Rectangle.Right, $Rectangle.Bottom
  175.                         If ($Rectangle.Top    -lt 0 -AND
  176.                             $Rectangle.Bottom -lt 0 -AND
  177.                             $Rectangle.Left   -lt 0 -AND
  178.                             $Rectangle.Right  -lt 0) {
  179.                             Write-Warning "$($_.ProcessName) `($($_.Id)`) is minimized! Coordinates will not be accurate."
  180.                         }
  181.                         $Object = [PSCustomObject]@{
  182.                             Id          = $_.Id
  183.                             ProcessName = $_.ProcessName
  184.                             Size        = $Size
  185.                             TopLeft     = $TopLeft
  186.                             BottomRight = $BottomRight
  187.                         }
  188.                         $Object
  189.                     }
  190.                 }
  191.             }
  192.         }
  193.     }
  194.     }
  195.  
  196. # Define Function so hide windows that we are not using exactly at this time.
  197. function Set-WindowState {
  198.     <#
  199.     .LINK
  200.     https://gist.github.com/Nora-Ballard/11240204
  201.     #>
  202.  
  203.     [CmdletBinding(DefaultParameterSetName = 'InputObject')]
  204.     param(
  205.         [Parameter(Position = 0, Mandatory = $true, ValueFromPipeline = $true)]
  206.         [Object[]] $InputObject,
  207.  
  208.         [Parameter(Position = 1)]
  209.         [ValidateSet('FORCEMINIMIZE', 'HIDE', 'MAXIMIZE', 'MINIMIZE', 'RESTORE',
  210.                      'SHOW', 'SHOWDEFAULT', 'SHOWMAXIMIZED', 'SHOWMINIMIZED',
  211.                      'SHOWMINNOACTIVE', 'SHOWNA', 'SHOWNOACTIVATE', 'SHOWNORMAL')]
  212.         [string] $State = 'SHOW'
  213.     )
  214.  
  215.     Begin {
  216.         $WindowStates = @{
  217.             'FORCEMINIMIZE'     = 11
  218.             'HIDE'              = 0
  219.             'MAXIMIZE'          = 3
  220.             'MINIMIZE'          = 6
  221.             'RESTORE'           = 9
  222.             'SHOW'              = 5
  223.             'SHOWDEFAULT'       = 10
  224.             'SHOWMAXIMIZED'     = 3
  225.             'SHOWMINIMIZED'     = 2
  226.             'SHOWMINNOACTIVE'   = 7
  227.             'SHOWNA'            = 8
  228.             'SHOWNOACTIVATE'    = 4
  229.             'SHOWNORMAL'        = 1
  230.         }
  231.  
  232.         $Win32ShowWindowAsync = Add-Type -MemberDefinition @'
  233. [DllImport("user32.dll")]
  234. public static extern bool ShowWindowAsync(IntPtr hWnd, int nCmdShow);
  235. '@ -Name "Win32ShowWindowAsync" -Namespace Win32Functions -PassThru
  236.  
  237.         if (!$global:MainWindowHandles) {
  238.             $global:MainWindowHandles = @{ }
  239.         }
  240.     }
  241.  
  242.     Process {
  243.         foreach ($process in $InputObject) {
  244.             if ($process.MainWindowHandle -eq 0) {
  245.                 if ($global:MainWindowHandles.ContainsKey($process.Id)) {
  246.                     $handle = $global:MainWindowHandles[$process.Id]
  247.                 } else {
  248.                     Write-Error "Main Window handle is '0'"
  249.                     continue
  250.                 }
  251.             } else {
  252.                 $handle = $process.MainWindowHandle
  253.                 $global:MainWindowHandles[$process.Id] = $handle
  254.             }
  255.  
  256.             $Win32ShowWindowAsync::ShowWindowAsync($handle, $WindowStates[$State]) | Out-Null
  257.             Write-Verbose ("Set Window State '{1} on '{0}'" -f $MainWindowHandle, $State)
  258.         }
  259.     }
  260. }
  261.  
  262.  
  263. Get-Process vmrc | Set-Window -width 1659 -Height 1010 -Passthru -Verbose
  264.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement