Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- open System.Windows.Forms
- open System.Drawing
- type Direction =
- | Up
- | Down
- | Left
- | Right
- type MoveEll () as this =
- inherit Control()
- let mutable raggio = 50
- let mutable raggioS = 10
- let mutable cbx , cby = 250,250
- let mutable alfa = raggio * 2
- let mutable csx, csy = cbx + alfa, cby + alfa
- let mutable dir = Up
- let mutable pressed = false
- let isClose (p : Point) (l : Point) =
- let dx = p.X - l.X
- let dy = p.Y - l.Y
- (dx * dx + dy * dy) < raggio * raggio
- let timer = new Timer(Interval=30)
- do this.SetStyle(ControlStyles.OptimizedDoubleBuffer,true)
- do timer.Tick.Add( fun e ->
- match dir with
- | Up -> if csy > cby - alfa then csy <- csy - 30
- else dir <- Left
- this.Invalidate()
- | Down -> if csy < cby + alfa then csy <- csy + 30
- else dir <- Right
- this.Invalidate()
- | Left -> if csx > cbx - alfa then csx <- csx - 30
- else dir <- Down
- this.Invalidate()
- | Right -> if csx < cbx + alfa then csx <- csx + 30
- else dir <- Up
- this.Invalidate()
- )
- override this.OnMouseDown e =
- let pc = new Point(cbx,cby)
- let pmouse = new Point(e.X,e.Y)
- if (isClose pc pmouse) then
- if pressed then
- pressed <- false
- timer.Stop()
- else
- pressed <- true
- timer.Start()
- this.Invalidate()
- override this.OnResize e =
- timer.Stop()
- dir <- Up
- csx <- cbx + alfa
- csy <- cby + alfa
- this.Invalidate()
- override this.OnPaint e =
- let g = e.Graphics
- let r = raggio
- let rs = raggioS
- if pressed then
- g.FillEllipse(Brushes.Green,cbx-r,cby-r,r*2,r*2)
- else
- g.FillEllipse(Brushes.Red,cbx-r,cby-r,r*2,r*2)
- g.FillEllipse(Brushes.Black,csx-rs,csy-rs,rs*2,rs*2)
- let f = new Form (Text="CrossFingers" , TopMost=true)
- f.Size <- new Size(500,500)
- let mc = new MoveEll(Dock = DockStyle.Fill)
- f.Controls.Add(mc)
- f.Show()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement