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
#region Imports
 
using System;
using System.ComponentModel;
using System.Drawing;
using System.Windows.Forms;
using static DPumpHydr.WinFrmUI.RLT.Helper.CrownHelper;
 
#endregion
 
namespace DPumpHydr.WinFrmUI.RLT.Controls
{
    #region CrownGroupBox
 
    public class CrownGroupBox : System.Windows.Forms.GroupBox
    {
        #region Properties
 
        private Color _borderColor = ThemeProvider.Theme.Colors.DarkBorder;
 
        [Category("Appearance")]
        [Description("Determines the color of the border.")]
        public Color BorderColor
        {
            get => _borderColor;
            set
            {
                _borderColor = value;
                Invalidate();
            }
        }
 
        #endregion
 
        #region Constructor
 
        public CrownGroupBox()
        {
            SetStyle
            (
                ControlStyles.OptimizedDoubleBuffer |
                ControlStyles.ResizeRedraw |
                ControlStyles.UserPaint,
                    true
            );
 
            ResizeRedraw = true;
            DoubleBuffered = true;
        }
 
        #endregion
 
        #region Event
 
        protected override void OnPaint(PaintEventArgs e)
        {
            Graphics g = e.Graphics;
            Rectangle rect = new(0, 0, ClientSize.Width, ClientSize.Height);
            SizeF stringSize = g.MeasureString(Text, Font);
 
            Color textColor = ThemeProvider.Theme.Colors.LightText;
            Color fillColor = ThemeProvider.Theme.Colors.GreyBackground;
 
            using (SolidBrush b = new(fillColor))
            {
                g.FillRectangle(b, rect);
            }
 
            using (Pen p = new(BorderColor, 1))
            {
                Rectangle borderRect = new(0, (int)stringSize.Height / 2, rect.Width - 1, rect.Height - ((int)stringSize.Height / 2) - 1);
                g.DrawRectangle(p, borderRect);
            }
 
            Rectangle textRect = new(rect.Left + ThemeProvider.Theme.Sizes.Padding, rect.Top, rect.Width - (ThemeProvider.Theme.Sizes.Padding * 2), (int)stringSize.Height);
 
            using (SolidBrush b2 = new(fillColor))
            {
                Rectangle modRect = new(textRect.Left, textRect.Top, Math.Min(textRect.Width, (int)stringSize.Width), textRect.Height);
                g.FillRectangle(b2, modRect);
            }
 
            using (SolidBrush b = new(textColor))
            {
                StringFormat stringFormat = new()
                {
                    LineAlignment = StringAlignment.Center,
                    Alignment = StringAlignment.Near,
                    FormatFlags = StringFormatFlags.NoWrap,
                    Trimming = StringTrimming.EllipsisCharacter
                };
 
                g.DrawString(Text, Font, b, textRect, stringFormat);
            }
        }
 
        #endregion
    }
 
    #endregion
}