tangxu
2024-11-04 ebd031e3bed6c1cfddce8fc9b98f7f9e95fb9e32
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
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
#region Imports
 
using DPumpHydr.WinFrmUI.RLT.Colors;
using DPumpHydr.WinFrmUI.RLT.Util;
using System;
using System.Drawing;
using System.Windows.Forms;
 
#endregion
 
namespace DPumpHydr.WinFrmUI.RLT.Controls
{
    #region RoyalListBox
 
    public class RoyalListBox : Control
    {
        private readonly RoyalScrollBar scrollBar;
 
        public RoyalListBoxItemCollection Items { get; }
 
        public RoyalListBoxSelectedItemCollection SelectedItems { get; }
 
        public RoyalListBoxSelectedIndexCollection SelectedIndicies { get; }
 
        private bool multiSelection;
        public bool MultiSelection
        {
            get => multiSelection;
            set { multiSelection = value; Invalidate(); }
        }
 
        private readonly bool multiSelectKeyDown = false;
        private int itemHeight;
        public int ItemHeight
        {
            get => itemHeight;
            set { itemHeight = value; Invalidate(); }
        }
 
        private Color hotLightColor;
        public Color HotLightColor
        {
            get => hotLightColor;
            set { hotLightColor = value; Invalidate(); }
        }
 
        private Color selectedColor;
        public Color SelectedColor
        {
            get => selectedColor;
            set { selectedColor = value; Invalidate(); }
        }
 
        private int selectedIndex;
        public int SelectedIndex
        {
            get => selectedIndex;
            set { selectedIndex = value; Invalidate(); }
        }
 
        public object SelectedValue => null;
 
        private int hotLightedIndex;
        public int HotLightedIndex
        {
            get => hotLightedIndex;
            set { hotLightedIndex = value; Invalidate(); }
        }
 
        public object HotLightedItem => null;
 
        public RoyalListBox()
        {
            SetStyle(ControlStyles.AllPaintingInWmPaint, true);
            SetStyle(ControlStyles.OptimizedDoubleBuffer, true);
 
            SelectedIndicies = new RoyalListBoxSelectedIndexCollection();
            SelectedItems = new RoyalListBoxSelectedItemCollection();
 
            Items = new RoyalListBoxItemCollection();
            Items.ItemAdded += new EventHandler(Items_ItemAdded);
            Items.ItemRemoved += new EventHandler(Items_ItemRemoved);
 
            ItemHeight = 30;
            HotLightColor = RoyalColors.HotTrackColor;
            SelectedColor = RoyalColors.AccentColor;
            SelectedIndex = -1;
            HotLightedIndex = -1;
 
            scrollBar = new RoyalScrollBar
            {
                GutterColor = RoyalColors.HotTrackColor,
                ThumbColor = RoyalColors.AccentColor,
                Location = new(Width - 5, 0),
                Size = new(5, Height),
                Orientation = Orientation.Vertical
            };
            scrollBar.ValueChanged += new EventHandler(ScrollBar_ValueChanged);
            scrollBar.SmallChange = ItemHeight;
            scrollBar.LargeChange = ItemHeight * 3;
            scrollBar.Show();
 
            Controls.Add(scrollBar);
            Controls.SetChildIndex(scrollBar, 0);
        }
 
        public int IndexFromPoint(Point p)
        {
            int index = (scrollBar.Value / ItemHeight) + (p.Y / ItemHeight);
 
            if (index >= Items.Count)
            {
                index = -1;
            }
 
            return index;
        }
 
        protected override void OnMouseMove(MouseEventArgs e)
        {
            int index = IndexFromPoint(e.Location);
 
            if (index >= 0 && index < Items.Count)
            {
                HotLightedIndex = index;
                Refresh();
            }
 
            base.OnMouseMove(e);
        }
 
        protected override void OnMouseEnter(EventArgs e)
        {
            base.OnMouseEnter(e);
        }
 
        protected override void OnMouseLeave(EventArgs e)
        {
            HotLightedIndex = -1;
            Refresh();
 
            base.OnMouseLeave(e);
        }
 
        protected override void OnMouseClick(MouseEventArgs e)
        {
            int index = IndexFromPoint(e.Location);
 
            if (index >= 0 && index < Items.Count)
            {
                if (multiSelection && multiSelectKeyDown)
                {
                    SelectedIndicies.Add(index);
                    SelectedItems.Add(Items[index]);
                    Refresh();
                }
                else
                {
                    SelectedIndicies.Clear();
                    SelectedItems.Clear();
 
                    SelectedIndex = index;
                    Refresh();
                }
            }
 
            base.OnMouseClick(e);
        }
 
        protected override void OnMouseWheel(MouseEventArgs e)
        {
            scrollBar.Value -= e.Delta / 4;
            base.OnMouseWheel(e);
        }
 
        protected override void OnResize(EventArgs e)
        {
            scrollBar.Location = new(Width - 8, 0);
            scrollBar.Size = new(8, Height);
 
            base.OnResize(e);
        }
 
        protected void DrawItem(DrawItemEventArgs e)
        {
            Color foreColor = RoyalColors.ForeColor;
            Color backColor = RoyalColors.BackColor;
            string text = Items[e.Index].ToString();
 
            if (e.State == DrawItemState.HotLight)
            {
                backColor = RoyalColors.HotTrackColor;
            }
            else if (e.State == DrawItemState.Selected)
            {
                foreColor = RoyalColors.PressedForeColor;
                backColor = SelectedColor;
            }
 
            e.Graphics.FillRectangle(new SolidBrush(backColor), e.Bounds);
            TextRenderer.DrawText(e.Graphics, text, Font, e.Bounds, foreColor, Color.Transparent,
                TextFormatFlags.Left | TextFormatFlags.VerticalCenter | TextFormatFlags.LeftAndRightPadding);
        }
 
        protected override void OnPaint(PaintEventArgs e)
        {
            int firstVisibleItem = scrollBar.Value / ItemHeight;
            int lastVisibleItem = (scrollBar.Value / ItemHeight) + (Height / ItemHeight) + 1;
 
            if (firstVisibleItem < 0)
            {
                firstVisibleItem = 0;
            }
 
            if (lastVisibleItem > Items.Count)
            {
                lastVisibleItem = Items.Count;
            }
 
            for (int i = firstVisibleItem; i < lastVisibleItem; i++)
            {
                DrawItemState state = DrawItemState.Default;
 
                if (multiSelection && SelectedIndicies.Count > 0)
                {
                    if (i == HotLightedIndex && !SelectedIndicies.Contains(i))
                    {
                        state = DrawItemState.HotLight;
                    }
                    else if (SelectedIndicies.Contains(i))
                    {
                        state = DrawItemState.Selected;
                    }
                }
                else
                {
                    if (i == HotLightedIndex && i != SelectedIndex)
                    {
                        state = DrawItemState.HotLight;
                    }
                    else if (i == SelectedIndex)
                    {
                        state = DrawItemState.Selected;
                    }
                }
 
                Rectangle rect = new(0, (i - firstVisibleItem) * ItemHeight, Width, ItemHeight);
                DrawItemEventArgs de = new(e.Graphics, Font, rect, i, state);
 
                DrawItem(de);
            }
            base.OnPaint(e);
        }
 
        private void Items_ItemAdded(object sender, EventArgs e)
        {
            scrollBar.Max = Items.Count * ItemHeight;
            scrollBar.Refresh();
        }
 
        private void Items_ItemRemoved(object sender, EventArgs e)
        {
            scrollBar.Max = Items.Count * ItemHeight;
            scrollBar.Refresh();
        }
 
        private void ScrollBar_ValueChanged(object sender, EventArgs e)
        {
            Refresh();
        }
    };
 
    #endregion
}