Advertisement
wandrake

Untitled

Mar 13th, 2012
310
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
F# 2.79 KB | None | 0 0
  1. // Ulteriori informazioni su F# all'indirizzo http://fsharp.net
  2.  
  3. open System.Windows.Forms
  4. open System.Drawing
  5.  
  6. #load "ColorPicker.fsx"
  7. open ColorPicker
  8.  
  9. type PaintSimple() as x=
  10.     inherit UserControl()
  11.  
  12.     let mutable pressed = false
  13.     let mutable _line: Point list = []
  14.     // ClientSize -> dimensioni dell'area effettivamente disegnabile, al netto
  15.     // dei bordi
  16.     let mutable _buffer: Bitmap = null
  17.  
  18.     let _allocateBuffer() =
  19.          _buffer <- new Bitmap(x.ClientSize.Width, x.ClientSize.Height)
  20.          use graphics = Graphics.FromImage(_buffer)
  21.          graphics.FillRectangle(Brushes.White, 0, 0, _buffer.Width, _buffer.Height)
  22.  
  23.     do
  24.         //x.DoubleBuffered <- true
  25.         x.SetStyle(ControlStyles.OptimizedDoubleBuffer, true)
  26.         _allocateBuffer()
  27.  
  28.     member x.Buffer = _buffer
  29.  
  30.     member x.Undo() =
  31.         _line <- []
  32.         x.Invalidate()
  33.  
  34.     override x.OnPaintBackground evt = ()
  35.  
  36.     override x.OnClientSizeChanged evt =
  37.         base.OnClientSizeChanged evt
  38.  
  39.         _allocateBuffer()
  40.         x.Invalidate()
  41.  
  42.     override x.OnKeyUp evt =
  43.         match evt.KeyCode with
  44.             //| Keys.Z when evt.Control -> x.Undo()
  45.             | Keys.Z -> x.Undo()
  46.  
  47.     override x.OnMouseDown evt =
  48.         base.OnMouseDown evt
  49.         if _line <> [] then
  50.             use graphics = Graphics.FromImage(_buffer)
  51.             graphics.SmoothingMode <- Drawing2D.SmoothingMode.HighQuality
  52.             if _line.Length > 1 then
  53.                 graphics.DrawLines(Pens.Black, (List.toArray _line))
  54.             _line <- []
  55.            
  56.         pressed <- true
  57.  
  58.     override x.OnMouseUp evt =
  59.         base.OnMouseUp evt
  60.         //points <- (_color, _line)::points
  61.         //_line <- []
  62.         pressed <- false
  63.  
  64.     override x.OnMouseMove evt =
  65.         base.OnMouseMove evt
  66.         if pressed then
  67.             //points <- evt.Location::points
  68.             _line <- evt.Location::_line
  69.             x.Invalidate()
  70.  
  71.     override x.OnPaint evt =
  72.         let graphics = evt.Graphics
  73.        
  74.         graphics.SmoothingMode <- Drawing2D.SmoothingMode.HighQuality
  75.         //if points.Length > 1 then
  76.         graphics.DrawImage(_buffer, 0, 0)
  77.         if _line.Length > 1 then
  78.             graphics.DrawLines(Pens.Black, List.toArray _line)
  79.         (*for list in points do
  80.             match list with
  81.             | (a, b) ->
  82.                 if b.Length > 1 then
  83.                     graphics.DrawLines(new Pen(a), List.toArray b)*)
  84.  
  85. let form = new Form(Text="Paint bacato", TopMost=true)
  86. //let paint = new PaintSimple(Width=500, Height=450)
  87. let paint = new PaintSimple(Width=640,Height=480)
  88. //colorPicker.BorderStyle <- BorderStyle.FixedSingle
  89. paint.BorderStyle <- BorderStyle.FixedSingle
  90.  
  91. //form.Controls.Add(paint)
  92. form.Controls.Add(paint)
  93. form.Show()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement