8 people like it.

Drag move for GUI controls

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.

 1: 
 2: 
 3: 
 4: 
 5: 
 6: 
 7: 
 8: 
 9: 
10: 
11: 
12: 
13: 
14: 
15: 
16: 
17: 
18: 
19: 
20: 
21: 
22: 
23: 
24: 
25: 
26: 
#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()
namespace System
namespace System.Windows
namespace System.Windows.Controls
val rect : Shapes.Rectangle

Full name: Script.rect
namespace System.Windows.Shapes
Multiple items
type Rectangle =
  inherit Shape
  new : unit -> Rectangle
  member GeometryTransform : Transform
  member RadiusX : float with get, set
  member RadiusY : float with get, set
  member RenderedGeometry : Geometry
  static val RadiusXProperty : DependencyProperty
  static val RadiusYProperty : DependencyProperty

Full name: System.Windows.Shapes.Rectangle

--------------------
Shapes.Rectangle() : unit
namespace System.Windows.Media
type Brushes =
  static member AliceBlue : SolidColorBrush
  static member AntiqueWhite : SolidColorBrush
  static member Aqua : SolidColorBrush
  static member Aquamarine : SolidColorBrush
  static member Azure : SolidColorBrush
  static member Beige : SolidColorBrush
  static member Bisque : SolidColorBrush
  static member Black : SolidColorBrush
  static member BlanchedAlmond : SolidColorBrush
  static member Blue : SolidColorBrush
  ...

Full name: System.Windows.Media.Brushes
property Media.Brushes.Red: Media.SolidColorBrush
val canvas : Canvas

Full name: Script.canvas
Multiple items
type Canvas =
  inherit Panel
  new : unit -> Canvas
  static val LeftProperty : DependencyProperty
  static val TopProperty : DependencyProperty
  static val RightProperty : DependencyProperty
  static val BottomProperty : DependencyProperty
  static member GetBottom : element:UIElement -> float
  static member GetLeft : element:UIElement -> float
  static member GetRight : element:UIElement -> float
  static member GetTop : element:UIElement -> float
  static member SetBottom : element:UIElement * length:float -> unit
  ...

Full name: System.Windows.Controls.Canvas

--------------------
Canvas() : unit
property Panel.Children: UIElementCollection
UIElementCollection.Add(element: UIElement) : int
val ignore : value:'T -> unit

Full name: Microsoft.FSharp.Core.Operators.ignore
val window : Window

Full name: Script.window
Multiple items
type Window =
  inherit ContentControl
  new : unit -> Window
  member Activate : unit -> bool
  member AllowsTransparency : bool with get, set
  member Close : unit -> unit
  member DialogResult : Nullable<bool> with get, set
  member DragMove : unit -> unit
  member Hide : unit -> unit
  member Icon : ImageSource with get, set
  member IsActive : bool
  member Left : float with get, set
  ...

Full name: System.Windows.Window

--------------------
Window() : unit
property ContentControl.Content: obj
val mutable offset : Point option

Full name: Script.offset
union case Option.None: Option<'T>
event UIElement.MouseDown: IEvent<Input.MouseButtonEventHandler,Input.MouseButtonEventArgs>
member System.IObservable.Add : callback:('T -> unit) -> unit
val e : Input.MouseButtonEventArgs
union case Option.Some: Value: 'T -> Option<'T>
Input.MouseEventArgs.GetPosition(relativeTo: IInputElement) : Point
event UIElement.MouseMove: IEvent<Input.MouseEventHandler,Input.MouseEventArgs>
val e : Input.MouseEventArgs
property Option.IsSome: bool
val point : Point
Canvas.SetLeft(element: UIElement, length: float) : unit
property Point.X: float
property Option.Value: Point
Canvas.SetTop(element: UIElement, length: float) : unit
property Point.Y: float
event UIElement.MouseUp: IEvent<Input.MouseButtonEventHandler,Input.MouseButtonEventArgs>
event UIElement.MouseLeave: IEvent<Input.MouseEventHandler,Input.MouseEventArgs>
Window.Show() : unit
Raw view Test code New version

More information

Link:http://fssnip.net/9N
Posted:12 years ago
Author:nagat01
Tags: wpf , gui , drag and drop