yangyin
2025-03-27 b0de14c2670b9ff0079dacfb4b7457b438368f11
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
147
148
149
150
151
152
153
#region Imports
 
using System.ComponentModel;
using System.Drawing;
using System.Windows.Forms;
using static DPumpHydr.WinFrmUI.RLT.Helper.CrownHelper;
 
#endregion
 
namespace DPumpHydr.WinFrmUI.RLT.Controls
{
    #region CrownSectionPanel
 
    public class CrownSectionPanel : System.Windows.Forms.Panel
    {
        #region Field Region
 
        private string _sectionHeader;
 
        #endregion
 
        #region Property Region
 
        [Browsable(false)]
        [DesignerSerializationVisibility(DesignerSerializationVisibility.Hidden)]
        public new Padding Padding => base.Padding;
 
        [Category("Appearance")]
        [Description("The section header text associated with this control.")]
        public string SectionHeader
        {
            get => _sectionHeader;
            set
            {
                _sectionHeader = value;
                Invalidate();
            }
        }
 
        #endregion
 
        #region Constructor Region
 
        public CrownSectionPanel()
        {
            SetStyle(ControlStyles.OptimizedDoubleBuffer |
                     ControlStyles.ResizeRedraw |
                     ControlStyles.UserPaint, true);
 
            base.Padding = new Padding(1, 25, 1, 1);
        }
 
        #endregion
 
        #region Event Handler Region
 
        protected override void OnEnter(System.EventArgs e)
        {
            base.OnEnter(e);
 
            Invalidate();
        }
 
        protected override void OnLeave(System.EventArgs e)
        {
            base.OnLeave(e);
 
            Invalidate();
        }
 
        protected override void OnMouseDown(MouseEventArgs e)
        {
            base.OnMouseDown(e);
 
            if (Controls.Count > 0)
            {
                Controls[0].Focus();
            }
        }
 
        #endregion
 
        #region Paint Region
 
        protected override void OnPaint(PaintEventArgs e)
        {
            Graphics g = e.Graphics;
            Rectangle rect = ClientRectangle;
 
            // Fill body
            using (SolidBrush b = new(ThemeProvider.Theme.Colors.GreyBackground))
            {
                g.FillRectangle(b, rect);
            }
 
            // Draw header
            Color bgColor = ContainsFocus ? ThemeProvider.Theme.Colors.BlueBackground : ThemeProvider.Theme.Colors.HeaderBackground;
            Color darkColor = ContainsFocus ? ThemeProvider.Theme.Colors.DarkBlueBorder : ThemeProvider.Theme.Colors.DarkBorder;
            Color lightColor = ContainsFocus ? ThemeProvider.Theme.Colors.LightBlueBorder : ThemeProvider.Theme.Colors.LightBorder;
 
            using (SolidBrush b = new(bgColor))
            {
                Rectangle bgRect = new(0, 0, rect.Width, 25);
                g.FillRectangle(b, bgRect);
            }
 
            using (Pen p = new(darkColor))
            {
                g.DrawLine(p, rect.Left, 0, rect.Right, 0);
                g.DrawLine(p, rect.Left, 25 - 1, rect.Right, 25 - 1);
            }
 
            using (Pen p = new(lightColor))
            {
                g.DrawLine(p, rect.Left, 1, rect.Right, 1);
            }
 
            int xOffset = 3;
 
            using (SolidBrush b = new(ThemeProvider.Theme.Colors.LightText))
            {
                Rectangle textRect = new(xOffset, 0, rect.Width - 4 - xOffset, 25);
 
                StringFormat format = new()
                {
                    Alignment = StringAlignment.Near,
                    LineAlignment = StringAlignment.Center,
                    FormatFlags = StringFormatFlags.NoWrap,
                    Trimming = StringTrimming.EllipsisCharacter
                };
 
                g.DrawString(SectionHeader, Font, b, textRect, format);
            }
 
            // Draw border
            using (Pen p = new(ThemeProvider.Theme.Colors.DarkBorder, 1))
            {
                Rectangle modRect = new(rect.Left, rect.Top, rect.Width - 1, rect.Height - 1);
 
                g.DrawRectangle(p, modRect);
            }
        }
 
        protected override void OnPaintBackground(PaintEventArgs e)
        {
            // Absorb event
        }
 
        #endregion
    }
 
    #endregion
}