Dreamine.UI.WinForms 1.0.1
Dreamine.UI.WinForms 사용자 인터페이스 기능과 구성 요소를 제공합니다.
로딩중...
검색중...
일치하는것 없음
DreamineButton.cs
이 파일의 문서화 페이지로 가기
1using System.Drawing;
2using System.Drawing.Drawing2D;
3using System.Windows.Input;
5
7
16public class DreamineButton : Control
17{
26 private bool _isHover;
35 private bool _isPressed;
36
37 // ── Properties ────────────────────────────────────────
46 private string _content = string.Empty;
55 public string Content
56 {
57 get => _content;
58 set { _content = value; Invalidate(); }
59 }
60
69 private Color _shineColor = Color.Empty;
78 public Color ShineColor
79 {
80 get => _shineColor;
81 set { _shineColor = value; Invalidate(); }
82 }
83
101 public Color BorderColor
102 {
103 get => _borderColor;
104 set { _borderColor = value; Invalidate(); }
105 }
106
124 public int CornerRadius
125 {
126 get => _cornerRadius;
127 set { _cornerRadius = value; Invalidate(); }
128 }
129
138 private bool _isSelected;
147 public bool IsSelected
148 {
149 get => _isSelected;
150 set { _isSelected = value; Invalidate(); }
151 }
152
161 public ICommand? Command { get; set; }
170 public object? CommandParameter { get; set; }
171
172 // ── Constructor ───────────────────────────────────────
182 {
183 SetStyle(
184 ControlStyles.AllPaintingInWmPaint |
185 ControlStyles.UserPaint |
186 ControlStyles.DoubleBuffer |
187 ControlStyles.ResizeRedraw |
188 ControlStyles.SupportsTransparentBackColor, true);
189
190 BackColor = DreamineTheme.NavBackground;
191 ForeColor = DreamineTheme.TextPrimary;
192 Font = new Font("Segoe UI", 10f, FontStyle.Regular, GraphicsUnit.Point);
193 Size = new Size(100, 34);
194 Cursor = Cursors.Hand;
195 }
196
197 // ── Mouse Events ──────────────────────────────────────
214 protected override void OnMouseEnter(EventArgs e)
215 {
216 base.OnMouseEnter(e);
217 _isHover = true;
218 Invalidate();
219 }
220
236 protected override void OnMouseLeave(EventArgs e)
237 {
238 base.OnMouseLeave(e);
239 _isHover = false;
240 _isPressed = false;
241 Invalidate();
242 }
243
259 protected override void OnMouseDown(MouseEventArgs e)
260 {
261 base.OnMouseDown(e);
262 if (e.Button == MouseButtons.Left) { _isPressed = true; Invalidate(); }
263 }
264
280 protected override void OnMouseUp(MouseEventArgs e)
281 {
282 base.OnMouseUp(e);
283 if (e.Button == MouseButtons.Left)
284 {
285 _isPressed = false;
286 Invalidate();
287 if (ClientRectangle.Contains(e.Location))
289 }
290 }
291
300 private void ExecuteCommand()
301 {
302 if (Command?.CanExecute(CommandParameter) == true)
303 Command.Execute(CommandParameter);
304 }
305
323 {
324 for (Control? p = Parent; p != null; p = p.Parent)
325 {
326 if (p.BackColor.A == 255)
327 return p.BackColor;
328 }
329 return FindForm()?.BackColor ?? DreamineTheme.AppBackground;
330 }
331
332 // ── Paint ─────────────────────────────────────────────
349 protected override void OnPaint(PaintEventArgs e)
350 {
351 var g = e.Graphics;
352 var rect = new Rectangle(1, 1, Width - 2, Height - 2);
353
354 // 둥근 사각형 바깥쪽(네 귀퉁이)을 부모의 실제 배경색으로 먼저 채운다.
355 // Win32 Region으로 잘라내는 방식은 안티앨리어싱이 없어 작은 반경에서 오히려
356 // 더 거칠게 보이므로, 대신 "부모 배경색 채우기 + AA 처리된 둥근 도형"으로
357 // 귀퉁이가 부모 배경에 자연스럽게 섞여 보이도록 한다(부모가 단색 배경일 때 효과적).
358 using (var cornerBrush = new SolidBrush(GetEffectiveParentBackColor()))
359 g.FillRectangle(cornerBrush, 0, 0, Width, Height);
360
361 // background color
362 var bg = BackColor;
363 if (_isPressed)
365 else if (_isHover)
367
368 var topColor = _isSelected
370 : bg;
371
372 // border
373 var borderColor = _isSelected || _isHover
375 : _borderColor;
376
377 using var borderPen = new Pen(borderColor, 1f);
378 DreamineDrawHelper.FillRoundedGradient(g, topColor, bg, borderPen, rect, _cornerRadius);
379
380 // shine overlay
381 if (_shineColor != Color.Empty)
383
384 // selected indicator (bottom line)
385 if (_isSelected)
386 {
387 using var selPen = new Pen(DreamineTheme.AccentBlue, 2f);
388 int y = Height - 3;
389 g.DrawLine(selPen, _cornerRadius, y, Width - _cornerRadius, y);
390 }
391
392 // text
393 DreamineDrawHelper.DrawCenteredText(g, _content, Font, Enabled ? ForeColor : DreamineTheme.TextSecondary, rect);
394 }
395}
override void OnMouseDown(MouseEventArgs e)
override void OnPaint(PaintEventArgs e)
override void OnMouseUp(MouseEventArgs e)
static Color Blend(Color base_, Color overlay, float alpha)
static void FillRoundedGradient(Graphics g, Color top, Color bottom, Pen? border, Rectangle rect, float radius)
static void DrawCenteredText(Graphics g, string text, Font font, Color color, Rectangle rect)
static void DrawShineOverlay(Graphics g, Color shineColor, Rectangle rect, float radius)
static readonly Color TextSecondary
static readonly Color AppBackground
static readonly Color NavBackground
static readonly Color BorderFocus
static readonly Color TextPrimary
static readonly Color BorderNormal