Created
March 8, 2019 16:36
-
-
Save noahpeltier/aaf816ae3d41e0ac669e473ee1805046 to your computer and use it in GitHub Desktop.
Stick this in a win forms app to make it drag-able
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
$global:dragging = $false | |
$global:mouseDragX = 0 | |
$global:mouseDragY = 0 | |
$form1.Add_MouseDown( { $global:dragging = $true | |
$global:mouseDragX = [System.Windows.Forms.Cursor]::Position.X - $form1.Left | |
$global:mouseDragY = [System.Windows.Forms.Cursor]::Position.Y -$form1.Top | |
}) | |
$form1.Add_MouseMove( { if($global:dragging) { | |
$screen = [System.Windows.Forms.Screen]::PrimaryScreen.WorkingArea | |
$currentX = [System.Windows.Forms.Cursor]::Position.X | |
$currentY = [System.Windows.Forms.Cursor]::Position.Y | |
[int]$newX = [Math]::Min($currentX - $global:mouseDragX, $screen.Right - $form1.Width) | |
[int]$newY = [Math]::Min($currentY - $global:mouseDragY, $screen.Bottom - $form1.Height) | |
$form1.Location = New-Object System.Drawing.Point($newX, $newY) | |
}}) | |
$form1.Add_MouseUp( { $global:dragging = $false }) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment