Advertisement
Guest User

Untitled

a guest
Oct 2nd, 2015
83
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
F# 3.74 KB | None | 0 0
  1. open System.Windows.Forms
  2. open System.Drawing
  3.  
  4. let cx, cy = 100.f , 100.f  
  5. let mutable selected = false
  6.  
  7. type Editor() as this =
  8.     inherit UserControl()
  9.     do
  10.         this.SetStyle(ControlStyles.DoubleBuffer, true)
  11.         this.SetStyle(ControlStyles.UserPaint,true)
  12.         this.SetStyle(ControlStyles.AllPaintingInWmPaint, true)
  13.  
  14.     let HitTest (p:Point) (h:Point) =
  15.    //p è il punto dove sta cliccando il mouse
  16.    //h è l'estremo del segmento
  17.             let centrox = float(cx)
  18.             let centroy = float(cy)
  19.             let h1x = float(h.X)
  20.             let h1y = float(h.Y)
  21.             let p1x = float(p.X)
  22.             let p1y = float(p.Y)
  23.             let m1 = ((h1y - centroy) / (h1x - centrox)) //coeff angolare retta centro segmento
  24.             let m2 = ((p1y - centroy) / (p1x - centrox)) //coeff angolare retta punto cliccato
  25.  
  26.             if (centrox < h1x) then
  27.                 if (centroy < h1y) then
  28.                  (m1 = m2) && (centrox < p1x) && (p1x <= h1x) && (centroy < p1y) && (p1y <= h1y)
  29.                 else
  30.                 (m1 = m2) && (centrox < p1x) && (p1x <= h1x) && (h1y < p1y) && (p1y <= centroy)
  31.             else
  32.                 if (centroy < h1y) then
  33.                     (m1 = m2) && (h1x < p1x) && (p1x <= centrox) && (centroy < p1y) && (p1y <= h1y)
  34.                 else
  35.                 (m1 = m2) && (h1x < p1x) && (p1x <= centrox) && (h1y < p1y) && (p1y <= centroy)
  36.  
  37.     override this.OnPaint e = (
  38.         let g = e.Graphics
  39.         g.SmoothingMode <- Drawing2D.SmoothingMode.HighQuality
  40.         g.TranslateTransform(cx, cy)
  41.         g.RotateTransform(-90.f)
  42.         let mutable t = System.DateTime.Now
  43.         t <- t.AddHours(1.)
  44.         let mutable s = g.Save()
  45.         g.RotateTransform(single((t.Hour % 12) * 30))
  46.         g.DrawLine(Pens.Black, -5, 0, 60, 0)
  47.         g.Restore(s)
  48.         s <- g.Save()
  49.         g.RotateTransform(single(t.Minute * 6))
  50.         g.DrawLine(Pens.Black, -5, 0, 75, 0)
  51.         g.Restore(s)
  52.         s <- g.Save()
  53.         g.RotateTransform(single(t.Second * 6))
  54.         g.DrawLine(Pens.Red, -5, 0, 75, 0)
  55.         )
  56.  
  57.     override this.OnMouseDown e = (
  58.         let clicked = e.Location
  59.         printfn "%d %d" clicked.X clicked.Y
  60.         let t1 = System.DateTime.Now
  61.         let an = (float(((t1.Hour % 12) * 30) / 180)) * System.Math.PI
  62.         let hour = new Point (int(sin(an)* float(90)), int( cos (an) * float(90))   )
  63.         selected <- HitTest clicked hour
  64.  //       if not selected then
  65.    //         let an = (float((t1.Minute * 6) / 180)) * System.Math.PI
  66.      //       let min = new Point (int(sin(an)* float(90)), int( cos (an) * float(90))   )
  67.        //     selected <-  HitTest clicked min
  68.            
  69.         if selected then
  70.             printfn "ok troia"
  71.         else
  72.             printfn "tu ma puttana"
  73.        
  74.        
  75.     )
  76.  
  77.     override this.OnMouseUp e = ()
  78.  
  79.     override this.OnMouseMove e = ()
  80.  
  81.  
  82.  
  83.  
  84. let f = new Form(Text="Adjustable Clock", Dock=DockStyle.Fill, TopMost=true)
  85. f.Show()
  86.  
  87. f.Paint.Add(fun e ->
  88.   let g = e.Graphics
  89.   g.SmoothingMode <- Drawing2D.SmoothingMode.HighQuality
  90.   g.FillEllipse(Brushes.Aquamarine, 0, 0, 200, 200)
  91.  
  92.   g.TranslateTransform(cx, cy)
  93.   g.RotateTransform(-90.f)
  94.  
  95.   g.FillEllipse(Brushes.Black, -5, -5, 10, 10)
  96.  
  97.   let mutable s = g.Save()
  98.   for i = 1 to 12 do
  99.     g.DrawLine(Pens.Black, 90, 0, 100, 0)
  100.     g.RotateTransform(30.f)
  101.   g.Restore(s)
  102.   s <- g.Save()
  103.   for i = 1 to 60 do
  104.     g.DrawLine(Pens.Black, 95, 0, 100, 0)
  105.     g.RotateTransform(6.f)
  106.   g.Restore(s)
  107.  
  108.   )
  109.  
  110. let lanc = new Editor(Dock=DockStyle.Fill)
  111. f.Controls.Add(lanc)
  112. lanc.BackColor <- Color.Transparent
  113.  
  114. let timer = new Timer(Interval=1000)
  115. timer.Start()
  116. timer.Tick.Add(fun _ ->
  117.   lanc.Invalidate()
  118. )
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement