Dreamine.UI.WinForms 1.0.1
Dreamine.UI.WinForms 사용자 인터페이스 기능과 구성 요소를 제공합니다.
로딩중...
검색중...
일치하는것 없음
DreamineRadioButton.cs
이 파일의 문서화 페이지로 가기
1using System.Drawing;
2using System.Drawing.Drawing2D;
4
6
15public class DreamineRadioButton : Control
16{
25 private bool _isHover;
34 private bool _isChecked;
35
44 public bool IsChecked
45 {
46 get => _isChecked;
47 set
48 {
49 if (_isChecked == value) return;
50 _isChecked = value;
51 Invalidate();
52 if (value) UncheckSiblings();
53 CheckedChanged?.Invoke(this, EventArgs.Empty);
54 }
55 }
56
65 private string _content = string.Empty;
74 public string Content
75 {
76 get => _content;
77 set { _content = value; Invalidate(); }
78 }
79
88 public string GroupName { get; set; } = string.Empty;
89
98 public event EventHandler? CheckedChanged;
99
108 private const int BulletSize = 16;
117 private const int BulletMargin = 2;
118
128 {
129 SetStyle(
130 ControlStyles.AllPaintingInWmPaint |
131 ControlStyles.UserPaint |
132 ControlStyles.DoubleBuffer |
133 ControlStyles.ResizeRedraw |
134 ControlStyles.SupportsTransparentBackColor, true);
135
136 BackColor = Color.Transparent;
137 ForeColor = DreamineTheme.TextPrimary;
138 Font = new Font("Segoe UI", 10f, FontStyle.Regular, GraphicsUnit.Point);
139 Height = 24;
140 Cursor = Cursors.Hand;
141 }
142
159 protected override void OnMouseEnter(EventArgs e) { base.OnMouseEnter(e); _isHover = true; Invalidate(); }
176 protected override void OnMouseLeave(EventArgs e) { base.OnMouseLeave(e); _isHover = false; Invalidate(); }
177
194 protected override void OnMouseUp(MouseEventArgs e)
195 {
196 base.OnMouseUp(e);
197 if (Enabled && e.Button == MouseButtons.Left && ClientRectangle.Contains(e.Location))
198 IsChecked = true;
199 }
200
209 private void UncheckSiblings()
210 {
211 if (Parent == null || string.IsNullOrEmpty(GroupName)) return;
212 foreach (Control c in Parent.Controls)
213 {
214 if (c != this && c is DreamineRadioButton rb &&
215 rb.GroupName == GroupName && rb.IsChecked)
216 {
217 rb._isChecked = false;
218 rb.Invalidate();
219 rb.CheckedChanged?.Invoke(rb, EventArgs.Empty);
220 }
221 }
222 }
223
240 protected override void OnPaint(PaintEventArgs e)
241 {
242 var g = e.Graphics;
243 g.SmoothingMode = SmoothingMode.AntiAlias;
244
245 int x = BulletMargin, y = (Height - BulletSize) / 2;
246 var bulletRect = new Rectangle(x, y, BulletSize, BulletSize);
247
248 // Outer circle
250 using var bgBrush = new SolidBrush(_isChecked ? DreamineTheme.AccentBlue : DreamineTheme.InputBackground);
251 using var borderPen = new Pen(borderColor, 1.5f);
252 g.FillEllipse(bgBrush, bulletRect);
253 g.DrawEllipse(borderPen, new Rectangle(x, y, BulletSize - 1, BulletSize - 1));
254
255 // Inner dot
256 if (_isChecked)
257 {
258 int dotSize = 6, dotX = x + (BulletSize - dotSize) / 2, dotY = y + (BulletSize - dotSize) / 2;
259 using var dotBrush = new SolidBrush(Color.White);
260 g.FillEllipse(dotBrush, dotX, dotY, dotSize, dotSize);
261 }
262
263 // Label
264 using var brush = new SolidBrush(Enabled ? ForeColor : DreamineTheme.TextSecondary);
265 int textX = BulletMargin + BulletSize + 6;
266 var sf = new StringFormat { LineAlignment = StringAlignment.Center };
267 g.TextRenderingHint = System.Drawing.Text.TextRenderingHint.ClearTypeGridFit;
268 g.DrawString(_content, Font, brush, new Rectangle(textX, 0, Width - textX, Height), sf);
269 }
270
279 protected override Size DefaultSize => new(160, 24);
280}
static readonly Color TextSecondary
static readonly Color BorderFocus
static readonly Color TextPrimary
static readonly Color BorderNormal
static readonly Color InputBackground