Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
Classe per creare facilmente rettangoli (filled) con angoli smussati.
open System.Windows.Forms
open System.Drawing
let f = new Form(TopMost=true, Text="Ciao")
f.Show()
type Style = Filled | Border
type RettangoloArrotondato() =
inherit UserControl()
let mutable c = Color.LightBlue
let mutable a = 30
let mutable s = Filled
member this.Color
with get() = c
and set(v) = c <- v
member this.Radius
with get() = a
and set(v) =
if (v <= 90) then a <- v else raise (System.ArgumentException("Radius must be < 90"))
member this.Style
with get() = s
and set(v) = s <- v
override this.OnPaint e =
let g = e.Graphics
g.SmoothingMode <- System.Drawing.Drawing2D.SmoothingMode.HighQuality
use b = new SolidBrush(c)
let w, h = this.Width - 1, this.Height - 1
match s with
| Filled ->
g.FillPie(b, 0, 0, a, a, 180, 90)
g.FillPie(b, w - a, 0, a, a, 270, 90)
g.FillPie(b, 0, h - a, a, a, 90, 90)
g.FillPie(b, w - a, h - a, a, a, 0, 90)
g.FillRectangle(b, a/2 - 1, 0, w - a + 2, a/2 + 1)
g.FillRectangle(b, 0, a/2 - 1, w, h - a + 2)
g.FillRectangle(b, a/2 - 1, h - a/2 - 1, w - a + 2, a/2 + 1)
| Border ->
use p = new Pen(b, Width=2.0f)
g.DrawArc(p, 0, 0, a, a, 180, 90)
g.DrawArc(p, w - a, 0, a, a, 270, 90)
g.DrawArc(p, 0, h - a, a, a, 90, 90)
g.DrawArc(p, 0 + w - a, 0 + h - a, a, a, 0, 90)
g.DrawLine(p, a/2 - 1, 0, w - a/2, 0)
g.DrawLine(p, a/2 - 1, h, w - a/2, h)
g.DrawLine(p, 0, a/2, 0, h - a/2)
g.DrawLine(p, w, a/2, w, h - a/2)
| _ -> raise (System.ArgumentException("Invalid style type, can be Filled or Border."))
let r = new RettangoloArrotondato(
Size=Size(250,100),
Location=Point(10, 10),
Color=Color.LightBlue,
Radius=30,
Style=Filled
)
let r2 = new RettangoloArrotondato(
Size=Size(250,100),
Location=Point(10, 150),
Color=Color.LightBlue,
Radius=30,
Style=Border
)
f.Controls.Add(r)
f.Controls.Add(r2)
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.