Advertisement
wandrake

Untitled

Feb 22nd, 2012
205
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
F# 2.33 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 = "Ciao!", 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.         if List.length !pointsTempList > 1 then
  26.             let points = List.toArray !pointsTempList
  27.             pointsMasterList :=
  28.                 (!pen, points) :: !pointsMasterList
  29.         pointsTempList := []
  30.         form.Invalidate()
  31.  
  32.     let mouseMove (e: MouseEventArgs) =
  33.         pointsTempList := e.Location :: !pointsTempList
  34.         form.Invalidate()
  35.  
  36.     let paint (e: PaintEventArgs) =
  37.         if List.length !pointsTempList > 1 then
  38.             e.Graphics.DrawLines
  39.                 (!pen, List.toArray !pointsTempList)
  40.  
  41.         !pointsMasterList
  42.             |> List.iter (fun (pen, points) -> e.Graphics.DrawLines(pen, points))
  43.  
  44.     let (leftMouse, otherMouse) =
  45.         form.MouseDown
  46.             |> Event.partition(fun e -> e.Button = MouseButtons.Left)
  47.  
  48.     let (rightMouse, otherMouse) =
  49.         otherMouse
  50.             |> Event.partition(fun e -> e.Button = MouseButtons.Right)
  51.  
  52.     leftMouse
  53.         |> Event.filter(fun _ -> !mouseDown = 0)
  54.         |> Event.add(fun _ -> pen := new Pen(Color.Black))
  55.  
  56.     rightMouse
  57.         |> Event.filter(fun _ -> !mouseDown = 0)
  58.         |> Event.add(fun _ -> pen := new Pen(Color.Red))
  59.  
  60.     form.MouseDown
  61.         |> Event.add(fun _ -> mouseDown := !mouseDown + 1; printfn "%d" !mouseDown)
  62.        
  63.     form.MouseUp
  64.         |> Event.add mouseUp
  65.  
  66.     form.MouseMove
  67.         |> Event.filter(fun _ -> !mouseDown > 0)
  68.         |> Event.add mouseMove
  69.  
  70.     form.Paint
  71.         |> Event.add paint
  72.  
  73.     form.Controls.Add(btnClear)
  74.     form.Controls.Add(btnClose)
  75.  
  76.     form
  77.  
  78. [<STAThread>]
  79. do Application.Run(mainForm)
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement