tangxu
2024-12-24 91105b77c916d06dd30380e20594e29f85eae3da
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
#region Imports
 
using System;
using System.ComponentModel;
using System.Drawing;
using System.Drawing.Drawing2D;
using System.Windows.Forms;
 
#endregion
 
namespace DPumpHydr.WinFrmUI.RLT.Controls
{
    #region DungeonRadioButton
 
    [DefaultEvent("CheckedChanged")]
    public class DungeonRadioButton : Control
    {
 
        #region Enums
 
        public enum MouseState : byte
        {
            None = 0,
            Over = 1,
            Down = 2,
            Block = 3
        }
 
        #endregion
        #region Variables
 
        private bool _Checked;
 
        public event CheckedChangedEventHandler CheckedChanged;
        public delegate void CheckedChangedEventHandler(object sender);
 
        #endregion
        #region Properties
 
        public Color BorderColor { get; set; } = Color.FromArgb(182, 88, 55);
 
        public Color CheckedBackColorA { get; set; } = Color.FromArgb(213, 85, 32);
 
        public Color CheckedBackColorB { get; set; } = Color.FromArgb(224, 123, 82);
 
        public Color CheckedColor { get; set; } = Color.FromArgb(255, 255, 255);
 
        public bool Checked
        {
            get => _Checked;
            set
            {
                _Checked = value;
                InvalidateControls();
                CheckedChanged?.Invoke(this);
                Invalidate();
            }
        }
 
        #endregion
        #region EventArgs
 
        protected override void OnTextChanged(EventArgs e)
        {
            Invalidate();
            base.OnTextChanged(e);
        }
 
        protected override void OnResize(EventArgs e)
        {
            base.OnResize(e);
            Height = 15;
        }
 
        protected override void OnMouseDown(MouseEventArgs e)
        {
            if (!_Checked)
            {
                Checked = true;
            }
 
            base.OnMouseDown(e);
            Focus();
        }
 
        #endregion
 
        public DungeonRadioButton()
        {
            SetStyle(ControlStyles.AllPaintingInWmPaint | ControlStyles.OptimizedDoubleBuffer | ControlStyles.ResizeRedraw | ControlStyles.SupportsTransparentBackColor | ControlStyles.UserPaint, true);
            BackColor = Color.Transparent;
            Font = new("Segoe UI", 12);
            Width = 180;
            ForeColor = Color.FromArgb(76, 76, 95);
            Cursor = Cursors.Hand;
        }
 
        private void InvalidateControls()
        {
            if (!IsHandleCreated || !_Checked)
            {
                return;
            }
 
            foreach (Control _Control in Parent.Controls)
            {
                if (!object.ReferenceEquals(_Control, this) && _Control is DungeonRadioButton button)
                {
                    button.Checked = false;
                }
            }
        }
 
        protected override void OnPaint(PaintEventArgs e)
        {
            base.OnPaint(e);
            Graphics MyDrawer = e.Graphics;
 
            MyDrawer.Clear(BackColor);
            MyDrawer.SmoothingMode = SmoothingMode.AntiAlias;
 
            // Fill the body of the ellipse with a gradient
            LinearGradientBrush LGB = new(new Rectangle(new Point(0, 0), new Size(14, 14)), CheckedBackColorA, CheckedBackColorB, 90);
            MyDrawer.FillEllipse(LGB, new Rectangle(new Point(0, 0), new Size(14, 14)));
 
            GraphicsPath GP = new();
            GP.AddEllipse(new Rectangle(0, 0, 14, 14));
            MyDrawer.SetClip(GP);
            MyDrawer.ResetClip();
 
            // Draw ellipse border
            MyDrawer.DrawEllipse(new(BorderColor), new Rectangle(new Point(0, 0), new Size(14, 14)));
 
            // Draw an ellipse inside the body
            if (_Checked)
            {
                SolidBrush EllipseColor = new(CheckedColor);
                MyDrawer.FillEllipse(EllipseColor, new Rectangle(new Point(4, 4), new Size(6, 6)));
            }
            MyDrawer.DrawString(Text, Font, new SolidBrush(ForeColor), 16, 7, new StringFormat { LineAlignment = StringAlignment.Center });
            e.Dispose();
        }
    }
 
    #endregion
}