Advertisement
Guest User

Untitled

a guest
Oct 18th, 2014
299
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
F# 2.66 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.                 action <- Some(Azione.Move)
  52.                 scrollTimer.Start()
  53.                
  54.         this.Invalidate()
  55.    
  56.     override this.OnMouseUp e =
  57.         if pressed <> true then
  58.             scrollTimer.Stop()
  59.        
  60.         this.Invalidate()  
  61.    
  62.     override this.OnPaint e =
  63.         let g = e.Graphics
  64.         let d = this.Raggio*2
  65.         if pressed then
  66.             g.DrawEllipse(Pens.Green,this.CentroX-r,this.CentroY-r, d,d)
  67.             g.FillEllipse(Brushes.Green,this.CentroX-r,this.CentroY-r, d,d)
  68.             g.DrawRectangle(Pens.Blue,this.CentroX-r,this.CentroY-r,d,d)
  69.            
  70.         else
  71.             g.DrawEllipse(Pens.Red,this.CentroX-r,this.CentroY-r, d,d)
  72.             g.FillEllipse(Brushes.Red,this.CentroX-r,this.CentroY-r, d,d)
  73.             g.DrawRectangle(Pens.Blue,this.CentroX-r,this.CentroY-r,d,d)
  74.         g.DrawEllipse(Pens.Black,this.CentroX+d,this.CentroY+d,r/4,r/4)    
  75.    
  76.     override this.OnResize e =
  77.     this.Invalidate()
  78.        
  79.     member this.DoAction (act:Azione) =
  80.         match act with
  81.         | Azione.Move ->
  82.           w2v.Translate(0.f, -10.f, Drawing2D.MatrixOrder.Append)
  83.           v2w.Translate(0.f, -10.f)
  84.           this.Invalidate()
  85.              
  86.  
  87.                
  88.        
  89.  
  90. let f = new Form(Text="ciao", TopMost=true)
  91. f.Size <- new Size(500,500)
  92. let c = new CircleIns(Dock=DockStyle.Fill)
  93. c.Raggio <- 40
  94. c.CentroX <- 240
  95. c.CentroY <- 240
  96. f.Controls.Add(c)
  97. f.Show()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement