Levelcode-IBRCAD/IBRCAD/RadioButtonNT/RadioButtonCustom.cs

88 lines
3.1 KiB
C#

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Drawing;
using System.Drawing.Drawing2D;
using System.Windows.Forms;
namespace ControllCustoms.RadioButtonNT
{
public class RadioButtonCustom : RadioButton
{
//propriedades privadas
private Color checkedColor = Color.MediumSlateBlue;
private Color unCheckedColor = Color.Gray;
//propriedades publicas
public Color CheckedColor { get => checkedColor; set => checkedColor = value; }
public Color UnCheckedColor { get => unCheckedColor; set => unCheckedColor = value; }
//construtor
public RadioButtonCustom()
{
this.MinimumSize = new Size(0, 21);
//Add a padding of 10 to the left to have a considerable distance between the text and the RadioButton.
this.Padding = new Padding(10, 0, 0, 0);
}//end construtor
protected override void OnPaint(PaintEventArgs pevent)
{
//Fields
Graphics graphics = pevent.Graphics;
graphics.SmoothingMode = SmoothingMode.AntiAlias;
float rbBorderSize = 18F;
float rbCheckSize = 12F;
RectangleF rectRbBorder = new RectangleF()
{
X = 0.5F,
Y = (this.Height - rbBorderSize) / 2, //Center
Width = rbBorderSize,
Height = rbBorderSize
};
RectangleF rectRbCheck = new RectangleF()
{
X = rectRbBorder.X + ((rectRbBorder.Width - rbCheckSize) / 2), //Center
Y = (this.Height - rbCheckSize) / 2, //Center
Width = rbCheckSize,
Height = rbCheckSize
};
//Drawing
using (Pen penBorder = new Pen(checkedColor, 1.6F))
using (SolidBrush brushRbCheck = new SolidBrush(checkedColor))
using (SolidBrush brushText = new SolidBrush(this.ForeColor))
{
//Draw surface
graphics.Clear(this.BackColor);
//Draw Radio Button
if (this.Checked)
{
graphics.DrawEllipse(penBorder, rectRbBorder);//Circle border
graphics.FillEllipse(brushRbCheck, rectRbCheck); //Circle Radio Check
}
else
{
penBorder.Color = unCheckedColor;
graphics.DrawEllipse(penBorder, rectRbBorder); //Circle border
}
//Draw text
graphics.DrawString(this.Text, this.Font, brushText,
rbBorderSize + 8, (this.Height - TextRenderer.MeasureText(this.Text, this.Font).Height) / 2);//Y=Center
}
}
//X-> Obsolete code, this was replaced by the Padding property in the constructor
//(this.Padding = new Padding(10,0,0,0);)
//protected override void OnResize(EventArgs e)
//{
// base.OnResize(e);
// this.Width = TextRenderer.MeasureText(this.Text, this.Font).Width + 30;
//}
}//end OnPaint
}