Skip to content

Instantly share code, notes, and snippets.

@7shi
Created September 10, 2011 01:47
Show Gist options
  • Save 7shi/1207815 to your computer and use it in GitHub Desktop.
Save 7shi/1207815 to your computer and use it in GitHub Desktop.
open System
open System.Windows
open System.Windows.Controls
open System.Windows.Shapes
open System.Windows.Media
let move sh x y =
Canvas.SetLeft(sh, x)
Canvas.SetTop(sh, y)
let mutable canvas : Canvas = null
let mutable elem : UIElement = null
let mutable click = new Point()
let mutable orig = new Point()
let down (el : UIElement) (e : Input.MouseButtonEventArgs) =
elem <- el
orig <- new Point(Canvas.GetLeft(el), Canvas.GetTop(el))
click <- e.GetPosition(canvas)
ignore <| canvas.CaptureMouse()
[<STAThread>]
do
let w = new Window(Title = "Test", Width = 256.0, Height = 256.0)
w.Show()
canvas <- new Canvas()
w.Content <- canvas
let r = new Rectangle(Width = 40.0, Height = 40.0)
move r 20.0 60.0
r.Stroke <- new SolidColorBrush(Colors.Black)
r.Fill <- new SolidColorBrush(Colors.Yellow)
ignore <| canvas.Children.Add(r)
let tb = new TextBlock(Text = "abcdefg", FontSize = 24.0)
move tb 20.0 30.0
ignore <| canvas.Children.Add(tb)
r.MouseDown.Add <| down r
tb.MouseDown.Add <| down tb
canvas.MouseMove.Add <| fun e ->
if elem <> null then
let p = e.GetPosition(canvas)
move elem (orig.X + p.X - click.X) (orig.Y + p.Y - click.Y)
canvas.MouseUp.Add <| fun _ ->
elem <- null
canvas.ReleaseMouseCapture()
ignore <| (new Application()).Run(w)
@7shi
Copy link
Author

7shi commented Jan 11, 2012

Hi Art.
Thank you for your interest in my gist.
In fact, this gist is a translation from C#. https://gist.github.com/1207814
How implement drag and drop is an another problem. I like the coroutine for drag and drop. https://gist.github.com/1275258
Best regards. 7shi

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment