-
-
Save mfic/81ade6524c8339c84d96a7926d17c33e to your computer and use it in GitHub Desktop.
Move Window To Top Left Corner of Parent Container
This file contains hidden or 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
| # Embed the C# code in PowerShell | |
| # Fork from: https://gist.github.com/JohnLBevan/1593bbb860c2d2af436a1c9414e8adfa | |
| Add-Type @" | |
| using System; | |
| using System.Runtime.InteropServices; | |
| public class WindowMover | |
| { | |
| [StructLayout(LayoutKind.Sequential)] | |
| public struct POINT | |
| { | |
| public int x; | |
| public int y; | |
| } | |
| [StructLayout(LayoutKind.Sequential)] | |
| public struct RECT | |
| { | |
| public int left; | |
| public int top; | |
| public int right; | |
| public int bottom; | |
| } | |
| public class User32 | |
| { | |
| [DllImport("user32.dll")] | |
| [return: MarshalAs(UnmanagedType.Bool)] | |
| public static extern bool GetCursorPos(out POINT lpPoint); | |
| [DllImport("user32.dll")] | |
| public static extern IntPtr GetForegroundWindow(); | |
| [DllImport("user32.dll")] | |
| [return: MarshalAs(UnmanagedType.Bool)] | |
| public static extern bool GetClientRect(IntPtr hWnd, out RECT lpRect); | |
| [DllImport("user32.dll")] | |
| [return: MarshalAs(UnmanagedType.Bool)] | |
| public static extern bool MoveWindow(IntPtr hWnd, int X, int Y, int nWidth, int nHeight, bool bRepaint); | |
| } | |
| public static void MoveActiveWindowToCursor() | |
| { | |
| POINT cursorPos; | |
| if (!User32.GetCursorPos(out cursorPos)) | |
| { | |
| throw new Exception("Failed to get cursor position."); | |
| } | |
| IntPtr hWnd = User32.GetForegroundWindow(); | |
| if (hWnd == IntPtr.Zero) | |
| { | |
| throw new Exception("No active window found."); | |
| } | |
| RECT windowRect; | |
| if (!User32.GetClientRect(hWnd, out windowRect)) | |
| { | |
| throw new Exception("Failed to get window size."); | |
| } | |
| int width = windowRect.right; | |
| int height = windowRect.bottom; | |
| if (!User32.MoveWindow(hWnd, cursorPos.x, cursorPos.y, width, height, true)) | |
| { | |
| throw new Exception("Failed to move the window."); | |
| } | |
| } | |
| } | |
| "@ | |
| # Allow some time to focus the window and move the cursor | |
| Write-Output "Quick; get the window in question in focus, then put your mouse in position!" | |
| Start-Sleep -Seconds 5 | |
| # Call the C# method to move the window | |
| try { | |
| [WindowMover]::MoveActiveWindowToCursor() | |
| Write-Output "Window moved successfully (hope you agree!)" | |
| } | |
| catch { | |
| Write-Warning $_.Exception.Message | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment