Advertisement
Guest User

Untitled

a guest
Oct 18th, 2014
186
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
F# 2.64 KB | None | 0 0
  1. open System.Windows.Forms
  2. open System.Drawing
  3.  
  4. Application.EnableVisualStyles()
  5.  
  6. type Azione =
  7. | Move
  8.  
  9. type CircleIns() as this =
  10.    
  11.     inherit Control()
  12.  
  13.     let mutable r,cx,cy = 2,2,2
  14.     let mutable pressed = false
  15.     let mutable action = None
  16.     let w2v = new Drawing2D.Matrix()
  17.     let v2w = new Drawing2D.Matrix()
  18.     let isClose (p : Point) (l : Point) =
  19.         let dx = p.X - l.X
  20.         let dy = p.Y - l.Y
  21.         (dx * dx + dy * dy) < this.Raggio * this.Raggio
  22.     let scrollTimer = new Timer(Interval=30)
  23.    
  24.  
  25.     do
  26.         scrollTimer.Tick.Add(fun _ ->
  27.             match action with
  28.             | Some a -> this.DoAction a
  29.             | None ->()        
  30.         )
  31.  
  32.     member this.Raggio
  33.         with get() = r
  34.         and set(v) = r <- v
  35.  
  36.     member this.CentroX
  37.         with get() = cx
  38.         and set(v) = cx <- v
  39.    
  40.     member this.CentroY
  41.         with get() = cy
  42.         and set(v) = cy <- v
  43.    
  44.     override this.OnMouseDown e =
  45.         let pc = new Point(this.CentroX,this.CentroY)
  46.         let pmouse = new Point(e.X,e.Y)
  47.         if (isClose pc pmouse) then
  48.             if pressed then pressed <- false
  49.             else
  50.                 pressed <- true
  51.                 scrollTimer.Start()
  52.                 this.DoAction Move
  53.         this.Invalidate()
  54.    
  55.     override this.OnMouseUp e =
  56.         if pressed <> true then
  57.             scrollTimer.Stop()
  58.        
  59.         this.Invalidate()  
  60.    
  61.     override this.OnPaint e =
  62.         let g = e.Graphics
  63.         let d = this.Raggio*2
  64.         if pressed then
  65.             g.DrawEllipse(Pens.Green,this.CentroX-r,this.CentroY-r, d,d)
  66.             g.FillEllipse(Brushes.Green,this.CentroX-r,this.CentroY-r, d,d)
  67.             g.DrawRectangle(Pens.Blue,this.CentroX-r,this.CentroY-r,d,d)
  68.            
  69.         else
  70.             g.DrawEllipse(Pens.Red,this.CentroX-r,this.CentroY-r, d,d)
  71.             g.FillEllipse(Brushes.Red,this.CentroX-r,this.CentroY-r, d,d)
  72.             g.DrawRectangle(Pens.Blue,this.CentroX-r,this.CentroY-r,d,d)
  73.         g.DrawEllipse(Pens.Black,this.CentroX+d,this.CentroY+d,r/4,r/4)    
  74.    
  75.     override this.OnResize e =
  76.     this.Invalidate()
  77.        
  78.     member this.DoAction (act:Azione) =
  79.         match act with
  80.         | Azione.Move ->
  81.           w2v.Translate(0.f, -10.f, Drawing2D.MatrixOrder.Append)
  82.           v2w.Translate(0.f, -10.f)
  83.           this.Invalidate()
  84.              
  85.  
  86.                
  87.        
  88.  
  89. let f = new Form(Text="ciao", TopMost=true)
  90. f.Size <- new Size(500,500)
  91. let c = new CircleIns(Dock=DockStyle.Fill)
  92. c.Raggio <- 40
  93. c.CentroX <- 240
  94. c.CentroY <- 240
  95. f.Controls.Add(c)
  96. f.Show()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement