Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
Orologio analogico
open System.Windows.Forms
open System.Drawing
let f = new Form(TopMost=true, Text="Clock", Size=Size(400, 400))
f.Show()
let pi = System.Math.PI
type AnalogClock() =
inherit UserControl()
do base.SetStyle(ControlStyles.OptimizedDoubleBuffer ||| ControlStyles.AllPaintingInWmPaint, true)
let mutable current = System.DateTime.Now
let mutable scala = 100
member this.DateTime
with get() = current
and set(v) = current <- v; this.Invalidate()
member this.CambiaScala
with get() = scala
and set(v) = scala <- v
override this.OnPaint e =
let g = e.Graphics
g.SmoothingMode <- Drawing2D.SmoothingMode.HighQuality
let m = min this.Width this.Height
let w, h = m, m
let c = Point(50, 50)
let cf = PointF(50.f, 50.f)
let newscala = single scala / 100.f
g.ScaleTransform(newscala, newscala)
let da = pi / 6.
let transform = g.Transform
g.DrawEllipse(Pens.Black, 1, 1, 98, 98)
for i = 1 to 12 do
g.DrawLine(Pens.Black, c.X + 50 - 5, c.Y, c.X + 49, c.Y)
let t = g.Transform
t.RotateAt(30.f, cf)
g.Transform <- t
g.Transform <- transform
let drawRotatedLine(p, a, x1:int, y1:int, x2:int, y2:int) =
let t = g.Transform
t.RotateAt(a, cf)
g.Transform <- t
g.DrawLine(p, x1, y1, x2, y2)
g.Transform <- transform
let sa = single((float current.Second) * (180. / 30.) - 180. / 2.)
let msa = single((float current.Millisecond) * ( 180. / 30. ) / 1000. )
drawRotatedLine(Pens.Black, sa + msa, c.X - 5, c.Y, c.X + 49, c.Y)
use mp = new Pen(Brushes.Black, Width=2.f)
let sm = single((float current.Minute) * (180. / 30.) - 180. / 2.)
drawRotatedLine(mp, sm, c.X - 5, c.Y, c.X + 40 , c.Y)
use hp = new Pen(Brushes.Black, Width=3.f)
let sp = single((float current.Hour) * (180. / 6.) - 180. / 2.)
drawRotatedLine(hp, sp, c.X - 5, c.Y, c.X + 30, c.Y)
let init (c:AnalogClock) =
c.Size <- f.ClientSize
c.CambiaScala <- min f.ClientSize.Height f.ClientSize.Width
let c = new AnalogClock(Location=Point(0, 0))
init(c)
f.Controls.Add(c)
f.ResizeEnd.Add(fun e ->
init(c)
)
let t = new Timer(Interval=25)
t.Tick.Add(fun _ ->
f.Invalidate()
c.DateTime <- System.DateTime.Now
)
t.Start()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.