gur111 icon

Windows Window Layout Arranger With Powershell

gur111 | PRO | 04/04/19 08:10:57 AM UTC | 0 ⭐ | 549 👁️ | Never ⏰ | []
PowerShell |

2.35 KB

|

None

|

0 👍

/

0 👎

Add-Type -AssemblyName System.Windows.Forms
 
Add-Type @"
using System;
using System.Collections;
using System.Collections.Generic;
using System.Runtime.InteropServices;
using System.Text;
 
public struct RECT
{
    public int left;
    public int top;
    public int right;
    public int bottom;
}
 
public class pInvoke
{
    [DllImport("user32.dll", SetLastError = true)]
    public static extern bool MoveWindow(IntPtr hWnd, int X, int Y, int nWidth, int nHeight, bool bRepaint);
 
    [DllImport("user32.dll", CharSet = CharSet.Auto, CallingConvention = CallingConvention.StdCall, ExactSpelling = true, SetLastError = true)]
    public static extern bool GetWindowRect(IntPtr hWnd, ref RECT rect);
}
"@
 
function Move-Window([System.IntPtr]$WindowHandle, [switch]$Top, [switch]$Bottom, [switch]$Left, [switch]$Right, [int]$width, [int]$height) {
  # get the window bounds
  $rect = New-Object RECT
  [pInvoke]::GetWindowRect($WindowHandle, [ref]$rect)
  # get which screen the app has been spawned into
  $activeScreen = [System.Windows.Forms.Screen]::FromHandle($WindowHandle).Bounds
 
  
  if($width){}
  else{
    $width = ($activeScreen.Right)/2
  }
  
  if($height){}else{
    $height = ($activeScreen.Bottom)/2
  }
  
  if ($Top) { # if top used, snap to top of screen
    $posY = $activeScreen.Top
  } elseif ($Bottom) { # if bottom used, snap to bottom of screen
    $posY = $activeScreen.Bottom - $height
    $height = $height - 35
  } else { # if neither, snap to current position of the window
    $posY = $rect.top
  }
 
  if ($Left) { # if left used, snap to left of screen
    $posX = $activeScreen.Left
  } elseif ($Right) { # if right used, snap to right of screen
    $posX = $activeScreen.Right - $width
  } else { # if neither, snap to current position of the window
    $posX = $rect.left
  }
  
  [pInvoke]::MoveWindow($WindowHandle, $posX, $posY, $width, $height, $true)
}
 
 
# spawn the window and return the window object
$app1 = Start-Process "C:\Python27\Scripts\rpyc_classic.py" -PassThru
$app2 = Start-Process .\pingMgmt.bat -PassThru
$app3 = Start-Process .\pingOutbound.bat -PassThru
 
 
sleep -Milliseconds 300
Move-Window -WindowHandle $app1.MainWindowHandle -Top -Left -width 1500
 
Move-Window -WindowHandle $app2.MainWindowHandle -Bottom -Left
 
Move-Window -WindowHandle $app3.MainWindowHandle -Bottom -Right

Comments