FaceListBox.cs
13.5 KB
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
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
namespace Asa.FaceControl
{
[DefaultProperty("Text")]
[DefaultEvent("SelectedIndexChanged")]
public partial class FaceListBox : ControlBase
{
private Item _item;
private int _itemHeight = 30;
private int _selectedIndex = -1;
private HorizontalAlignment _textAlign = HorizontalAlignment.Center;
private FaceScrollBar scrollBar;
private RectangleF[] itemRect;
private RectangleF[] itemTextRect;
private int itemOver = -1;
private float itemOffset = 0;
private float itemStep = 0;
private bool scrollVisible;
private readonly SolidBrush BRUSH_OVER;
private readonly SolidBrush BRUSH_DOWN;
private readonly SolidBrush BRUSH_LINE_1;
private readonly SolidBrush BRUSH_LINE_2;
public FaceListBox()
{
InitializeComponent();
BRUSH_OVER = new SolidBrush(theme.OVER);
BRUSH_DOWN = new SolidBrush(theme.DOWN);
BRUSH_LINE_1 = new SolidBrush(theme.LIST_LINE_1);
BRUSH_LINE_2 = new SolidBrush(theme.LIST_LINE_2);
MouseUp += FaceListBox_MouseUp;
MouseDown += FaceListBox_MouseDown;
MouseMove += FaceListBox_MouseMove;
MouseLeave += FaceListBox_MouseLeave;
MouseWheel += FaceListBox_MouseWheel;
Resize += FaceListBox_Resize;
scrollVisible = false;
scrollBar = new() { Visible = false };
scrollBar.MouseEnter += ScrollBar_MouseEnter;
scrollBar.MouseLeave += ScrollBar_MouseLeave;
scrollBar.Scroll += ScrollBar_Scroll;
Controls.Add(scrollBar);
_item = new Item(ItemRefresh);
}
[Browsable(true), Category("行为"), Description("属性值更改时发生")]
public event EventHandler SelectedIndexChanged;
[Browsable(true), Category("外观"), Description("控件上显示的文本的对齐方式"), DefaultValue(HorizontalAlignment.Center)]
[DesignerSerializationVisibility(DesignerSerializationVisibility.Visible)]
public HorizontalAlignment TextAlign
{
get => _textAlign;
set
{
_textAlign = value;
CalcSize();
Refresh();
}
}
[Browsable(true), Category("外观"), Description(""), DefaultValue(30)]
[DesignerSerializationVisibility(DesignerSerializationVisibility.Visible)]
public int ItemHeight { set => _itemHeight = value; get => _itemHeight; }
[Browsable(false)]
public int SelectedIndex
{
get => _selectedIndex;
set
{
if (_item.list.Count == 0)
_selectedIndex = -1;
else if (value >= _item.list.Count)
_selectedIndex = _item.list.Count - 1;
else if (value < 0)
_selectedIndex = -1;
else
_selectedIndex = value;
if (scrollVisible && _selectedIndex > -1)
{
float val = (itemRect[_selectedIndex].Top - BorderWidth) / itemStep;
scrollBar.Value += Convert.ToInt32(val);
}
else
{
Refresh();
}
SelectedIndexChanged?.Invoke(this, new EventArgs());
}
}
[Browsable(false)]
public new string Text
{
set
{
int idx = _item.list.FindIndex(match => match == value);
SelectedIndex = idx;
}
get
{
if (_selectedIndex == -1) return "";
return _item[_selectedIndex];
}
}
[Browsable(false)]
public Item Items { get => _item; }
public void SelectedFirst()
{
SelectedIndex = 0;
}
public void SelectedLast()
{
SelectedIndex = _item.list.Count - 1;
}
protected override void CalcSize()
{
base.CalcSize();
if (_item == null) return;
int count = _item.list.Count;
itemRect = new RectangleF[count];
itemTextRect = new RectangleF[count];
float y = BorderWidth - itemOffset;
int width = Width - BorderWidth - BorderWidth - (scrollVisible ? scrollBar.Width : 0);
using Graphics g = CreateGraphics();
for (int i = 0; i < count; i++)
{
itemRect[i] = new RectangleF(BorderWidth, y, width, _itemHeight);
y += _itemHeight;
SizeF sf = g.MeasureString(_item.list[i], Font, itemRect[i].Size, stringFormat);
itemTextRect[i] = new RectangleF(0, itemRect[i].Y + (_itemHeight - sf.Height) / 2f, sf.Width, sf.Height);
switch (TextAlign)
{
case HorizontalAlignment.Left:
itemTextRect[i].X = borderRect.X;
break;
case HorizontalAlignment.Center:
itemTextRect[i].X = borderRect.X + (width - sf.Width) / 2f;
break;
case HorizontalAlignment.Right:
itemTextRect[i].X = borderRect.X + width - sf.Width;
break;
}
}
if (scrollBar != null)
{
scrollBar.Width = 30;
scrollBar.Height = Height - BorderWidth - BorderWidth;
scrollBar.Left = Width - BorderWidth - scrollBar.Width;
scrollBar.Top = BorderWidth;
}
}
protected override void DrawEnabled(Graphics g)
{
base.DrawEnabled(g);
Brush brush;
for (int i = 0; i < itemRect.Length; i++)
{
if (itemRect[i].Bottom < 0) continue;
if (itemRect[i].Y > Height) continue;
if (i == _selectedIndex)
brush = BRUSH_DOWN;
else if (i == itemOver)
brush = BRUSH_OVER;
else
brush = i % 2 == 0 ? BRUSH_LINE_1 : BRUSH_LINE_2;
g.FillRectangle(brush, itemRect[i]);
}
//brush = new SolidBrush(ForeColor);
//for (int i = 0; i < itemTextRect.Length; i++)
// g.DrawString(_item.list[i], Font, brush, itemTextRect[i], stringFormat);
for (int i = 0; i < itemTextRect.Length; i++)
{
string text = _item.list[i];
Brush itemBrush = new SolidBrush(ForeColor);
if (text.Contains("<OCR>"))
{
int startIndex = text.IndexOf("<OCR>");
int endIndex = startIndex + "<OCR>".Length;
string prefix = text.Substring(0, startIndex);
string redText = text.Substring(startIndex, endIndex - startIndex);
string suffix = text.Substring(endIndex);
SizeF prefixSize = g.MeasureString(prefix, Font);
SizeF redTextSize = g.MeasureString(redText, Font);
float x = itemTextRect[i].X + prefixSize.Width;
float y = itemTextRect[i].Top;
g.DrawString(prefix, Font, itemBrush, itemTextRect[i].Location, stringFormat);
g.DrawString(redText, Font, Brushes.Red, new PointF(x, y), stringFormat);
g.DrawString(suffix, Font, itemBrush, new PointF(x + redTextSize.Width, y), stringFormat);
}
else
{
g.DrawString(text, Font, itemBrush, itemTextRect[i], stringFormat);
}
}
}
protected override void DrawDisabled(Graphics g)
{
base.DrawDisabled(g);
Brush brush;
for (int i = 0; i < itemRect.Length; i++)
{
if (itemRect[i].Bottom < 0) continue;
if (itemRect[i].Y > Height) continue;
brush = i % 2 == 0 ? BRUSH_LINE_1 : BRUSH_LINE_2;
g.FillRectangle(brush, itemRect[i]);
}
for (int i = 0; i < itemTextRect.Length; i++)
g.DrawString(_item.list[i], Font, BRUSH_DISABLED, itemTextRect[i], stringFormat);
}
private void ItemRefresh()
{
int height = _item.list.Count * _itemHeight + BorderWidth + BorderWidth;
scrollVisible = height > Height;
scrollBar.Visible = scrollVisible;
itemOffset = 0;
if (scrollVisible)
{
float n = height - Height;
scrollBar.Maximum = Convert.ToInt32(Math.Ceiling(n / 10f));
itemStep = n / scrollBar.Maximum;
itemOffset = itemStep * scrollBar.Value;
}
CalcSize();
Refresh();
}
private void FaceListBox_MouseUp(object sender, MouseEventArgs e)
{
SelectedIndexChanged?.Invoke(this, new EventArgs());
}
private void FaceListBox_MouseDown(object sender, MouseEventArgs e)
{
_selectedIndex = itemOver;
}
private void FaceListBox_MouseMove(object sender, MouseEventArgs e)
{
int index = Array.FindIndex(itemRect, match => match.Contains(e.Location));
if (index != itemOver)
{
itemOver = index;
Refresh();
}
}
private void FaceListBox_MouseLeave(object sender, EventArgs e)
{
itemOver = -1;
Refresh();
}
private void FaceListBox_Resize(object sender, EventArgs e)
{
ItemRefresh();
}
private void FaceListBox_MouseWheel(object sender, MouseEventArgs e)
{
if (!Enabled) return;
if (e.Delta > 0)
scrollBar.Value--;
else
scrollBar.Value++;
}
private void ScrollBar_MouseEnter(object sender, EventArgs e)
{
borderEnter = true;
Refresh();
}
private void ScrollBar_MouseLeave(object sender, EventArgs e)
{
borderEnter = false;
Refresh();
}
private void ScrollBar_Scroll(object sender, ScrollEventArgs e)
{
itemOffset = itemStep * scrollBar.Value;
CalcSize();
Refresh();
}
public class Item
{
internal List<string> list = new();
private Action _action;
public Item(Action action)
{
_action = action;
}
public string this[int index]
{
set
{
if (index >= 0 || index < list.Count)
{
list[index] = value;
_action?.Invoke();
}
}
get
{
if (index < 0 || index >= list.Count)
return "";
else
return list[index];
}
}
public int Count
{
get => list.Count;
}
public void Add(string s)
{
if (s == null) return;
list.Add(s);
_action?.Invoke();
}
public void AddWithoutExist(string s)
{
if (s == null) return;
int index = list.FindIndex(match => match == s);
if (index > -1) return;
list.Add(s);
_action?.Invoke();
}
public void AddRange(params string[] s)
{
if (s == null) return;
list.AddRange(s);
_action?.Invoke();
}
public void AddRangeWithoutExist(params string[] s)
{
if (s == null) return;
for (int i = 0; i < s.Length; i++)
{
int index = list.FindIndex(match => match == s[i]);
if (index == -1) list.Add(s[i]);
}
_action?.Invoke();
}
public void Remove(string s)
{
if (s == null) return;
int index = list.FindIndex(match => match == s);
if (index != -1)
{
list.RemoveAt(index);
_action?.Invoke();
}
}
public void Clear()
{
list.Clear();
_action?.Invoke();
}
public void RemoveAt(int index)
{
if (index > -1 && index < list.Count)
{
list.RemoveAt(index);
_action?.Invoke();
}
}
}
public enum ItemChange
{
Add,
AddRange,
RemoveAt,
Clear,
Rename
}
}
}