Advertisement
Guest User

Untitled

a guest
Oct 24th, 2014
173
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
F# 2.31 KB | None | 0 0
  1. open System.Windows.Forms
  2. open System.Drawing
  3.  
  4. type Direction =
  5. | Up
  6. | Down
  7. | Left
  8. | Right
  9.  
  10. type MoveEll () as this =
  11.  
  12.     inherit Control()
  13.  
  14.     let mutable raggio = 50
  15.     let mutable raggioS = 10
  16.     let mutable cbx , cby = 250,250
  17.     let mutable alfa = raggio * 2
  18.     let mutable csx, csy = cbx + alfa, cby + alfa
  19.     let mutable dir = Up
  20.     let mutable pressed = false
  21.     let isClose (p : Point) (l : Point) =
  22.         let dx = p.X - l.X
  23.         let dy = p.Y - l.Y
  24.         (dx * dx + dy * dy) < raggio * raggio
  25.    
  26.  
  27.     let timer = new Timer(Interval=30)
  28.  
  29.     do this.SetStyle(ControlStyles.OptimizedDoubleBuffer,true)
  30.    
  31.     do timer.Tick.Add( fun e ->
  32.         match dir with
  33.         |   Up -> if csy > cby - alfa then csy <- csy - 30
  34.                   else dir <- Left
  35.                   this.Invalidate()
  36.                    
  37.         |   Down -> if csy < cby + alfa then csy <- csy + 30
  38.                     else dir <- Right
  39.                     this.Invalidate()
  40.  
  41.         |   Left -> if csx > cbx - alfa then csx <- csx - 30
  42.                     else dir <- Down
  43.                     this.Invalidate()
  44.  
  45.         |   Right -> if csx < cbx + alfa then csx <- csx + 30
  46.                      else dir <- Up
  47.                      this.Invalidate()  
  48.         )
  49.  
  50.      
  51.     override this.OnMouseDown e =
  52.       let pc = new Point(cbx,cby)
  53.       let pmouse = new Point(e.X,e.Y)
  54.       if (isClose pc pmouse) then  
  55.        
  56.         if pressed then
  57.                 pressed <- false
  58.                 timer.Stop()
  59.         else
  60.                 pressed <- true
  61.                 timer.Start()
  62.        
  63.       this.Invalidate()
  64.    
  65.     override this.OnResize e =
  66.        
  67.         timer.Stop()
  68.         dir <- Up
  69.         csx <- cbx + alfa
  70.         csy <- cby + alfa
  71.         this.Invalidate()
  72.  
  73.     override this.OnPaint e =
  74.         let g = e.Graphics
  75.         let r = raggio
  76.         let rs = raggioS
  77.         if pressed then
  78.             g.FillEllipse(Brushes.Green,cbx-r,cby-r,r*2,r*2)
  79.         else
  80.             g.FillEllipse(Brushes.Red,cbx-r,cby-r,r*2,r*2)
  81.         g.FillEllipse(Brushes.Black,csx-rs,csy-rs,rs*2,rs*2)
  82.        
  83.  
  84.  
  85. let f = new Form (Text="CrossFingers" , TopMost=true)
  86. f.Size <- new Size(500,500)
  87. let mc = new MoveEll(Dock = DockStyle.Fill)
  88. f.Controls.Add(mc)
  89. f.Show()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement