Created
September 10, 2011 01:47
-
-
Save 7shi/1207815 to your computer and use it in GitHub Desktop.
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
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) |
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
Hi 7shi.
Thanks for this example of fsharp wpf mouse gui.
Have you seen Phil Treford's SL WPF Time flys ...? http://www.trelford.com/blog/?page=2
I also like Jon Harrop's Quick hull (F#.NET Journal subscription) where he uses int option ref.
"... A key aspect of the GUI is the ability to drag and drop points in the point set and watch the convex hull as it is recomputed in real time. The following drag value is the current state of the outstanding drag operation, if any, represented by the index of the point being dragged:
This design follows the mantra "make illegal states unrepresentable". Alternative designs in languages where this cannot be so easily expressed tend to use one boolean variable to indicate whether or not a drag is in progress and another integer variable to convey the index when meaningful. Using data structures such as the option type in F# precludes the possibility of non-sensical values being created and encountered at run-time, reducing the scope for errors and the number of run-time tests. ..."
Best regards. PLH OOE Art Scott Twitter: semasiographic