yangyin
2025-02-24 b3b65a002fe68b1383314098790f5a153b87765f
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
#region Imports
 
using DPumpHydr.WinFrmUI.RLT.Colors;
using DPumpHydr.WinFrmUI.RLT.Util;
using System.ComponentModel;
using System.Drawing;
using System.Drawing.Drawing2D;
using System.Drawing.Text;
using System.Windows.Forms;
 
#endregion
 
namespace DPumpHydr.WinFrmUI.RLT.Controls
{
    #region ForeverListBox
 
    public class ForeverListBox : Control
    {
        private ListBox withEventsField_ListBx = new();
        private ListBox ListBx
        {
            get => withEventsField_ListBx;
            set
            {
                if (withEventsField_ListBx != null)
                {
                    withEventsField_ListBx.DrawItem -= Drawitem;
                }
 
                withEventsField_ListBx = value;
                if (withEventsField_ListBx != null)
                {
                    withEventsField_ListBx.DrawItem += Drawitem;
                }
            }
        }
 
        private string[] _items = { "" };
 
        [Category("Options")]
        public string[] items
        {
            get => _items;
            set
            {
                _items = value;
                ListBx.Items.Clear();
                ListBx.Items.AddRange(value);
                Invalidate();
            }
        }
 
        [Category("Colors")]
        public Color SelectedColor { get; set; } = ForeverLibrary.ForeverColor;
 
        public string SelectedItem =>
                //return ListBx.SelectedItem.ToString();
                (string)ListBx.Items[SelectedIndex];
 
        public int SelectedIndex
        {
            get
            {
                int functionReturnValue = 0;
                //return ListBx.SelectedIndex;
                if (ListBx.SelectedIndex < 0)
                {
                    return functionReturnValue;
                }
 
                return functionReturnValue;
            }
        }
 
        public void Clear()
        {
            ListBx.Items.Clear();
        }
 
        public void ClearSelected()
        {
            for (int i = ListBx.SelectedItems.Count - 1; i >= 0; i += -1)
            {
                ListBx.Items.Remove(ListBx.SelectedItems[i]);
            }
        }
 
        public void Drawitem(object sender, DrawItemEventArgs e)
        {
            if (e.Index < 0)
            {
                return;
            }
 
            e.DrawBackground();
            e.DrawFocusRectangle();
 
            e.Graphics.SmoothingMode = SmoothingMode.HighQuality;
            e.Graphics.PixelOffsetMode = PixelOffsetMode.HighQuality;
            e.Graphics.InterpolationMode = InterpolationMode.HighQualityBicubic;
            e.Graphics.TextRenderingHint = TextRenderingHint.ClearTypeGridFit;
 
            //-- if selected
            if (e.State.ToString().IndexOf("Selected,") >= 0)
            {
                //-- Base
                e.Graphics.FillRectangle(new SolidBrush(SelectedColor), new Rectangle(e.Bounds.X, e.Bounds.Y, e.Bounds.Width, e.Bounds.Height));
 
                //-- Text
                e.Graphics.DrawString(" " + ListBx.Items[e.Index].ToString(), new Font("Segoe UI", 8), Brushes.White, e.Bounds.X, e.Bounds.Y + 2);
            }
            else
            {
                //-- Base
                e.Graphics.FillRectangle(new SolidBrush(Color.FromArgb(51, 53, 55)), new Rectangle(e.Bounds.X, e.Bounds.Y, e.Bounds.Width, e.Bounds.Height));
 
                //-- Text 
                e.Graphics.DrawString(" " + ListBx.Items[e.Index].ToString(), new Font("Segoe UI", 8), Brushes.White, e.Bounds.X, e.Bounds.Y + 2);
            }
 
            e.Graphics.Dispose();
        }
 
        protected override void OnCreateControl()
        {
            base.OnCreateControl();
            if (!Controls.Contains(ListBx))
            {
                Controls.Add(ListBx);
            }
        }
 
        public void AddRange(object[] items)
        {
            ListBx.Items.Remove("");
            ListBx.Items.AddRange(items);
        }
 
        public void AddItem(object item)
        {
            ListBx.Items.Remove("");
            ListBx.Items.Add(item);
        }
 
        private readonly Color BaseColor = Color.FromArgb(45, 47, 49);
 
        public ForeverListBox()
        {
            SetStyle(ControlStyles.AllPaintingInWmPaint | ControlStyles.UserPaint | ControlStyles.ResizeRedraw | ControlStyles.OptimizedDoubleBuffer, true);
            DoubleBuffered = true;
 
            ListBx.DrawMode = System.Windows.Forms.DrawMode.OwnerDrawFixed;
            ListBx.ScrollAlwaysVisible = false;
            ListBx.HorizontalScrollbar = false;
            ListBx.BorderStyle = BorderStyle.None;
            ListBx.BackColor = BaseColor;
            ListBx.ForeColor = Color.White;
            ListBx.Location = new(3, 3);
            ListBx.Font = new("Segoe UI", 8);
            ListBx.ItemHeight = 20;
            ListBx.Items.Clear();
            ListBx.IntegralHeight = false;
 
            Size = new(131, 101);
            BackColor = BaseColor;
        }
 
        protected override void OnPaint(PaintEventArgs e)
        {
            //UpdateColors();
 
            Bitmap B = new(Width, Height);
            Graphics G = Graphics.FromImage(B);
 
            Rectangle Base = new(0, 0, Width, Height);
 
            Graphics _with19 = G;
            _with19.SmoothingMode = SmoothingMode.HighQuality;
            _with19.PixelOffsetMode = PixelOffsetMode.HighQuality;
            _with19.TextRenderingHint = TextRenderingHint.ClearTypeGridFit;
            _with19.Clear(BackColor);
 
            //-- Size
            ListBx.Size = new(Width - 6, Height - 2);
 
            //-- Base
            _with19.FillRectangle(new SolidBrush(BaseColor), Base);
 
            base.OnPaint(e);
            G.Dispose();
            e.Graphics.InterpolationMode = InterpolationMode.HighQualityBicubic;
            e.Graphics.DrawImageUnscaled(B, 0, 0);
            B.Dispose();
        }
 
        private void UpdateColors()
        {
            ForeverColors Colors = ForeverLibrary.GetColors(this);
 
            SelectedColor = Colors.Forever;
        }
    }
 
    #endregion
}