Skip to content

Instantly share code, notes, and snippets.

@noahpeltier
Created March 8, 2019 16:36
Show Gist options
  • Save noahpeltier/aaf816ae3d41e0ac669e473ee1805046 to your computer and use it in GitHub Desktop.
Save noahpeltier/aaf816ae3d41e0ac669e473ee1805046 to your computer and use it in GitHub Desktop.
Stick this in a win forms app to make it drag-able
$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