Advertisement
wandrake

Untitled

Feb 22nd, 2012
206
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
F# 2.61 KB | None | 0 0
  1. open System
  2. open System.Drawing
  3. open System.Windows.Forms
  4.  
  5. let mainForm =
  6.     let form = new Form(Text = "Sono una cosa inutile :D!", Width = 800, Height = 600)
  7.  
  8.     let pointsMasterList = ref []
  9.     let pointsTempList = ref[]
  10.     let mouseDown = ref 0
  11.     let pen = ref (new Pen (Color.Black))
  12.  
  13.     let btnClear = new Button (Text = "Clear", Size = new Size(50, 20), Location = new Point(500, 500))
  14.     let btnClose = new Button (Text = "Close", Size = new Size(50, 20), Location = new Point(500, 530))
  15.  
  16.     btnClear.Click
  17.         |> Event.add (fun _ -> pointsTempList := []; pointsMasterList := []; form.Invalidate())
  18.  
  19.     btnClose.Click
  20.         |> Event.add (fun _ -> exit 0)
  21.  
  22.     let mouseUp _ =
  23.         printfn "Mouse uppin'"
  24.         mouseDown := !mouseDown - 1
  25.         printfn "mouseDown: %d" !mouseDown
  26.         if List.length !pointsTempList > 1 then
  27.             let points = List.toArray !pointsTempList
  28.             pointsMasterList :=
  29.                 (!pen, points) :: !pointsMasterList
  30.         pointsTempList := []
  31.         form.Invalidate ()
  32.  
  33.     let mouseMove (e: MouseEventArgs) =
  34.         pointsTempList := e.Location :: !pointsTempList
  35.         form.Invalidate ()
  36.  
  37.     let paint (e: PaintEventArgs) =
  38.         if List.length !pointsTempList > 1 then
  39.             e.Graphics.DrawLines
  40.                 (!pen, List.toArray !pointsTempList)
  41.  
  42.         !pointsMasterList
  43.             |> List.iter (fun (pen, points) -> e.Graphics.DrawLines (pen, points))
  44.  
  45.     let (leftMouse, otherMouse) =
  46.         form.MouseDown
  47.             |> Event.partition (fun e -> e.Button = MouseButtons.Left)
  48.  
  49.     let (rightMouse, otherMouse) =
  50.         otherMouse
  51.             |> Event.partition (fun e -> e.Button = MouseButtons.Right)
  52.  
  53.     leftMouse
  54.         |> Event.filter (fun _ -> !mouseDown = 0)
  55.         |> Event.add (fun _ -> pen := new Pen(Color.Black))
  56.  
  57.     rightMouse
  58.         |> Event.filter (fun _ -> !mouseDown = 0)
  59.         |> Event.add (fun _ -> pen := new Pen(Color.Red))
  60.  
  61.     form.MouseDown
  62.         |> Event.filter (fun e -> e.Button = MouseButtons.Left || e.Button = MouseButtons.Right)
  63.         |> Event.add(fun _ -> mouseDown := !mouseDown + 1; printfn "mouseDown: %d" !mouseDown)
  64.  
  65.     form.MouseUp
  66.         |> Event.filter (fun e -> e.Button = MouseButtons.Left || e.Button = MouseButtons.Right)
  67.         |> Event.add mouseUp
  68.    
  69.     form.MouseMove
  70.         |> Event.filter (fun _ -> !mouseDown > 0)
  71.         |> Event.add mouseMove
  72.  
  73.     form.Paint
  74.         |> Event.add paint
  75.  
  76.     form.Controls.Add (btnClear)
  77.     form.Controls.Add (btnClose)
  78.  
  79.     form
  80.  
  81. [<STAThread>]
  82. do Application.Run (mainForm)
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement