tangxu
2024-12-16 23fadc9cb0c09b665a1bbcef7eaf16f916045dc4
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
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
#region Imports
 
using DPumpHydr.WinFrmUI.RLT.Child.Metro;
using DPumpHydr.WinFrmUI.RLT.Design.Metro;
using DPumpHydr.WinFrmUI.RLT.Enum.Metro;
using DPumpHydr.WinFrmUI.RLT.Extension.Metro;
using DPumpHydr.WinFrmUI.RLT.Interface.Metro;
using DPumpHydr.WinFrmUI.RLT.Manager;
using DPumpHydr.WinFrmUI.RLT.Native;
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Drawing;
using System.Drawing.Text;
using System.Runtime.InteropServices;
using System.Windows.Forms;
 
#endregion
 
namespace DPumpHydr.WinFrmUI.RLT.Controls
{
    #region MetroListBox
 
    [ToolboxItem(true)]
    [ToolboxBitmap(typeof(MetroListBox), "Bitmaps.ListBox.bmp")]
    [Designer(typeof(MetroListBoxDesigner))]
    [DefaultProperty("Items")]
    [DefaultEvent("SelectedIndexChanged")]
    [ComVisible(true)]
    public class MetroListBox : Control, IMetroControl
    {
        #region Interfaces
 
        [Category("Metro"), Description("Gets or sets the style associated with the control.")]
        public Style Style
        {
            get => StyleManager?.Style ?? _style;
            set
            {
                _style = value;
                switch (value)
                {
                    case Style.Light:
                        ApplyTheme();
                        break;
                    case Style.Dark:
                        ApplyTheme(Style.Dark);
                        break;
                    case Style.Custom:
                        ApplyTheme(Style.Custom);
                        break;
                    default:
                        ApplyTheme();
                        break;
                }
                _svs.Style = value;
                Invalidate();
            }
        }
 
        [Category("Metro"), Description("Gets or sets the Style Manager associated with the control.")]
        public MetroStyleManager StyleManager
        {
            get => _styleManager;
            set
            {
                _styleManager = value;
                Invalidate();
            }
        }
 
        [Category("Metro"), Description("Gets or sets the The Author name associated with the theme.")]
        public string ThemeAuthor { get; set; }
 
        [Category("Metro"), Description("Gets or sets the The Theme name associated with the theme.")]
        public string ThemeName { get; set; }
 
        #endregion Interfaces
 
        #region Global Vars
 
        private readonly Utilites _utl;
 
        #endregion Global Vars
 
        #region Internal Vars
 
        private Style _style;
        private MetroStyleManager _styleManager;
        private List<object> _indicates;
        private bool _multiSelect;
        private int _selectedIndex;
        private object _selectedItem;
        private string _selectedText;
        private bool _showScrollBar;
        private bool _multiKeyDown;
        private int _hoveredItem;
        private MetroScrollBar _svs;
        private object _selectedValue;
 
        private bool _isDerivedStyle = true;
        private int _itemHeight;
        private bool _showBorder;
        private Color _selectedItemColor;
        private Color _selectedItemBackColor;
        private Color _hoveredItemColor;
        private Color _hoveredItemBackColor;
        private Color _disabledForeColor;
        private Color _disabledBackColor;
        private Color _borderColor;
 
        #endregion Internal Vars
 
        #region Constructors
 
        public MetroListBox()
        {
            SetStyle
            (
                ControlStyles.UserPaint |
                ControlStyles.AllPaintingInWmPaint |
                ControlStyles.Selectable |
                ControlStyles.ResizeRedraw |
                ControlStyles.OptimizedDoubleBuffer |
                ControlStyles.SupportsTransparentBackColor,
                    true
            );
            UpdateStyles();
            base.BackColor = Color.Transparent;
            base.Font = MetroFonts.Light(10);
            _utl = new Utilites();
            ApplyTheme();
            SetDefaults();
        }
 
        private void SetDefaults()
        {
            SelectedIndex = -1;
            _hoveredItem = -1;
            _showScrollBar = false;
            Items = new MetroItemCollection();
            Items.ItemUpdated += InvalidateScroll;
            SelectedItems = new List<object>();
            _indicates = new List<object>();
            ItemHeight = 30;
            _multiKeyDown = false;
            _svs = new MetroScrollBar()
            {
                Orientation = ScrollOrientate.Vertical,
                Size = new(12, Height),
                Maximum = Items.Count * ItemHeight,
                SmallChange = 1,
                LargeChange = 5
            };
            _svs.Scroll += HandleScroll;
            _svs.MouseDown += VS_MouseDown;
            _svs.BackColor = Color.Transparent;
            if (!Controls.Contains(_svs))
            {
                Controls.Add(_svs);
            }
        }
 
        #endregion Constructors
 
        #region ApplyTheme
 
        private void ApplyTheme(Style style = Style.Light)
        {
            if (!IsDerivedStyle)
            {
                return;
            }
 
            switch (style)
            {
                case Style.Light:
                    ForeColor = Color.Black;
                    BackColor = Color.White;
                    SelectedItemBackColor = Color.FromArgb(65, 177, 225);
                    SelectedItemColor = Color.White;
                    HoveredItemColor = Color.DimGray;
                    HoveredItemBackColor = Color.LightGray;
                    DisabledBackColor = Color.FromArgb(204, 204, 204);
                    DisabledForeColor = Color.FromArgb(136, 136, 136);
                    BorderColor = Color.LightGray;
                    ThemeAuthor = "Taiizor";
                    ThemeName = "MetroLight";
                    UpdateProperties();
                    break;
                case Style.Dark:
                    ForeColor = Color.FromArgb(170, 170, 170);
                    BackColor = Color.FromArgb(30, 30, 30);
                    SelectedItemBackColor = Color.FromArgb(65, 177, 225);
                    SelectedItemColor = Color.White;
                    HoveredItemColor = Color.DimGray;
                    HoveredItemBackColor = Color.LightGray;
                    DisabledBackColor = Color.FromArgb(80, 80, 80);
                    DisabledForeColor = Color.FromArgb(109, 109, 109);
                    BorderColor = Color.FromArgb(64, 64, 64);
                    ThemeAuthor = "Taiizor";
                    ThemeName = "MetroDark";
                    UpdateProperties();
                    break;
                case Style.Custom:
                    if (StyleManager != null)
                    {
                        foreach (KeyValuePair<string, object> varkey in StyleManager.ListBoxDictionary)
                        {
                            switch (varkey.Key)
                            {
                                case "ForeColor":
                                    ForeColor = _utl.HexColor((string)varkey.Value);
                                    break;
                                case "BackColor":
                                    BackColor = _utl.HexColor((string)varkey.Value);
                                    break;
                                case "DisabledBackColor":
                                    DisabledBackColor = _utl.HexColor((string)varkey.Value);
                                    break;
                                case "DisabledForeColor":
                                    DisabledForeColor = _utl.HexColor((string)varkey.Value);
                                    break;
                                case "HoveredItemBackColor":
                                    HoveredItemBackColor = _utl.HexColor((string)varkey.Value);
                                    break;
                                case "HoveredItemColor":
                                    HoveredItemColor = _utl.HexColor((string)varkey.Value);
                                    break;
                                case "SelectedItemBackColor":
                                    SelectedItemBackColor = _utl.HexColor((string)varkey.Value);
                                    break;
                                case "SelectedItemColor":
                                    SelectedItemColor = _utl.HexColor((string)varkey.Value);
                                    break;
                                case "BorderColor":
                                    BorderColor = _utl.HexColor((string)varkey.Value);
                                    break;
                                default:
                                    return;
                            }
                        }
                    }
 
                    UpdateProperties();
                    break;
                default:
                    throw new ArgumentOutOfRangeException(nameof(style), style, null);
            }
        }
 
        private void UpdateProperties()
        {
            Invalidate();
        }
 
        #endregion ApplyTheme
 
        #region Draw Control
 
        protected override void OnPaint(PaintEventArgs e)
        {
            Graphics g = e.Graphics;
            g.TextRenderingHint = TextRenderingHint.ClearTypeGridFit;
            Rectangle mainRect = new(0, 0, Width - (ShowBorder ? 1 : 0), Height - (ShowBorder ? 1 : 0));
 
            using SolidBrush bg = new(Enabled ? BackColor : DisabledBackColor);
            using SolidBrush usic = new(Enabled ? ForeColor : DisabledForeColor);
            using SolidBrush sic = new(SelectedItemColor);
            using SolidBrush sibc = new(SelectedItemBackColor);
            using SolidBrush hic = new(HoveredItemColor);
            using SolidBrush hibc = new(HoveredItemBackColor);
            using StringFormat sf = new() { Alignment = StringAlignment.Near, LineAlignment = StringAlignment.Center };
            int firstItem = _svs.Value / ItemHeight < 0 ? 0 : _svs.Value / ItemHeight;
            int lastItem = (_svs.Value / ItemHeight) + (Height / ItemHeight) + 1 > Items.Count ? Items.Count : (_svs.Value / ItemHeight) + (Height / ItemHeight) + 1;
 
            g.FillRectangle(bg, mainRect);
 
            for (int i = firstItem; i < lastItem; i++)
            {
                string itemText = (string)Items[i];
 
                Rectangle rect = new(5, (i - firstItem) * ItemHeight, Width - 1, ItemHeight);
                g.DrawString(itemText, Font, usic, rect, sf);
                if (MultiSelect && _indicates.Count != 0)
                {
                    if (i == _hoveredItem && !_indicates.Contains(i))
                    {
                        g.FillRectangle(hibc, rect);
                        g.DrawString(itemText, Font, hic, rect, sf);
                    }
                    else if (_indicates.Contains(i))
                    {
                        g.FillRectangle(sibc, rect);
                        g.DrawString(itemText, Font, sic, rect, sf);
                    }
                }
                else
                {
                    if (i == _hoveredItem && i != SelectedIndex)
                    {
                        g.FillRectangle(hibc, rect);
                        g.DrawString(itemText, Font, hic, rect, sf);
                    }
                    else if (i == SelectedIndex)
                    {
                        g.FillRectangle(sibc, rect);
                        g.DrawString(itemText, Font, sic, rect, sf);
                    }
                }
 
            }
            if (ShowBorder)
            {
                g.DrawRectangle(Pens.LightGray, mainRect);
            }
        }
 
        #endregion Draw Control
 
        #region Properties
 
        [TypeConverter(typeof(CollectionConverter))]
        [DesignerSerializationVisibility(DesignerSerializationVisibility.Content)]
        [Editor("System.Windows.Forms.Design.StringCollectionEditor, System.Design", "System.Drawing.Design.UITypeEditor, System.Drawing")]
        [Category("Metro"), Description("Gets the items of the ListBox.")]
        public MetroItemCollection Items { get; private set; }
 
        [Browsable(false)]
        [Category("Metro"), Description("Gets a collection containing the currently selected items in the ListBox.")]
        public List<object> SelectedItems { get; private set; }
 
        [Category("Metro"), Description("Gets or sets the height of an item in the ListBox.")]
        public int ItemHeight
        {
            get => _itemHeight;
            set
            {
                _itemHeight = value;
                Refresh();
            }
        }
 
        [Browsable(false), Category("Metro"), Description("Gets or sets the currently selected item in the ListBox.")]
        public object SelectedItem
        {
            get => _selectedItem;
            set
            {
                _selectedItem = value;
                Invalidate();
            }
        }
 
        [Browsable(false), Category("Metro"),
         Description("Gets or sets the currently selected Text in the ListBox.")]
        public string SelectedText
        {
            get => _selectedText;
            set
            {
                _selectedText = value;
                Invalidate();
            }
        }
 
        [Browsable(false), Category("Metro"), Description("Gets or sets the zero-based index of the currently selected item in a ListBox.")]
        public int SelectedIndex
        {
            get => _selectedIndex;
            set
            {
                _selectedIndex = value;
                Invalidate();
            }
        }
 
        [Browsable(true), Category("Metro"), Description("Gets or sets the value of the member property specified by the ValueMember property.")]
        public object SelectedValue
        {
            get => _selectedValue;
            set
            {
                _selectedValue = value;
                Invalidate();
            }
        }
 
        [Category("Metro"), Description("Gets or sets a value indicating whether the ListBox supports multiple rows.")]
        public bool MultiSelect
        {
            get => _multiSelect;
            set
            {
                _multiSelect = value;
 
                if (SelectedItems.Count > 1)
                {
                    SelectedItems.RemoveRange(1, SelectedItems.Count - 1);
                }
 
                Invalidate();
            }
        }
 
        [Browsable(false)]
        public int Count => Items.Count;
 
        [Category("Metro"), Description("Gets or sets a value indicating whether the vertical scroll bar be shown or not.")]
        public bool ShowScrollBar
        {
            get => _showScrollBar;
            set
            {
                _showScrollBar = value;
                _svs.Visible = value;
                Invalidate();
            }
        }
 
        [Category("Metro"), Description("Gets or sets a value indicating whether the border shown or not.")]
        public bool ShowBorder
        {
            get => _showBorder;
            set
            {
                _showBorder = value;
                Refresh();
            }
        }
 
        [Category("Metro"), Description("Gets or sets backcolor used by the control.")]
        public override Color BackColor { get; set; }
 
        [Category("Metro"), Description("Gets or sets forecolor used by the control.")]
        public override Color ForeColor { get; set; }
 
        [Browsable(false)]
        [EditorBrowsable(EditorBrowsableState.Never)]
        public override string Text { get => base.Text; set => base.Text = value; }
 
        [Category("Metro"), Description("Gets or sets selected item used by the control.")]
        public Color SelectedItemColor
        {
            get => _selectedItemColor;
            set
            {
                _selectedItemColor = value;
                Refresh();
            }
        }
 
        [Category("Metro"), Description("Gets or sets selected item backcolor used by the control.")]
        public Color SelectedItemBackColor
        {
            get => _selectedItemBackColor;
            set
            {
                _selectedItemBackColor = value;
                Refresh();
            }
        }
 
        [Category("Metro"), Description("Gets or sets hovered item used by the control.")]
        public Color HoveredItemColor
        {
            get => _hoveredItemColor;
            set
            {
                _hoveredItemColor = value;
                Refresh();
            }
        }
 
        [Category("Metro"), Description("Gets or sets hovered item backcolor used by the control.")]
        public Color HoveredItemBackColor
        {
            get => _hoveredItemBackColor;
            set
            {
                _hoveredItemBackColor = value;
                Refresh();
            }
        }
 
        [Category("Metro"), Description("Gets or sets disabled forecolor used by the control.")]
        public Color DisabledForeColor
        {
            get => _disabledForeColor;
            set
            {
                _disabledForeColor = value;
                Refresh();
            }
        }
 
        [Category("Metro"), Description("Gets or sets disabled backcolor used by the control.")]
        public Color DisabledBackColor
        {
            get => _disabledBackColor;
            set
            {
                _disabledBackColor = value;
                Refresh();
            }
        }
 
        [Category("Metro"), Description("Gets or sets border color used by the control.")]
        public Color BorderColor
        {
            get => _borderColor;
            set
            {
                _borderColor = value;
                Refresh();
            }
        }
 
        [Category("Metro")]
        [Description("Gets or sets the whether this control reflect to parent(s) style. \n " +
                     "Set it to false if you want the style of this control be independent. ")]
        public bool IsDerivedStyle
        {
            get => _isDerivedStyle;
            set
            {
                _isDerivedStyle = value;
                Refresh();
            }
        }
 
        #endregion Properties
 
        #region Methods
 
        public void AddItem(string newItem)
        {
            Items.Add(newItem);
            InvalidateScroll(this, null);
        }
 
        public void AddItems(string[] newItems)
        {
            foreach (string str in newItems)
            {
                AddItem(str);
            }
 
            InvalidateScroll(this, null);
        }
 
        public void RemoveItemAt(int index)
        {
            Items.RemoveAt(index);
            InvalidateScroll(this, null);
        }
 
        public void RemoveItem(string item)
        {
            Items.Remove(item);
            InvalidateScroll(this, null);
        }
 
        public int IndexOf(string value)
        {
            return Items.IndexOf(value);
        }
 
        public bool Contains(object item)
        {
            return Items.Contains(item.ToString());
        }
 
        public void RemoveItems(string[] itemsToRemove)
        {
            foreach (string item in itemsToRemove)
            {
                Items.Remove(item);
            }
 
            InvalidateScroll(this, null);
        }
 
        public void Clear()
        {
            for (int i = Items.Count - 1; i >= 0; i += -1)
            {
                Items.RemoveAt(i);
            }
 
            InvalidateScroll(this, null);
        }
 
        #endregion Methods
 
        #region Events
 
        public event SelectedIndexChangedEventHandler SelectedIndexChanged;
 
        public delegate void SelectedIndexChangedEventHandler(object sender);
 
        public event SelectedValueEventHandler SelectedValueChanged;
 
        public delegate void SelectedValueEventHandler(object sender);
 
        protected override void OnSizeChanged(EventArgs e)
        {
            InvalidateScroll(this, e);
            InvalidateLayout();
            base.OnSizeChanged(e);
        }
 
        protected override void OnMouseDown(MouseEventArgs e)
        {
            Focus();
            if (e.Button == MouseButtons.Left)
            {
                int index = (_svs.Value / ItemHeight) + (e.Location.Y / ItemHeight);
                if (index >= 0 && index < Items.Count)
                {
                    if (MultiSelect && _multiKeyDown)
                    {
                        _indicates.Add(index);
                        SelectedItems.Add(Items[index]);
                    }
                    else
                    {
                        _indicates.Clear();
                        SelectedItems.Clear();
                        _selectedItem = Items[index];
                        _selectedIndex = index;
                        _selectedValue = Items[index];
                        _selectedText = Items[index].ToString();
                        SelectedIndexChanged?.Invoke(this);
                        SelectedValueChanged?.Invoke(this);
                    }
                }
                Invalidate();
            }
            base.OnMouseDown(e);
        }
 
        private void HandleScroll(object sender)
        {
            Invalidate();
        }
 
        private void InvalidateScroll(object sender, EventArgs e)
        {
            _svs.Maximum = Items.Count * ItemHeight;
            Invalidate();
        }
 
        private void VS_MouseDown(object sender, MouseEventArgs e)
        {
            Focus();
        }
 
        private void InvalidateLayout()
        {
            _svs.Size = new(12, Height - (ShowBorder ? 2 : 0));
            _svs.Location = new(Width - (_svs.Width + (ShowBorder ? 2 : 0)), ShowBorder ? 1 : 0);
            Invalidate();
        }
 
        protected override void OnMouseWheel(MouseEventArgs e)
        {
            _svs.Value -= e.Delta / 4;
            base.OnMouseWheel(e);
        }
 
        protected override bool IsInputKey(Keys keyData)
        {
            switch (keyData)
            {
                case Keys.Down:
                    try
                    {
                        SelectedItems.Remove(Items[SelectedIndex]);
                        SelectedIndex += 1;
                        SelectedItems.Add(Items[SelectedIndex]);
                    }
                    catch
                    {
                        //
                    }
                    break;
 
                case Keys.Up:
                    try
                    {
                        SelectedItems.Remove(Items[SelectedIndex]);
                        SelectedIndex -= 1;
                        SelectedItems.Add(Items[SelectedIndex]);
                    }
                    catch
                    {
                        //
                    }
                    break;
            }
            Invalidate();
            return base.IsInputKey(keyData);
        }
 
        protected override void OnMouseMove(MouseEventArgs e)
        {
            base.OnMouseMove(e);
            Cursor = Cursors.Hand;
            int index = (_svs.Value / ItemHeight) + (e.Location.Y / ItemHeight);
 
            if (index >= Items.Count)
            {
                index = -1;
            }
 
            if (index >= 0 && index < Items.Count)
            {
                _hoveredItem = index;
            }
 
            Invalidate();
        }
 
        protected override void OnMouseLeave(EventArgs e)
        {
            _hoveredItem = -1;
            Cursor = Cursors.Default;
            Invalidate();
            base.OnMouseLeave(e);
        }
 
        protected override void OnHandleCreated(EventArgs e)
        {
            base.OnHandleCreated(e);
            _svs.Location = new(Width - (_svs.Width + (ShowBorder ? 2 : 0)), ShowBorder ? 1 : 0);
            InvalidateScroll(this, e);
        }
 
        protected override void WndProc(ref Message m)
        {
            if (m.Msg == User32.WM_SETCURSOR)
            {
                User32.SetCursor(User32.LoadCursor(IntPtr.Zero, User32.IDC_HAND));
                m.Result = IntPtr.Zero;
                return;
            }
            base.WndProc(ref m);
        }
 
        #endregion Events
 
    }
 
    #endregion
}