Home
Insert
Update snippet 'Drag move for GUI controls'
Title
Passcode
Description
This script opens a window with a red rectangle which is moved by dragging. I like writing interactive GUI without MVVM, FRP and Markup language.
Source code
#r @"PresentationCore" #r @"PresentationFramework" #r @"WindowsBase" #r @"System.Xaml" open System.Windows open System.Windows.Controls let rect = Shapes.Rectangle(Width=50.,Height=50., Fill=Media.Brushes.Red) let canvas = Canvas() canvas.Children.Add rect |> ignore let window = Window(Width=200.,Height=200.) window.Content <- canvas let mutable offset = None rect.MouseDown.Add <| fun e -> offset <- Some (e.GetPosition rect) rect.MouseMove.Add <| fun e -> if offset.IsSome then let point = e.GetPosition canvas Canvas.SetLeft(rect, point.X - offset.Value.X) Canvas.SetTop (rect, point.Y - offset.Value.Y) rect.MouseUp.Add <| fun _ -> offset <- None rect.MouseLeave.Add <| fun _ -> offset <- None window.Show()
Tags
wpf
gui
drag and drop
wpf
gui
drag and drop
Author
Link
Reference NuGet packages
If your snippet has external dependencies, enter the names of NuGet packages to reference, separated by a comma (
#r
directives are not required).
Update