9

I'm running the command as follows.

Start-Process dotnet -ArgumentList "run"

The window can be managed using -WindowStyle flag to be maximized, minimized, hidden and normal. However, what I usually do is to push the frame to the left (and the second to the right).

Is it possible to tell PowerShell to float the window to the edge? Something like this wishful pseudo-code?

Start-Process dotnet -ArgumentList "run" -WindowStyle FloatLeft
Konrad Viltersten
  • 36,151
  • 76
  • 250
  • 438
  • 4
    Hi, check this out: https://www.windrath.com/2016/05/powershell-start-process-wrapper/ – sodawillow Dec 19 '16 at 19:51
  • @sodawillow Hmm... So... That would be a no, right? (If the complexity of something defeats the gain, I'd say it's not doable, hehe.) Maybe it's my ignorance and incompetence on the subject but that stuff looks scary... – Konrad Viltersten Dec 19 '16 at 19:53
  • 1
    The script is quite short, and demonstrates how you can manipulate the window coordinates. You can either use it, or build a new one if/when you understand the inner workings of it. It does not seem too shady to me ^^. _"float to the edge"_ means X equals total width minus window width, right ? ( : – sodawillow Dec 19 '16 at 19:54
  • 1
    As sodawillow linked, User32.dll is the dll with the Win32 methods for moving windows. Powershell does not have this functionality natively `WindowStyle` only has `Normal, Hidden, Minimized, and Maximized` as available options. This is the PowerShell equivalent of rundll32.exe using .Net Platform invoke. – BenH Dec 19 '16 at 20:58
  • @sodawillow Not exactly. By "float to the edge", I meant the same effect as you get when pressing Win+RightArrow, i.e. the window occupies half of the width and is stuck to the edge of the screen. – Konrad Viltersten Dec 20 '16 at 11:57

1 Answers1

6

Try this, which uses the -Passthru option for Start-Process to get the process info. Then, we use a bit of pInvoke magic to move the window we've just created to somewhere else.

This example will enable you to snap the spawned window to the edges of the windows current screen. You can specify X or Y edges, or both. Top, Left wins if all 4 switches are specified.

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) {
  # 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 ($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 - ($rect.bottom - $rect.top)
  } 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 - ($rect.right - $rect.left)
  } else { # if neither, snap to current position of the window
    $posX = $rect.left
  }

  [pInvoke]::MoveWindow($app.MainWindowHandle, $posX, $posY, $rect.right - $rect.left, $rect.bottom - $rect.top, $true)
}

# spawn the window and return the window object
$app = Start-Process dotnet -ArgumentList "run" -PassThru

Move-Window -WindowHandle $app.MainWindowHandle -Bottom -Left
TechSpud
  • 3,418
  • 1
  • 27
  • 35