-
-
Save d-faure/543e8f74a1a7573dd5b7cab872ea56ba to your computer and use it in GitHub Desktop.
PowerShell script to open KSP at specified desktop offset
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# PowerShell script to open KSP at specified desktop offset | |
# --------------------------------------------------------- | |
# cf. https://superuser.com/a/1324170/399048 | |
# | |
# CKAN's Game command-line: | |
# powershell -file .\KSPLauncher.ps1 -X 1921 -Y -350 | |
# | |
[CmdletBinding()] Param ( | |
[int] $X = 0, | |
[int] $Y = 0, | |
[string] $KSPCmd = "KSP_x64.exe", | |
[string] $KSPOpts = "-single-instance", | |
[string] $KSPWorkdir = $pwd.ToString() | |
) | |
Begin { | |
Try { | |
[void][Window] | |
} Catch { | |
Add-Type @" | |
using System; | |
using System.Runtime.InteropServices; | |
public struct RECT | |
{ | |
public int Left; // x position of upper-left corner | |
public int Top; // y position of upper-left corner | |
public int Right; // x position of lower-right corner | |
public int Bottom; // y position of lower-right corner | |
} | |
public class Window { | |
[DllImport("user32.dll")] | |
[return: MarshalAs(UnmanagedType.Bool)] | |
public static extern bool GetWindowRect( | |
IntPtr hWnd, out RECT lpRect); | |
[DllImport("user32.dll")] | |
[return: MarshalAs(UnmanagedType.Bool)] | |
public extern static bool MoveWindow( | |
IntPtr handle, int x, int y, int width, int height, bool redraw); | |
[DllImport("user32.dll")] | |
[return: MarshalAs(UnmanagedType.Bool)] | |
public static extern bool ShowWindow( | |
IntPtr handle, int state); | |
} | |
"@ | |
} | |
} | |
Process { | |
Write-Verbose "Params `(X=$($X), Y=$($Y), KSPCmd='$($KSPCmd)', KSPOpts='$($KSPOpts)', KSPWorkdir='$($KSPWorkdir)'`)" | |
$kspInstance = Start-Process -FilePath $KSPCmd -ArgumentList $KSPOpts -WorkingDirectory $KSPWorkdir -PassThru | |
If ($kspInstance -eq $null) { | |
Write-Error "'$($KSPCmd)' process not found" | |
Return | |
} | |
If (-NOT ($PSBoundParameters.ContainsKey('X') -OR $PSBoundParameters.ContainsKey('Y'))) { | |
Write-Verbose "nowhere to move" | |
Return | |
} | |
While ($kspInstance.MainWindowHandle -eq [System.IntPtr]::Zero) { | |
Start-Sleep -Milliseconds 100 | |
} | |
Write-Verbose "Active $($kspInstance.ProcessName) `(Id=$($kspInstance.Id), Handle=$($kspInstance.MainWindowHandle)`)" | |
$kspRect = New-Object RECT | |
$ok = [Window]::GetWindowRect($kspInstance.MainWindowHandle, [ref] $kspRect) | |
Write-Verbose "Rect `(Left=$($kspRect.Left), Top=$($kspRect.Top), Right=$($kspRect.Right), Bottom=$($kspRect.Bottom)`)" | |
$W = $kspRect.Right - $kspRect.Left | |
$H = $kspRect.Bottom - $kspRect.Top | |
Write-Verbose "New Rect `(X=$($X), Y=$($Y), W='$($W)', H='$($H)'`)" | |
If ($ok) { | |
$ok = [Window]::MoveWindow($kspInstance.MainWindowHandle, $X, $Y, $W, $H, $true) | |
} | |
Write-Verbose "window moved" | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment