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
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
#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 CrownComboBox
 
    public class CrownComboBox : ComboBox
    {
        [Browsable(false)]
        [DesignerSerializationVisibility(DesignerSerializationVisibility.Hidden)]
        public new Color ForeColor { get; set; }
 
        [Browsable(false)]
        [DesignerSerializationVisibility(DesignerSerializationVisibility.Hidden)]
        public new Color BackColor { get; set; }
 
        [Browsable(false)]
        [DesignerSerializationVisibility(DesignerSerializationVisibility.Hidden)]
        public new FlatStyle FlatStyle { get; set; }
 
        [Browsable(false)]
        [DesignerSerializationVisibility(DesignerSerializationVisibility.Hidden)]
        public new ComboBoxStyle DropDownStyle { get; set; }
 
        private Bitmap _buffer;
 
        public CrownComboBox() : base()
        {
            SetStyle
            (
                ControlStyles.OptimizedDoubleBuffer |
                ControlStyles.ResizeRedraw |
                ControlStyles.UserPaint,
                    true
            );
 
            DrawMode = DrawMode.OwnerDrawVariable;
 
            base.FlatStyle = FlatStyle.Flat;
            base.DropDownStyle = ComboBoxStyle.DropDownList;
        }
 
        protected override void Dispose(bool disposing)
        {
            if (disposing)
            {
                _buffer = null;
            }
 
            base.Dispose(disposing);
        }
 
        protected override void OnTabStopChanged(EventArgs e)
        {
            base.OnTabStopChanged(e);
            Invalidate();
        }
 
        protected override void OnTabIndexChanged(EventArgs e)
        {
            base.OnTabIndexChanged(e);
            Invalidate();
        }
 
        protected override void OnGotFocus(EventArgs e)
        {
            base.OnGotFocus(e);
            Invalidate();
        }
 
        protected override void OnLostFocus(EventArgs e)
        {
            base.OnLostFocus(e);
            Invalidate();
        }
 
        protected override void OnTextChanged(EventArgs e)
        {
            base.OnTextChanged(e);
            Invalidate();
        }
 
        protected override void OnTextUpdate(EventArgs e)
        {
            base.OnTextUpdate(e);
            Invalidate();
        }
 
        protected override void OnSelectedValueChanged(EventArgs e)
        {
            base.OnSelectedValueChanged(e);
            Invalidate();
        }
        protected override void OnVisibleChanged(EventArgs e)
        {
            base.OnVisibleChanged(e);
            Invalidate();
        }
 
        protected override void OnInvalidated(InvalidateEventArgs e)
        {
            base.OnInvalidated(e);
            PaintCombobox();
        }
 
        protected override void OnResize(EventArgs e)
        {
            base.OnResize(e);
            _buffer = null;
            Invalidate();
        }
 
        protected override void OnEnabledChanged(EventArgs e)
        {
            base.OnEnabledChanged(e);
            _buffer = null;
            Invalidate();
        }
 
        private void PaintCombobox()
        {
            if (_buffer == null)
            {
                _buffer = new(ClientRectangle.Width, ClientRectangle.Height);
            }
 
            using Graphics g = Graphics.FromImage(_buffer);
            Rectangle rect = new(0, 0, ClientSize.Width, ClientSize.Height);
 
            Color textColor = Enabled ? ThemeProvider.Theme.Colors.LightText : ThemeProvider.Theme.Colors.DisabledText;
 
            Color borderColor = ThemeProvider.Theme.Colors.GreySelection;
            Color fillColor = ThemeProvider.Theme.Colors.LightBackground;
 
            if (Focused && TabStop)
            {
                borderColor = ThemeProvider.Theme.Colors.BlueHighlight;
            }
 
            using (SolidBrush b = new(fillColor))
            {
                g.FillRectangle(b, rect);
            }
 
            using (Pen p = new(borderColor, 1))
            {
                Rectangle modRect = new(rect.Left, rect.Top, rect.Width - 1, rect.Height - 1);
                g.DrawRectangle(p, modRect);
            }
 
            Bitmap icon = Properties.Resources.scrollbar_arrow_hot;
            g.DrawImageUnscaled(icon, rect.Right - icon.Width - (ThemeProvider.Theme.Sizes.Padding / 2), (rect.Height / 2) - (icon.Height / 2));
 
            string text = SelectedItem != null ? SelectedItem.ToString() : Text;
 
            using (SolidBrush b = new(textColor))
            {
                int padding = 2;
 
                Rectangle modRect = new(rect.Left + padding, rect.Top + padding, rect.Width - icon.Width - (ThemeProvider.Theme.Sizes.Padding / 2) - (padding * 2), rect.Height - (padding * 2));
 
                StringFormat stringFormat = new()
                {
                    LineAlignment = StringAlignment.Center,
                    Alignment = StringAlignment.Near,
                    FormatFlags = StringFormatFlags.NoWrap,
                    Trimming = StringTrimming.EllipsisCharacter
                };
 
                g.DrawString(text, Font, b, modRect, stringFormat);
            }
        }
 
        protected override void OnPaint(PaintEventArgs e)
        {
            if (_buffer == null)
            {
                PaintCombobox();
            }
 
            Graphics g = e.Graphics;
            g.DrawImageUnscaled(_buffer, Point.Empty);
        }
 
        protected override void OnDrawItem(DrawItemEventArgs e)
        {
            Graphics g = e.Graphics;
            Rectangle rect = e.Bounds;
 
            Color textColor = ThemeProvider.Theme.Colors.LightText;
            Color fillColor = ThemeProvider.Theme.Colors.LightBackground;
 
            if ((e.State & DrawItemState.Selected) == DrawItemState.Selected || (e.State & DrawItemState.Focus) == DrawItemState.Focus || (e.State & DrawItemState.NoFocusRect) != DrawItemState.NoFocusRect)
            {
                fillColor = ThemeProvider.Theme.Colors.BlueSelection;
            }
 
            using (SolidBrush b = new(fillColor))
            {
                g.FillRectangle(b, rect);
            }
 
            if (e.Index >= 0 && e.Index < Items.Count)
            {
                string text = Items[e.Index].ToString();
 
                using SolidBrush b = new(textColor);
                int padding = 2;
 
                Rectangle modRect = new(rect.Left + padding, rect.Top + padding, rect.Width - (padding * 2), rect.Height - (padding * 2));
 
                StringFormat stringFormat = new()
                {
                    LineAlignment = StringAlignment.Center,
                    Alignment = StringAlignment.Near,
                    FormatFlags = StringFormatFlags.NoWrap,
                    Trimming = StringTrimming.EllipsisCharacter
                };
 
                g.DrawString(text, Font, b, modRect, stringFormat);
            }
        }
    }
 
    #endregion
}