Dreamine.UI.WinForms 1.0.1
Dreamine.UI.WinForms 사용자 인터페이스 기능과 구성 요소를 제공합니다.
로딩중...
검색중...
일치하는것 없음
DreamineComboBox.cs
이 파일의 문서화 페이지로 가기
1using System.Drawing;
2using System.Drawing.Drawing2D;
4
6
15public class DreamineComboBox : UserControl
16{
25 private readonly ComboBox _inner;
34 private bool _isFocused;
35
36 // ── Properties ────────────────────────────────────────
45 public ComboBox.ObjectCollection Items => _inner.Items;
46
55 public object? SelectedItem
56 {
57 get => _inner.SelectedItem;
58 set => _inner.SelectedItem = value;
59 }
60
69 public int SelectedIndex
70 {
71 get => _inner.SelectedIndex;
72 set => _inner.SelectedIndex = value;
73 }
74
83 public override Color ForeColor
84 {
85 get => base.ForeColor;
86 set { base.ForeColor = value; _inner.ForeColor = value; }
87 }
88
97 public new Font Font
98 {
99 get => base.Font;
100 set { base.Font = value; _inner.Font = value; }
101 }
102
111 public event EventHandler? SelectedIndexChanged;
112
113 // ── Constructor ───────────────────────────────────────
123 {
124 // _inner must be created first — SetStyle / property setters trigger
125 // OnLayout and ForeColor/Font overrides before the field is otherwise set.
126 _inner = new ComboBox
127 {
128 FlatStyle = FlatStyle.Flat,
129 BackColor = DreamineTheme.InputBackground,
131 Font = new Font("Segoe UI", 10f, FontStyle.Regular, GraphicsUnit.Point),
132 DropDownStyle = ComboBoxStyle.DropDownList,
133 };
134
135 _inner.GotFocus += (_, _) => { _isFocused = true; Invalidate(); };
136 _inner.LostFocus += (_, _) => { _isFocused = false; Invalidate(); };
137 _inner.SelectedIndexChanged += (s, e) => SelectedIndexChanged?.Invoke(this, e);
138 _inner.DrawMode = DrawMode.OwnerDrawFixed;
139 _inner.DrawItem += OnDrawItem;
140
141 SetStyle(
142 ControlStyles.AllPaintingInWmPaint |
143 ControlStyles.UserPaint |
144 ControlStyles.DoubleBuffer |
145 ControlStyles.ResizeRedraw, true);
146
147 BackColor = DreamineTheme.InputBackground;
149 Font = _inner.Font;
150 Height = 36;
151
152 Controls.Add(_inner);
153 }
154
171 protected override void OnLayout(LayoutEventArgs e)
172 {
173 base.OnLayout(e);
174 if (_inner == null) return;
175 _inner.SetBounds(0, (Height - _inner.PreferredHeight) / 2,
176 Width, _inner.PreferredHeight);
177 }
178
203 private void OnDrawItem(object? sender, DrawItemEventArgs e)
204 {
205 if (e.Index < 0) return;
206 var isSelected = (e.State & DrawItemState.Selected) != 0;
207 var isHover = (e.State & DrawItemState.HotLight) != 0;
208
209 var bg = isSelected
211 : isHover
212 ? Color.FromArgb(0xFF, 0x1E, 0x4A, 0x80)
214
215 using var bgBrush = new SolidBrush(bg);
216 e.Graphics.FillRectangle(bgBrush, e.Bounds);
217
218 string? text = _inner.Items[e.Index]?.ToString();
219 if (!string.IsNullOrEmpty(text))
220 {
221 using var textBrush = new SolidBrush(DreamineTheme.TextPrimary);
222 var sf = new StringFormat { LineAlignment = StringAlignment.Center };
223 e.Graphics.TextRenderingHint = System.Drawing.Text.TextRenderingHint.ClearTypeGridFit;
224 e.Graphics.DrawString(text, e.Font!, textBrush,
225 new Rectangle(e.Bounds.X + 6, e.Bounds.Y, e.Bounds.Width - 6, e.Bounds.Height), sf);
226 }
227 }
228
245 protected override void OnPaint(PaintEventArgs e)
246 {
247 var g = e.Graphics;
248 var rect = new Rectangle(0, 0, Width - 1, Height - 1);
249 using var bgBrush = new SolidBrush(BackColor);
251 using var pen = new Pen(borderColor, 1.5f);
253 }
254
263 protected override Size DefaultSize => new(200, 36);
264}
override void OnLayout(LayoutEventArgs e)
void OnDrawItem(object? sender, DrawItemEventArgs e)
static void FillRoundedRect(Graphics g, Brush fill, Pen? border, Rectangle rect, float radius)
static readonly Color BorderFocus
static readonly Color TextPrimary
static readonly Color BorderNormal
static readonly Color InputBackground