Advertisement
gur111

Windows Window Layout Arranger With Powershell

Apr 4th, 2019
241
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. Add-Type -AssemblyName System.Windows.Forms
  2.  
  3. Add-Type @"
  4. using System;
  5. using System.Collections;
  6. using System.Collections.Generic;
  7. using System.Runtime.InteropServices;
  8. using System.Text;
  9.  
  10. public struct RECT
  11. {
  12.    public int left;
  13.    public int top;
  14.    public int right;
  15.    public int bottom;
  16. }
  17.  
  18. public class pInvoke
  19. {
  20.    [DllImport("user32.dll", SetLastError = true)]
  21.    public static extern bool MoveWindow(IntPtr hWnd, int X, int Y, int nWidth, int nHeight, bool bRepaint);
  22.  
  23.    [DllImport("user32.dll", CharSet = CharSet.Auto, CallingConvention = CallingConvention.StdCall, ExactSpelling = true, SetLastError = true)]
  24.    public static extern bool GetWindowRect(IntPtr hWnd, ref RECT rect);
  25. }
  26. "@
  27.  
  28. function Move-Window([System.IntPtr]$WindowHandle, [switch]$Top, [switch]$Bottom, [switch]$Left, [switch]$Right, [int]$width, [int]$height) {
  29.   # get the window bounds
  30.   $rect = New-Object RECT
  31.   [pInvoke]::GetWindowRect($WindowHandle, [ref]$rect)
  32.   # get which screen the app has been spawned into
  33.   $activeScreen = [System.Windows.Forms.Screen]::FromHandle($WindowHandle).Bounds
  34.  
  35.  
  36.   if($width){}
  37.   else{
  38.     $width = ($activeScreen.Right)/2
  39.   }
  40.  
  41.   if($height){}else{
  42.     $height = ($activeScreen.Bottom)/2
  43.   }
  44.  
  45.   if ($Top) { # if top used, snap to top of screen
  46.     $posY = $activeScreen.Top
  47.   } elseif ($Bottom) { # if bottom used, snap to bottom of screen
  48.     $posY = $activeScreen.Bottom - $height
  49.     $height = $height - 35
  50.   } else { # if neither, snap to current position of the window
  51.     $posY = $rect.top
  52.   }
  53.  
  54.   if ($Left) { # if left used, snap to left of screen
  55.     $posX = $activeScreen.Left
  56.   } elseif ($Right) { # if right used, snap to right of screen
  57.     $posX = $activeScreen.Right - $width
  58.   } else { # if neither, snap to current position of the window
  59.     $posX = $rect.left
  60.   }
  61.  
  62.   [pInvoke]::MoveWindow($WindowHandle, $posX, $posY, $width, $height, $true)
  63. }
  64.  
  65.  
  66. # spawn the window and return the window object
  67. $app1 = Start-Process "C:\Python27\Scripts\rpyc_classic.py" -PassThru
  68. $app2 = Start-Process .\pingMgmt.bat -PassThru
  69. $app3 = Start-Process .\pingOutbound.bat -PassThru
  70.  
  71.  
  72. sleep -Milliseconds 300
  73. Move-Window -WindowHandle $app1.MainWindowHandle -Top -Left -width 1500
  74.  
  75. Move-Window -WindowHandle $app2.MainWindowHandle -Bottom -Left
  76.  
  77. Move-Window -WindowHandle $app3.MainWindowHandle -Bottom -Right
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement