forked from unknwon/CharmControlLibrary
-
Notifications
You must be signed in to change notification settings - Fork 0
/
CharmCatalogueList.cs
667 lines (605 loc) · 21.2 KB
/
CharmCatalogueList.cs
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
#region 文档说明
/* ******************************************************************************************************
* 文档作者:无闻
* 创建日期:2012年10月28日
* 文档用途:可伸缩二级分类列表自绘目录i列表
* ------------------------------------------------------------------------------------------------------
* 修改记录:
* 2013-02-12(无闻):
* - 改造成二级分类式可伸缩列表框
* - 正式植入 CharmControlLibrary,并更名为 CharmCatalogueList
* 2013-03-18:针对CSBox界面标准2.0进行升级改造
* ------------------------------------------------------------------------------------------------------
* 参考文献:
*
* ******************************************************************************************************/
#endregion
#region 命名空间引用
using System;
using System.Drawing;
using System.Windows.Forms;
using System.Collections;
using System.Collections.Generic;
using System.ComponentModel;
using CharmControlLibrary.Properties;
#endregion
namespace CharmControlLibrary
{
/// <summary>
/// 表示 CharmControlLibrary.CharmCatalogueList 列表框
/// </summary>
public partial class CharmCatalogueList : ListBox
{
#region 字段
// 鼠标现行选中项
private CatalogListBoxItem mMouseItem;
// 列表项集合
private CatalogListBoxItemCollection mItems;
// 隐藏列表项集合
private List<CatalogListBoxItem> mHideItems;
// 按钮高亮项索引(针对某一表项而言)
private int buttonHighlightIndex;
// 分类名称
private string mClassName;
#endregion
#region 属性
/// <summary>
/// 获取或设置当前选中项分类名称
/// </summary>
public string ClassName
{
get { return mClassName; }
set { mClassName = value; }
}
internal ObjectCollection OldItems
{
get { return base.Items; }
}
/// <summary>
/// 获取 CharmCatalogueList 的项
/// </summary>
[Localizable(true)]
[MergableProperty(false)]
[DesignerSerializationVisibility(
DesignerSerializationVisibility.Content)]
public new CatalogListBoxItemCollection Items
{
get { return mItems; }
}
#endregion
#region 重载事件
/// <summary>
/// 引发 Paint 事件
/// </summary>
/// <param name="e">包含事件数据的 PaintEventArgs</param>
protected override void OnPaint(PaintEventArgs e)
{
// 获取绘制对象
Graphics g = e.Graphics;
// 循环绘制每项
for (int i = 0; i < Items.Count; i++)
{
// 获取表项工作区矩形
Rectangle itemRect = GetItemRectangle(i);
// 获取当前绘制的表项
CatalogListBoxItem item = Items[i];
// 绘制表项分割线
g.DrawLine(Pens.LightGray, new Point(0, itemRect.Top), new Point(Width, itemRect.Top));
// 绘制表项背景
g.DrawImage(Resources.Catalog_normal, new Rectangle(new Point(0, itemRect.Y), new Size(189, 33)));
// 第1项需要绘制特殊背景
if (i == 0)
g.DrawImage(Resources.Catalog_First, new Rectangle(new Point(0, itemRect.Y), new Size(189, 33))); ;
// 判断该表项是否为分类
if (item.IsClass)
{
// 绘制展开/收缩图标
if (item.IsExpand)
g.DrawImage(Resources.catalog_class_expand, new Rectangle(new Point(itemRect.Left + 8, itemRect.Y + 11), new Size(12, 12)));
else
g.DrawImage(Resources.catalog_class_collapse, new Rectangle(new Point(itemRect.Left + 8, itemRect.Y + 11), new Size(12, 12))); ;
// 绘制表项标题文本
g.DrawString(item.Text, new Font("微软雅黑", 10, FontStyle.Bold), Brushes.DarkBlue, itemRect.Left + 25, itemRect.Top + 7);
}
else
{
// 判断是否为选中项
if (SelectedIndex == i)
{
// 绘制现行选中项
g.DrawImage(Resources.Catalog_pushed, new Rectangle(new Point(0, itemRect.Y), new Size(189, 33)));
if (item.StatusImages[2] != null)
{
g.DrawImage(item.StatusImages[2], new Rectangle(new Point(25, itemRect.Y + 7), item.StatusImages[0].Size));
g.DrawString(item.Text, new Font("微软雅黑", 9, FontStyle.Bold), Brushes.White, itemRect.Left + 55, itemRect.Top + 7);
}
else
g.DrawString(item.Text, new Font("微软雅黑", 9, FontStyle.Bold), Brushes.White, itemRect.Left + 50, itemRect.Top + 7);
}
else
{
// 非现行选中项
// 判断是否为高亮项,是则绘制高亮背景
if (mMouseItem == Items[i])
{
g.DrawImage(Resources.Catalog_hover, new Rectangle(new Point(0, itemRect.Y), new Size(189, 33)));
if (item.StatusImages[1] != null)
{
g.DrawImage(item.StatusImages[1], new Rectangle(new Point(25, itemRect.Y + 7), item.StatusImages[0].Size));
g.DrawString(item.Text, new Font("微软雅黑", 9), Brushes.Black, itemRect.Left + 55, itemRect.Top + 7);
}
else
g.DrawString(item.Text, new Font("微软雅黑", 9), Brushes.Black, itemRect.Left + 50, itemRect.Top + 7);
}
else if (item.StatusImages[0] != null)
{
g.DrawImage(item.StatusImages[0], new Rectangle(new Point(25, itemRect.Y + 7), item.StatusImages[0].Size));
g.DrawString(item.Text, new Font("微软雅黑", 9), Brushes.Black, itemRect.Left + 55, itemRect.Top + 7);
}
else
g.DrawString(item.Text, new Font("微软雅黑", 9), Brushes.Black, itemRect.Left + 50, itemRect.Top + 7);
}
}
}
//base.OnPaint(e);
}
/// <summary>
/// 测量表项事件
/// </summary>
/// <param name="e"></param>
protected override void OnMeasureItem(MeasureItemEventArgs e)
{
//base.OnMeasureItem(e);
try
{
e.ItemHeight = 33;
}
catch { }
}
/// <summary>
/// 引发 MouseMove 事件
/// </summary>
/// <param name="e">包含事件数据的 MouseEventArgs</param>
protected override void OnMouseMove(MouseEventArgs e)
{
bool isChange = false; // 是否改变标识符
base.OnMouseMove(e);
for (int i = 0; i < Items.Count; i++)
{
Rectangle bounds = GetItemRectangle(i);
if (bounds.Contains(e.X, e.Y))
{
if (e.X > 445 && e.X < 545 && e.Y > (27 + bounds.Y) && e.Y < (57 + bounds.Y))
{
if (buttonHighlightIndex != i)
{
buttonHighlightIndex = i;
isChange = true;
}
}
else if (e.X > 330 && e.X < 410 && e.Y > (45 + bounds.Y) && e.Y < (65 + bounds.Y))
{
if (Cursor != Cursors.Hand)
Cursor = Cursors.Hand;
}
else
{
if (Cursor != Cursors.Arrow)
Cursor = Cursors.Arrow;
if (buttonHighlightIndex == i)
{
buttonHighlightIndex = -1;
isChange = true;
}
}
if (Items[i] != mMouseItem)
{
mMouseItem = Items[i];
isChange = true;
}
}
if (isChange)
break;
}
if (isChange)
{
Invalidate();
}
}
/// <summary>
/// 选中项被改变事件
/// </summary>
/// <param name="e"></param>
protected override void OnSelectedIndexChanged(EventArgs e)
{
// 判断不为主分类标签才执行事件
if (!Items[SelectedIndex].IsClass)
{
mMouseItem = Items[SelectedIndex];
mClassName = mMouseItem.Text;
base.OnSelectedIndexChanged(e);
}
else
{
// 分类表项被单击
// 获取分类展开标识、分类ID
bool isExpand = Items[SelectedIndex].IsExpand;
int classID = Items[SelectedIndex].ClassID;
// 判断当前展开状态
if (isExpand)
{
// 当前为展开状态,需要闭合
for (int i = Items.Count - 1; i >= 0; i--)
{
if (Items[i].ClassID == classID && !Items[i].IsClass)
{
mHideItems.Insert(0, Items[i]);
Items.RemoveAt(i);
}
}
// 设置展开状态为假
Items[SelectedIndex].IsExpand = false;
}
else
{
// 当前为闭合状态,需要展开
for (int i = mHideItems.Count - 1; i >= 0; i--)
{
if ((mHideItems[i]).ClassID == classID)
{
Items.Insert(SelectedIndex + 1,mHideItems[i]);
mHideItems.RemoveAt(i);
}
}
// 设置展开状态为真
Items[SelectedIndex].IsExpand = true;
}
}
// 重绘列表
Invalidate();
}
#endregion
#region 构造方法
/// <summary>
/// 初始化 CharmCatalogueList 类的新实例
/// </summary>
public CharmCatalogueList()
: base()
{
mItems = new CatalogListBoxItemCollection(this);
mHideItems = new List<CatalogListBoxItem>();
base.DrawMode = DrawMode.OwnerDrawVariable;
SetStyle(ControlStyles.UserPaint, true);
SetStyle(ControlStyles.DoubleBuffer, true);// 双缓冲
SetStyle(ControlStyles.ResizeRedraw, true);//调整大小时重绘
SetStyle(ControlStyles.AllPaintingInWmPaint, true); // 禁止擦除背景.
SetStyle(ControlStyles.OptimizedDoubleBuffer, true);// 双缓冲
SetStyle(ControlStyles.SupportsTransparentBackColor, true);
// 初始化属性
buttonHighlightIndex = -1;
}
#endregion
}
/// <summary>
/// 表示 CharmControlLibrary.CatalogListBoxItem 列表项
/// </summary>
[Serializable]
[TypeConverter(typeof(ExpandableObjectConverter))]
public class CatalogListBoxItem : IDisposable
{
#region 字段
// 表项标题文本
private string mText;
// 指示表项是否为分类
private bool mIsClass;
// 所属分类ID(非分类表项专用)
private int mClassID;
// 指示分类下级是否展开(分类表项专用)
private bool mIsExpand;
// 表项状态图标(非分类表项专用)
private Image[] mStatusImages;
#endregion
#region 属性
/// <summary>
/// 获取或设置表项标题文本
/// </summary>
public string Text
{
get { return mText; }
set { mText = value; }
}
/// <summary>
/// 获取或设置一个值,该值指示该项是否为分类
/// </summary>
public bool IsClass
{
get { return mIsClass; }
set { mIsClass = value; }
}
/// <summary>
/// 获取或设置所属分类ID(非分类表项专用)
/// </summary>
public int ClassID
{
get { return mClassID; }
set { mClassID = value; }
}
/// <summary>
/// 获取或设置一个值,该值指示该项下级表项是否展开(分类表项专用)
/// </summary>
public bool IsExpand
{
get { return mIsExpand; }
set { mIsExpand = value; }
}
/// <summary>
/// 获取或设置表项状态图标(非分类表项专用)
/// </summary>
public Image[] StatusImages
{
get { return mStatusImages; }
set { mStatusImages = value; }
}
#endregion
#region 构造方法
/// <summary>
/// 初始化 CatalogListBoxItem 类的新实例
/// </summary>
public CatalogListBoxItem()
{
}
/// <summary>
/// 初始化 CatalogListBoxItem 类的新实例
/// </summary>
/// <param name="text">表项标题文本</param>
/// <param name="classID">所属分类ID</param>
public CatalogListBoxItem(
string text,
int classID)
{
mText = text;
mIsClass = false;
mClassID = classID;
IsExpand = false;
mStatusImages = new Image[3];
}
/// <summary>
/// 表示用于 CatalogueList 的表项
/// </summary>
/// <param name="text">表项标题文本</param>
/// <param name="classID">所属分类ID</param>
/// <param name="isClass">是否为分类</param>
public CatalogListBoxItem(
string text,
int classID,
bool isClass)
{
mText = text;
mIsClass = isClass;
mClassID = classID;
IsExpand = false;
mStatusImages = new Image[3];
}
#endregion
#region 方法
/// <summary>
/// 释放由 Component 使用的所有资源
/// </summary>
public void Dispose()
{
mStatusImages = null;
}
#endregion
}
/// <summary>
/// 表示 CharmControlLibrary.CatalogListBoxItemCollection 列表项集合
/// </summary>
[ListBindable(false)]
public class CatalogListBoxItemCollection : IList, ICollection, IEnumerable
{
#region 字段
private CharmCatalogueList _owner;
#endregion
#region 属性
internal CharmCatalogueList Owner
{
get { return _owner; }
}
/// <summary>
/// 获取或设置集合中指定索引处的项
/// </summary>
/// <param name="index">集合中要获取或设置项的索引</param>
/// <returns></returns>
public CatalogListBoxItem this[int index]
{
get { return Owner.OldItems[index] as CatalogListBoxItem; }
set { Owner.OldItems[index] = value; }
}
/// <summary>
/// 获取集合中项的数目
/// </summary>
public int Count
{
get { return Owner.OldItems.Count; }
}
/// <summary>
/// 获取一个值,该值指示集合是否为只读
/// </summary>
public bool IsReadOnly
{
get { return Owner.OldItems.IsReadOnly; }
}
#endregion
#region 方法
/// <summary>
/// 表示 CharmCatalogueList 的列表项集合
/// </summary>
/// <param name="owner"></param>
public CatalogListBoxItemCollection(CharmCatalogueList owner)
{
_owner = owner;
}
/// <summary>
/// 向 CharmCatalogueList 的项列表添加项
/// </summary>
/// <param name="item"></param>
/// <returns></returns>
public int Add(CatalogListBoxItem item)
{
if (item == null)
{
throw new ArgumentException("item");
}
return Owner.OldItems.Add(item);
}
/// <summary>
/// 确定指定的项是否位于集合内
/// </summary>
/// <param name="item"></param>
/// <returns></returns>
public bool Contains(CatalogListBoxItem item)
{
return Owner.OldItems.Contains(item);
}
/// <summary>
/// 从集合中移除所有项
/// </summary>
public void Clear()
{
Owner.OldItems.Clear();
}
/// <summary>
/// 返回指定项在集合中的索引
/// </summary>
/// <param name="item"></param>
/// <returns></returns>
public int IndexOf(CatalogListBoxItem item)
{
return Owner.OldItems.IndexOf(item);
}
/// <summary>
/// 将整个集合复制到现有的数组集合中,从该数组的指定位置开始复制
/// </summary>
/// <param name="destination"></param>
/// <param name="arrayIndex"></param>
public void CopyTo(CatalogListBoxItem[] destination, int arrayIndex)
{
Owner.OldItems.CopyTo(destination, arrayIndex);
}
/// <summary>
/// 将项插入 CharmCatalogueList 的指定索引处
/// </summary>
/// <param name="index"></param>
/// <param name="item"></param>
public void Insert(int index, CatalogListBoxItem item)
{
if (item == null)
{
throw new ArgumentNullException("item");
}
Owner.OldItems.Insert(index, item);
}
/// <summary>
/// 从集合中移除指定的对象
/// </summary>
/// <param name="item"></param>
public void Remove(CatalogListBoxItem item)
{
Owner.OldItems.Remove(item);
}
/// <summary>
/// 移除集合中指定索引处的项
/// </summary>
/// <param name="index"></param>
public void RemoveAt(int index)
{
Owner.OldItems.RemoveAt(index);
}
#endregion
#region 成员接口
/// <summary>
/// 返回一个枚举数,将使用该枚举数循环访问集合
/// </summary>
/// <returns></returns>
public IEnumerator GetEnumerator()
{
return Owner.OldItems.GetEnumerator();
}
int IList.Add(object value)
{
if (!(value is CatalogListBoxItem))
{
throw new ArgumentException();
}
return Add(value as CatalogListBoxItem);
}
bool IList.Contains(object value)
{
return Contains(value as CatalogListBoxItem);
}
void IList.Clear()
{
Clear();
}
bool ICollection.IsSynchronized
{
get { return false; }
}
object ICollection.SyncRoot
{
get { return this; }
}
int IList.IndexOf(object value)
{
return IndexOf(value as CatalogListBoxItem);
}
void ICollection.CopyTo(Array array, int index)
{
CopyTo((CatalogListBoxItem[])array, index);
}
int ICollection.Count
{
get { return Count; }
}
bool IList.IsFixedSize
{
get { return false; }
}
bool IList.IsReadOnly
{
get { return IsReadOnly; }
}
void IList.Remove(object value)
{
Remove(value as CatalogListBoxItem);
}
void IList.RemoveAt(int index)
{
RemoveAt(index);
}
object IList.this[int index]
{
get
{
return this[index];
}
set
{
if (!(value is CatalogListBoxItem))
{
throw new ArgumentException();
}
this[index] = value as CatalogListBoxItem;
}
}
void IList.Insert(int index, object value)
{
if (!(value is CatalogListBoxItem))
{
throw new ArgumentException();
}
Insert(index, value as CatalogListBoxItem);
}
#endregion
}
}