using System; using System.Windows.Forms; using System.Drawing; namespace CheckboxTest.NowyCheckbox { class NowyCheckbox : Control { #region Private members public bool IsChecked = false; private Label CheckBoxLabel; private Rectangle CheckBoxRectangle; private bool MouseOver = false; #endregion #region Public Members (in Attributes) private Color CheckBoxCharColorValue = Color.FromArgb(0, 0, 0); public Color CheckBoxCharColor { get { return CheckBoxCharColorValue; } set { if (CheckBoxCharColor != value) { CheckBoxCharColorValue = value; Invalidate(); } } } private Color CheckBoxCharHighlightColorValue = Color.FromArgb(0, 120, 215); public Color CheckBoxCharHighlightColor { get { return CheckBoxCharHighlightColorValue; } set { if (CheckBoxCharHighlightColor != value) { CheckBoxCharHighlightColorValue = value; Invalidate(); } } } private Font CheckBoxCharFontValue; public Font CheckBoxCharFont { get { return CheckBoxCharFontValue; } set { if (CheckBoxCharFont != value) { CheckBoxCharFontValue = value; Invalidate(); } } } private string CheckBoxCharValue = "!"; public string CheckBoxChar { get { return CheckBoxCharValue; } set { if (CheckBoxChar != value) { CheckBoxCharValue = value; RefreshLabel(); } } } private Font CheckBoxFontValue; public Font CheckBoxFont { get { return CheckBoxFontValue; } set { if (CheckBoxFont != value) { CheckBoxFontValue = value; RefreshLabel(); } } } private string CheckBoxTextValue = "Nowy CheckBox"; public string CheckBoxText { get { return CheckBoxTextValue; } set { if (CheckBoxText != value) { CheckBoxTextValue = value; RefreshLabel(); } } } private int CheckBoxSizeValue = 12; public int CheckBoxSize { get { return CheckBoxSizeValue; } set { if (CheckBoxSize != value) { CheckBoxSizeValue = value; RefreshLabel(); Invalidate(); } } } private int CheckBoxFrameStrengthValue = 1; public int CheckBoxFrameStrength { get { return CheckBoxFrameStrengthValue; } set { if (CheckBoxFrameStrengthValue != value) { CheckBoxFrameStrengthValue = value; Invalidate(); } } } #region Public Member private int CheckBoxCharOffsetXValue = 0; public int CheckBoxCharOffsetX { get { return CheckBoxCharOffsetXValue; } set { if (CheckBoxCharOffsetX != value) { CheckBoxCharOffsetXValue = value; Invalidate(); } } } private int CheckBoxCharOffsetYValue = 0; public int CheckBoxCharOffsetY { get { return CheckBoxCharOffsetYValue; } set { if (CheckBoxCharOffsetY != value) { CheckBoxCharOffsetYValue = value; Invalidate(); } } } private int CheckBoxOffsetXValue = 0; public int CheckBoxOffsetX { get { return CheckBoxOffsetXValue; } set { if (CheckBoxOffsetX != value) { CheckBoxOffsetXValue = value; RefreshLabel(); } } } private int CheckBoxOffsetYValue = 0; public int CheckBoxOffsetY { get { return CheckBoxOffsetYValue; } set { if (CheckBoxOffsetY != value) { CheckBoxOffsetYValue = value; RefreshLabel(); } } } #endregion #region Public Colors private Color CheckBoxFrameColorValue = Color.FromArgb(0, 0, 0); public Color CheckBoxFrameColor { get { return CheckBoxFrameColorValue; } set { if (CheckBoxFrameColorValue != value) { CheckBoxFrameColorValue = value; Invalidate(); } } } private Color CheckBoxFrameHighlightColorValue = Color.FromArgb(0, 120, 250); public Color CheckBoxFrameHighlightColor { get { return CheckBoxFrameHighlightColorValue; } set { if (CheckBoxFrameHighlightColorValue != value) { CheckBoxFrameHighlightColorValue = value; Invalidate(); } } } private Color CheckBoxBackColorValue = Color.FromArgb(255, 255, 255); public Color CheckBoxBackColor { get { return CheckBoxBackColorValue; } set { if (CheckBoxBackColorValue != value) { CheckBoxBackColorValue = value; Invalidate(); } } } private Color CheckBoxBackHighlightColorValue = Color.FromArgb(255, 255, 255); public Color CheckBoxBackHighlightColor { get { return CheckBoxBackHighlightColorValue; } set { if (CheckBoxBackHighlightColorValue != value) { CheckBoxBackHighlightColorValue = value; Invalidate(); } } } private Color CheckBoxForeHighlightColorValue = Color.FromArgb(0, 0, 0); public Color CheckBoxForeHighlightColor { get { return CheckBoxForeHighlightColorValue; } set { if (CheckBoxForeHighlightColorValue != value) { CheckBoxForeHighlightColorValue = value; RefreshLabel(); } } } private Color CheckBoxForeColorValue = Color.FromArgb(0, 0, 0); public Color CheckBoxForeColor { get { return CheckBoxForeColorValue; } set { if (CheckBoxForeColorValue != value) { CheckBoxForeColorValue = value; RefreshLabel(); } } } #endregion #endregion #region Constructor public NowyCheckbox() { DoubleBuffered = true; Size = new Size(112, 18); CheckBoxFont = this.Font; CheckBoxCharFont = this.Font; var midHeight = 8 - Font.Height / 2; CheckBoxLabel = new Label() { Font = CheckBoxFont, Size = new Size(this.Width - 16, this.Height), Location = new Point(16, midHeight), Text = CheckBoxText }; Controls.Add(CheckBoxLabel); CreateMouseEvents(); } #endregion #region Create mouse events private void CreateMouseEvents() { MouseEnter += (sender, e) => { OnCustomMouseEnter(e); }; MouseLeave += (sender, e) => { OnCustomMouseLeave(e); }; MouseDown += (sender, e) => { OnCustomMouseDown(e); }; CheckBoxLabel.MouseEnter += (sender, e) => { OnCustomMouseEnter(e); }; CheckBoxLabel.MouseLeave += (sender, e) => { OnCustomMouseLeave(e); }; CheckBoxLabel.MouseDown += (sender, e) => { OnCustomMouseDown(e); }; } #endregion #region Mouse Events private void OnCustomMouseDown(EventArgs e) { IsChecked = !IsChecked; Invalidate(); } private void OnCustomMouseEnter(EventArgs e) { if (MouseOver == false) { MouseOver = true; Invalidate(); RefreshLabel(); } } private void OnCustomMouseLeave(EventArgs e) { if (MouseOver == true) { MouseOver = false; Invalidate(); RefreshLabel(); } } #endregion #region Paint NowyCheckbox protected override void OnPaint(PaintEventArgs e) { base.OnPaint(e); PaintRectangle(e); if (IsChecked == true) { PaintArrowChar(e); } } #endregion #region Paint NowyCheckboxRectangle private void PaintRectangle(PaintEventArgs e) { var midHeight = Height / 2 - CheckBoxSize / 2; CheckBoxRectangle = new Rectangle(0, midHeight, CheckBoxSize, CheckBoxSize); var fillColor = MouseOver == true ? CheckBoxBackHighlightColor : CheckBoxBackColor; var frameColor = MouseOver == true ? CheckBoxFrameHighlightColor : CheckBoxFrameColor; using (var pen = new Pen(frameColor, CheckBoxFrameStrength)) { var brush = new SolidBrush(fillColor); e.Graphics.FillRectangle(brush, CheckBoxRectangle); e.Graphics.DrawRectangle(pen, CheckBoxRectangle); } } #endregion #region Paint Checkbox Arrow private void PaintArrowChar(PaintEventArgs e) { var charColor = MouseOver == true ? CheckBoxCharHighlightColor : CheckBoxCharColor; var midX = CheckBoxSize / 2 - 3 + CheckBoxCharOffsetX; var midY = Height / 2 - CheckBoxCharFont.Height / 2 + CheckBoxCharOffsetY; using (var brush = new SolidBrush(charColor)) { e.Graphics.DrawString(CheckBoxChar, CheckBoxCharFont, brush, new Point(midX, midY)); } } #endregion #region [OnResize] protected override void OnResize(EventArgs e) { base.OnResize(e); RefreshLabel(); } #endregion #region Refresh Label private void RefreshLabel() { if (CheckBoxLabel == null) return; CheckBoxLabel.Font = CheckBoxFont; CheckBoxLabel.Text = CheckBoxText; CheckBoxLabel.ForeColor = MouseOver == true ? CheckBoxForeHighlightColor : CheckBoxForeColor; var offsetWidth = Width - CheckBoxSize; CheckBoxLabel.Size = new Size(offsetWidth, Height); var offsetX = CheckBoxSize + 6 + CheckBoxOffsetX; var midHeight = Height / 2 - CheckBoxFont.Height / 2 + CheckBoxOffsetY; CheckBoxLabel.Location = new Point(offsetX, midHeight); Invalidate(); } #endregion } }