-
Notifications
You must be signed in to change notification settings - Fork 241
Expand file tree
/
Copy pathSTNodeTreeView.cs
More file actions
920 lines (867 loc) · 43.4 KB
/
STNodeTreeView.cs
File metadata and controls
920 lines (867 loc) · 43.4 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
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
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Drawing;
using System.Reflection;
using System.Windows.Forms;
using System.ComponentModel;
using System.Collections;
using SkiaSharp;
using SkiaSharp.Views.Desktop;
/*
MIT License
Copyright (c) 2021 DebugST@crystal_lz
Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in all
copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
SOFTWARE.
*/
/*
* create: 2021-02-23
* modify: 2021-03-02
* Author: Crystal_lz
* blog: http://st233.com
* Gitee: https://gitee.com/DebugST
* Github: https://github.com/DebugST
*/
namespace ST.Library.UI.NodeEditor
{
public class STNodeTreeView : SKControl
{
private Color _ItemBackColor = Color.FromArgb(255, 45, 45, 45);
/// <summary>
/// 获取或设置每行属性选项背景色
/// </summary>
[Description("获取或设置每行属性选项背景色")]
public Color ItemBackColor {
get { return _ItemBackColor; }
set {
_ItemBackColor = value;
}
}
private Color _ItemHoverColor = Color.FromArgb(50, 125, 125, 125);
/// <summary>
/// 获取或设置属性选项被鼠标悬停时候背景色
/// </summary>
[Description("获取或设置属性选项被鼠标悬停时候背景色")]
public Color ItemHoverColor {
get { return _ItemHoverColor; }
set { _ItemHoverColor = value; }
}
private Color _TitleColor = Color.FromArgb(255, 60, 60, 60);
/// <summary>
/// 获取或设置顶部检索区域背景色
/// </summary>
[Description("获取或设置顶部检索区域背景颜色")]
public Color TitleColor {
get { return _TitleColor; }
set {
_TitleColor = value;
this.Invalidate(new Rectangle(0, 0, this.Width, m_nItemHeight));
}
}
/// <summary>
/// 获取或设置检索文本框的背景色
/// </summary>
[Description("获取或设置检索文本框的背景色")]
public Color TextBoxColor {
get { return m_tbx.BackColor; }
set {
m_tbx.BackColor = value;
this.Invalidate(new Rectangle(0, 0, this.Width, m_nItemHeight));
}
}
private Color _HightLightTextColor = Color.Lime;
/// <summary>
/// 获取或设置检索时候高亮文本颜色
/// </summary>
[Description("获取或设置检索时候高亮文本颜色"), DefaultValue(typeof(Color), "Lime")]
public Color HightLightTextColor {
get { return _HightLightTextColor; }
set { _HightLightTextColor = value; }
}
private Color _InfoButtonColor = Color.Gray;
/// <summary>
/// 获取或设置信息显示按钮颜色 若设置AutoColor无法设置此属性值
/// </summary>
[Description("获取或设置信息显示按钮颜色 若设置AutoColor无法设置此属性值"), DefaultValue(typeof(Color), "Gray")]
public Color InfoButtonColor {
get { return _InfoButtonColor; }
set { _InfoButtonColor = value; }
}
private Color _FolderCountColor = Color.FromArgb(40, 255, 255, 255);
/// <summary>
/// 获取或设置统计个数的文本颜色
/// </summary>
[Description("获取或设置统计个数的文本颜色")]
public Color FolderCountColor {
get { return _FolderCountColor; }
set { _FolderCountColor = value; }
}
private Color _SwitchColor = Color.LightGray;
private bool _ShowFolderCount = true;
/// <summary>
/// 获取或设置是否统计STNode的个数
/// </summary>
[Description("获取或设置是否统计STNode的个数"), DefaultValue(typeof(Color), "LightGray")]
public bool ShowFolderCount {
get { return _ShowFolderCount; }
set { _ShowFolderCount = value; }
}
private bool _ShowInfoButton = true;
/// <summary>
/// 获取或设置是否显示信息按钮
/// </summary>
[Description("获取或设置是否显示信息按钮"), DefaultValue(true)]
public bool ShowInfoButton {
get { return _ShowInfoButton; }
set { _ShowInfoButton = value; }
}
private bool _InfoPanelIsLeftLayout = true;
/// <summary>
/// 获取或设置预览窗口是否是向左布局
/// </summary>
[Description("获取或设置预览窗口是否是向左布局"), DefaultValue(true)]
public bool InfoPanelIsLeftLayout {
get { return _InfoPanelIsLeftLayout; }
set { _InfoPanelIsLeftLayout = value; }
}
private bool _AutoColor = true;
/// <summary>
/// 获取或设置控件中部分颜色来之对应的STNode的标题颜色
/// </summary>
[Description("获取或设置控件中部分颜色来之对应的STNode的标题颜色"), DefaultValue(true)]
public bool AutoColor {
get { return _AutoColor; }
set {
_AutoColor = value;
this.Invalidate();
}
}
private STNodeEditor _Editor;
/// <summary>
/// 获取节点预览时候使用的STNodeEditor
/// </summary>
[Description("获取节点预览时候使用的STNodeEditor"), Browsable(false)]
public STNodeEditor Editor {
get { return _Editor; }
}
private STNodePropertyGrid _PropertyGrid;
/// <summary>
/// 获取节点预览时候使用的STNodePropertyGrid
/// </summary>
[Description("获取节点预览时候使用的STNodePropertyGrid"), Browsable(false)]
public STNodePropertyGrid PropertyGrid {
get { return _PropertyGrid; }
}
private int m_nItemHeight = 29;
private static Type m_type_node_base = typeof(STNode);
private static char[] m_chr_splitter = new char[] { '/', '\\' };
private STNodeTreeCollection m_items_draw;
private STNodeTreeCollection m_items_source = new STNodeTreeCollection("ROOT");
private Dictionary<Type, string> m_dic_all_type = new Dictionary<Type, string>();
private Pen m_pen;
private SolidBrush m_brush;
private StringFormat m_sf;
private DrawingTools m_dt;
private SKCanvas m_canvas;
private Color m_clr_item_1 = Color.FromArgb(10, 0, 0, 0);// Color.FromArgb(255, 40, 40, 40);
private Color m_clr_item_2 = Color.FromArgb(10, 255, 255, 255);// Color.FromArgb(255, 50, 50, 50);
private int m_nOffsetY; //控件绘制时候需要偏移的垂直高度
private int m_nSourceOffsetY; //绘制源数据时候需要偏移的垂直高度
private int m_nSearchOffsetY; //绘制检索数据时候需要偏移的垂直高度
private int m_nVHeight; //控件中内容需要的总高度
private bool m_bHoverInfo; //当前鼠标是否悬停在信息显示按钮上
private STNodeTreeCollection m_item_hover; //当前鼠标悬停的树节点
private Point m_pt_control; //鼠标在控件上的坐标
private Point m_pt_offsety; //鼠标在控件上锤子偏移后的坐标
private Rectangle m_rect_clear; //清空检索按钮区域
private string m_str_search; //检索的文本
private TextBox m_tbx = new TextBox(); //检索文本框
/// <summary>
/// 构造一个STNode树控件
/// </summary>
public STNodeTreeView() {
this.SetStyle(ControlStyles.UserPaint, true);
this.SetStyle(ControlStyles.ResizeRedraw, true);
this.SetStyle(ControlStyles.AllPaintingInWmPaint, true);
this.SetStyle(ControlStyles.OptimizedDoubleBuffer, true);
this.SetStyle(ControlStyles.SupportsTransparentBackColor, true);
this.MinimumSize = new System.Drawing.Size(100, 60);
this.Size = new System.Drawing.Size(200, 150);
m_items_draw = m_items_source;
m_pen = new Pen(Color.Black);
m_brush = new SolidBrush(Color.White);
m_sf = new StringFormat();
m_sf.LineAlignment = StringAlignment.Center;
m_dt.Pen = m_pen;
m_dt.SolidBrush = m_brush;
this.ForeColor = Color.FromArgb(255, 220, 220, 220);
this.BackColor = Color.FromArgb(255, 35, 35, 35);
m_tbx.Left = 6;
m_tbx.BackColor = Color.FromArgb(255, 30, 30, 30);
m_tbx.ForeColor = this.ForeColor;
m_tbx.BorderStyle = BorderStyle.None;
m_tbx.MaxLength = 20;
m_tbx.TextChanged += new EventHandler(m_tbx_TextChanged);
this.Controls.Add(m_tbx);
this.AllowDrop = true;
this._Editor = new STNodeEditor();
this._PropertyGrid = new STNodePropertyGrid();
}
#region private method ==========
private void m_tbx_TextChanged(object sender, EventArgs e) {
m_str_search = m_tbx.Text.Trim().ToLower();
m_nSearchOffsetY = 0;
if (m_str_search == string.Empty) {
m_items_draw = m_items_source;
this.Invalidate();
return;
}
m_items_draw = m_items_source.Copy();
this.Search(m_items_draw, new Stack<string>(), m_str_search);
this.Invalidate();
}
private bool Search(STNodeTreeCollection items, Stack<string> stack, string strText) {
bool bFound = false;
string[] strName = new string[items.Count];
int nCounter = 0;
foreach (STNodeTreeCollection v in items) {
if (v.NameLower.IndexOf(strText) != -1) {
v.IsOpen = bFound = true;
} else {
if (!this.Search(v, stack, strText)) {
stack.Push(v.Name);
nCounter++;
} else {
v.IsOpen = bFound = true;
}
}
}
for (int i = 0; i < nCounter; i++) items.Remove(stack.Pop(), false);
return bFound;
}
private bool AddSTNode(Type stNodeType, STNodeTreeCollection items, string strLibName, bool bShowException) {
if (m_dic_all_type.ContainsKey(stNodeType)) return false;
if (stNodeType == null) return false;
if (!stNodeType.IsSubclassOf(m_type_node_base)) {
if (bShowException)
throw new ArgumentException("不支持的类型[" + stNodeType.FullName + "] [stNodeType]参数值必须为[STNode]子类的类型");
else return false;
}
var attr = this.GetNodeAttribute(stNodeType);
if (attr == null) {
if (bShowException)
throw new InvalidOperationException("类型[" + stNodeType.FullName + "]未被[STNodeAttribute]所标记");
else return false;
}
string strPath = string.Empty;
items.STNodeCount++;
if (!string.IsNullOrEmpty(attr.Path)) {
var strKeys = attr.Path.Split(m_chr_splitter);
for (int i = 0; i < strKeys.Length; i++) {
items = items.Add(strKeys[i]);
items.STNodeCount++;
strPath += "/" + strKeys[i];
}
}
try {
STNode node = (STNode)Activator.CreateInstance(stNodeType);
STNodeTreeCollection stt = new STNodeTreeCollection(node.Title);
stt.Path = (strLibName + "/" + attr.Path).Trim('/');
stt.STNodeType = stNodeType;
items[stt.Name] = stt;
stt.STNodeTypeColor = node.TitleColor;
m_dic_all_type.Add(stNodeType, stt.Path);
this.Invalidate();
} catch (Exception ex) {
if (bShowException) throw ex;
return false;
}
return true;
}
private STNodeTreeCollection AddAssemblyPrivate(string strFile) {
strFile = System.IO.Path.GetFullPath(strFile);
var asm = Assembly.LoadFrom(strFile);
STNodeTreeCollection items = new STNodeTreeCollection(System.IO.Path.GetFileNameWithoutExtension(strFile));
foreach (var v in asm.GetTypes()) {
if (v.IsAbstract) continue;
if (v.IsSubclassOf(m_type_node_base)) this.AddSTNode(v, items, items.Name, false);
}
return items;
}
private STNodeAttribute GetNodeAttribute(Type stNodeType) {
if (stNodeType == null) return null;
foreach (var v in stNodeType.GetCustomAttributes(true)) {
if (!(v is STNodeAttribute)) continue;
return (STNodeAttribute)v;
}
return null;
}
private STNodeTreeCollection FindItemByPoint(STNodeTreeCollection items, Point pt) {
foreach (STNodeTreeCollection t in items) {
if (t.DisplayRectangle.Contains(pt)) return t;
if (t.IsOpen) {
var n = FindItemByPoint(t, pt);
if (n != null) return n;
}
}
return null;
}
#endregion
#region overide method ==========
protected override void OnCreateControl() {
base.OnCreateControl();
m_tbx.Top = (m_nItemHeight - m_tbx.Height) / 2;
}
protected override void OnResize(EventArgs e) {
base.OnResize(e);
m_tbx.Width = this.Width - 29;
m_rect_clear = new Rectangle(this.Width - 20, 9, 12, 12);
}
protected override void OnPaintSurface(SKPaintSurfaceEventArgs e) {
base.OnPaintSurface(e);
m_nOffsetY = string.IsNullOrEmpty(m_str_search) ? m_nSourceOffsetY : m_nSearchOffsetY;
m_canvas = e.Surface.Canvas;
m_canvas.Clear(SkiaDrawingHelper.ToSKColor(this.BackColor));
m_canvas.Save();
m_canvas.Translate(0, m_nOffsetY);
int nCounter = 0;
foreach (STNodeTreeCollection v in m_items_draw)
nCounter = this.OnStartDrawItem(m_dt, v, nCounter, 0);
m_nVHeight = (nCounter + 1) * m_nItemHeight;
foreach (STNodeTreeCollection v in m_items_draw)
this.OnDrawSwitch(m_dt, v);
m_canvas.Restore();
this.OnDrawSearch(m_dt);
m_canvas = null;
}
protected override void OnMouseMove(MouseEventArgs e) {
base.OnMouseMove(e);
bool bRedraw = false;
m_pt_offsety = m_pt_control = e.Location;
m_pt_offsety.Y -= m_nOffsetY;
if (!string.IsNullOrEmpty(m_str_search) && m_rect_clear.Contains(e.Location))
this.Cursor = Cursors.Hand;
else
this.Cursor = Cursors.Arrow;
var node = this.FindItemByPoint(m_items_draw, m_pt_offsety);
if (m_item_hover != node) {
m_item_hover = node;
bRedraw = true;
}
if (node != null) {
bool bHoverInfo = node.InfoRectangle.Contains(m_pt_offsety);
if (bHoverInfo != m_bHoverInfo) {
m_bHoverInfo = bHoverInfo;
bRedraw = true;
}
}
if (bRedraw) this.Invalidate();
}
protected override void OnMouseDown(MouseEventArgs e) {
base.OnMouseDown(e);
this.Focus();
if (!string.IsNullOrEmpty(m_str_search) && m_rect_clear.Contains(e.Location)) {
m_tbx.Text = string.Empty;
return;
}
m_pt_offsety = m_pt_control = e.Location;
m_pt_offsety.Y -= m_nOffsetY;
if (m_item_hover == null) return;
if (m_item_hover.SwitchRectangle.Contains(m_pt_offsety)) {
m_item_hover.IsOpen = !m_item_hover.IsOpen;
this.Invalidate();
} else if (m_item_hover.InfoRectangle.Contains(m_pt_offsety)) {
Rectangle rect = this.RectangleToScreen(m_item_hover.DisplayRectangle);
FrmNodePreviewPanel frm = new FrmNodePreviewPanel(m_item_hover.STNodeType,
new Point(rect.Right - m_nItemHeight, rect.Top + m_nOffsetY),
m_nItemHeight,
this._InfoPanelIsLeftLayout,
this._Editor, this._PropertyGrid);
frm.BackColor = this.BackColor;
frm.Show(this);
} else if (m_item_hover.STNodeType != null) {
DataObject d = new DataObject("STNodeType", m_item_hover.STNodeType);
this.DoDragDrop(d, DragDropEffects.Copy);
}
}
protected override void OnMouseDoubleClick(MouseEventArgs e) {
base.OnMouseDoubleClick(e);
m_pt_offsety = m_pt_control = e.Location;
m_pt_offsety.Y -= m_nOffsetY;
STNodeTreeCollection item = this.FindItemByPoint(m_items_draw, m_pt_offsety);
if (item == null || item.STNodeType != null) return;
item.IsOpen = !item.IsOpen;
this.Invalidate();
}
protected override void OnMouseLeave(EventArgs e) {
base.OnMouseLeave(e);
if (m_item_hover != null) {
m_item_hover = null;
this.Invalidate();
}
}
protected override void OnMouseWheel(MouseEventArgs e) {
base.OnMouseWheel(e);
if (e.Delta > 0) {
if (m_nOffsetY == 0) return;
m_nOffsetY += m_nItemHeight;
if (m_nOffsetY > 0) m_nOffsetY = 0;
} else {
if (this.Height - m_nOffsetY >= m_nVHeight) return;
m_nOffsetY -= m_nItemHeight;
}
if (string.IsNullOrEmpty(m_str_search))
m_nSourceOffsetY = m_nOffsetY;
else
m_nSearchOffsetY = m_nOffsetY;
this.Invalidate();
}
#endregion
#region protected method ==========
/// <summary>
/// 当绘制检索文本区域时候发生
/// </summary>
/// <param name="dt">绘制工具</param>
protected virtual void OnDrawSearch(DrawingTools dt) {
if (m_canvas == null) return;
using (var title = new SKPaint { Color = SkiaDrawingHelper.ToSKColor(this._TitleColor), Style = SKPaintStyle.Fill, IsAntialias = true })
using (var box = new SKPaint { Color = SkiaDrawingHelper.ToSKColor(m_tbx.BackColor), Style = SKPaintStyle.Fill, IsAntialias = true })
using (var stroke = new SKPaint { Color = SkiaDrawingHelper.ToSKColor(this.ForeColor), Style = SKPaintStyle.Stroke, StrokeWidth = 1, IsAntialias = true }) {
m_canvas.DrawRect(0, 0, this.Width, m_nItemHeight, title);
m_canvas.DrawRect(5, 5, this.Width - 10, m_nItemHeight - 10, box);
if (string.IsNullOrEmpty(m_str_search)) {
m_canvas.DrawOval(new SKRect(this.Width - 17, 8, this.Width - 9, 16), stroke);
m_canvas.DrawLine(this.Width - 13, 17, this.Width - 13, m_nItemHeight - 9, stroke);
} else {
m_canvas.DrawOval(new SKRect(this.Width - 20, 9, this.Width - 10, 19), stroke);
m_canvas.DrawLine(this.Width - 18, 11, this.Width - 12, 17, stroke);
m_canvas.DrawLine(this.Width - 12, 11, this.Width - 18, 17, stroke);
}
}
}
/// <summary>
/// 当开始绘制树节点的每一个节点时候发生
/// </summary>
/// <param name="dt">绘制工具</param>
/// <param name="Items">当前需要绘制的集合</param>
/// <param name="nCounter">已经绘制个数的计数器</param>
/// <param name="nLevel">当前位于第几级子集合</param>
/// <returns>已经绘制个数</returns>
protected virtual int OnStartDrawItem(DrawingTools dt, STNodeTreeCollection Items, int nCounter, int nLevel) {
Items.DisplayRectangle = new Rectangle(0, m_nItemHeight * (nCounter + 1), this.Width, m_nItemHeight);
Items.SwitchRectangle = new Rectangle(5 + nLevel * 10, (nCounter + 1) * m_nItemHeight, 10, m_nItemHeight);
if (this._ShowInfoButton && Items.STNodeType != null)
Items.InfoRectangle = new Rectangle(this.Width - 18, Items.DisplayRectangle.Top + (m_nItemHeight - 14) / 2, 14, 14);
else Items.InfoRectangle = Rectangle.Empty;
this.OnDrawItem(dt, Items, nCounter++, nLevel);
if (!Items.IsOpen) return nCounter;
foreach (STNodeTreeCollection n in Items) {
if (n.STNodeType == null)
nCounter = this.OnStartDrawItem(dt, n, nCounter++, nLevel + 1);
}
foreach (STNodeTreeCollection n in Items) {
if (n.STNodeType != null)
nCounter = this.OnStartDrawItem(dt, n, nCounter++, nLevel + 1);
}
foreach (STNodeTreeCollection v in Items) this.OnDrawSwitch(dt, v);
return nCounter;
}
/// <summary>
/// 当绘制树节点每一个节点时候发生
/// </summary>
/// <param name="dt">绘制工具</param>
/// <param name="items">当前需要绘制的集合</param>
/// <param name="nCounter">已经绘制个数的计数器</param>
/// <param name="nLevel">当前位于第几级子集合</param>
protected virtual void OnDrawItem(DrawingTools dt, STNodeTreeCollection items, int nCounter, int nLevel) {
if (m_canvas == null) return;
using (var fill = new SKPaint { Style = SKPaintStyle.Fill, IsAntialias = true })
using (var line = new SKPaint { Style = SKPaintStyle.Stroke, StrokeWidth = 1, IsAntialias = true }) {
fill.Color = SkiaDrawingHelper.ToSKColor(nCounter % 2 == 0 ? m_clr_item_1 : m_clr_item_2);
m_canvas.DrawRect(items.DisplayRectangle.Left, items.DisplayRectangle.Top, items.DisplayRectangle.Width, items.DisplayRectangle.Height, fill);
if (items == m_item_hover) {
fill.Color = SkiaDrawingHelper.ToSKColor(this._ItemHoverColor);
m_canvas.DrawRect(items.DisplayRectangle.Left, items.DisplayRectangle.Top, items.DisplayRectangle.Width, items.DisplayRectangle.Height, fill);
}
Rectangle rect = new Rectangle(45 + nLevel * 10, items.SwitchRectangle.Top, this.Width - 45 - nLevel * 10, m_nItemHeight);
line.Color = SkiaDrawingHelper.ToSKColor(Color.FromArgb(100, 125, 125, 125));
m_canvas.DrawLine(9, items.SwitchRectangle.Top + m_nItemHeight / 2, items.SwitchRectangle.Left + 19, items.SwitchRectangle.Top + m_nItemHeight / 2, line);
if (nCounter != 0) {
for (int i = 0; i <= nLevel; i++) {
m_canvas.DrawLine(9 + i * 10, items.SwitchRectangle.Top - m_nItemHeight / 2, 9 + i * 10, items.SwitchRectangle.Top + m_nItemHeight / 2 - 1, line);
}
}
this.OnDrawItemText(dt, items, rect);
this.OnDrawItemIcon(dt, items, rect);
}
}
/// <summary>
/// 当绘制树节点展开与关闭开关时候发生
/// </summary>
/// <param name="dt">绘制工具</param>
/// <param name="items">当前需要绘制的集合</param>
protected virtual void OnDrawSwitch(DrawingTools dt, STNodeTreeCollection items) {
if (m_canvas == null) return;
if (items.Count != 0) {
using (var stroke = new SKPaint { Color = SkiaDrawingHelper.ToSKColor(this._SwitchColor), Style = SKPaintStyle.Stroke, StrokeWidth = 1, IsAntialias = true }) {
int nT = items.SwitchRectangle.Y + m_nItemHeight / 2 - 4;
m_canvas.DrawRect(items.SwitchRectangle.Left, nT, 8, 8, stroke);
m_canvas.DrawLine(items.SwitchRectangle.Left + 1, nT + 4, items.SwitchRectangle.Right - 3, nT + 4, stroke);
if (items.IsOpen) return;
m_canvas.DrawLine(items.SwitchRectangle.Left + 4, nT + 1, items.SwitchRectangle.Left + 4, nT + 7, stroke);
}
//if (items.IsOpen) {
// //g.FillPolygon(m_brush, new Point[]{
// // new Point(items.DotRectangle.Left + 0, items.DotRectangle.Top + m_nItemHeight / 2 - 2),
// // new Point(items.DotRectangle.Left + 9, items.DotRectangle.Top + m_nItemHeight / 2 - 2),
// // new Point(items.DotRectangle.Left + 4, items.DotRectangle.Top + m_nItemHeight / 2 + 3)
// //});
// g.DrawRectangle(m_pen, items.SwitchRectangle.Left, nT, 8, 8);
// g.DrawLine(m_pen, items.SwitchRectangle.Left + 1, nT + 4, items.SwitchRectangle.Right - 3, nT + 4);
//} else {
// //g.FillPolygon(m_brush, new Point[]{
// // new Point(items.DotRectangle.Left + 2, items.DotRectangle.Top + m_nItemHeight / 2 - 5),
// // new Point(items.DotRectangle.Left + 2, items.DotRectangle.Top + m_nItemHeight / 2 + 5),
// // new Point(items.DotRectangle.Left + 7, items.DotRectangle.Top + m_nItemHeight / 2)
// //});
// g.DrawRectangle(m_pen, items.SwitchRectangle.Left, nT, 8, 8);
// g.DrawLine(m_pen, items.SwitchRectangle.Left + 1, nT + 4, items.SwitchRectangle.Right - 3, nT + 4);
// g.DrawLine(m_pen, items.SwitchRectangle.Left + 4, nT + 1, items.SwitchRectangle.Left + 4, nT + 7);
//}
}
}
/// <summary>
/// 当绘制树节点的文本时候发生
/// </summary>
/// <param name="dt">绘制工具</param>
/// <param name="items">当前需要绘制的集合</param>
/// <param name="rect">文本域所在矩形区域</param>
protected virtual void OnDrawItemText(DrawingTools dt, STNodeTreeCollection items, Rectangle rect) {
if (m_canvas == null) return;
rect.Width -= 20;
using (var textNormal = new SKPaint { Color = SkiaDrawingHelper.ToSKColor(items.STNodeType == null ? Color.FromArgb(this.ForeColor.A * 2 / 3, this.ForeColor) : this.ForeColor), TextSize = Math.Max(10f, this.Font.Size), IsAntialias = true })
using (var textHi = new SKPaint { Color = SkiaDrawingHelper.ToSKColor(this._HightLightTextColor), TextSize = Math.Max(10f, this.Font.Size), IsAntialias = true }) {
var fm = textNormal.FontMetrics;
float y = rect.Top + (rect.Height - (fm.Descent - fm.Ascent)) / 2 - fm.Ascent;
string name = items.Name ?? string.Empty;
if (!string.IsNullOrEmpty(m_str_search)) {
int idx = items.NameLower.IndexOf(m_str_search);
if (idx >= 0) {
string pre = name.Substring(0, idx);
string hit = name.Substring(idx, Math.Min(m_str_search.Length, name.Length - idx));
string suf = name.Substring(idx + hit.Length);
float x = rect.Left;
m_canvas.DrawText(pre, x, y, textNormal);
x += textNormal.MeasureText(pre);
m_canvas.DrawText(hit, x, y, textHi);
x += textHi.MeasureText(hit);
m_canvas.DrawText(suf, x, y, textNormal);
return;
}
}
m_canvas.DrawText(name, rect.Left, y, textNormal);
}
}
/// <summary>
/// 当绘制树节点图标时候发生
/// </summary>
/// <param name="dt">绘制工具</param>
/// <param name="items">当前需要绘制的集合</param>
/// <param name="rect">文本域所在矩形区域</param>
protected virtual void OnDrawItemIcon(DrawingTools dt, STNodeTreeCollection items, Rectangle rect) {
if (m_canvas == null) return;
if (items.STNodeType != null) {
using (var stroke = new SKPaint { Color = SkiaDrawingHelper.ToSKColor(this._AutoColor ? items.STNodeTypeColor : Color.DarkCyan), Style = SKPaintStyle.Stroke, StrokeWidth = 1, IsAntialias = true })
using (var fill = new SKPaint { Color = SKColors.LightGray, Style = SKPaintStyle.Fill, IsAntialias = true }) {
m_canvas.DrawRect(rect.Left - 15, rect.Top + m_nItemHeight / 2 - 5, 11, 10, stroke);
m_canvas.DrawRect(rect.Left - 17, rect.Top + m_nItemHeight / 2 - 2, 5, 5, fill);
m_canvas.DrawRect(rect.Left - 6, rect.Top + m_nItemHeight / 2 - 2, 5, 5, fill);
if (m_item_hover == items && m_bHoverInfo) {
fill.Color = SkiaDrawingHelper.ToSKColor(this.BackColor);
m_canvas.DrawRect(items.InfoRectangle.Left, items.InfoRectangle.Top, items.InfoRectangle.Width, items.InfoRectangle.Height, fill);
}
}
using (var info = new SKPaint { Color = SkiaDrawingHelper.ToSKColor(this._AutoColor ? items.STNodeTypeColor : this._InfoButtonColor), Style = SKPaintStyle.Stroke, StrokeWidth = 2, IsAntialias = true }) {
m_canvas.DrawLine(items.InfoRectangle.X + 4, items.InfoRectangle.Y + 3, items.InfoRectangle.X + 10, items.InfoRectangle.Y + 3, info);
m_canvas.DrawLine(items.InfoRectangle.X + 4, items.InfoRectangle.Y + 6, items.InfoRectangle.X + 10, items.InfoRectangle.Y + 6, info);
m_canvas.DrawLine(items.InfoRectangle.X + 4, items.InfoRectangle.Y + 11, items.InfoRectangle.X + 10, items.InfoRectangle.Y + 11, info);
m_canvas.DrawLine(items.InfoRectangle.X + 7, items.InfoRectangle.Y + 7, items.InfoRectangle.X + 7, items.InfoRectangle.Y + 10, info);
}
using (var box = new SKPaint { Color = SkiaDrawingHelper.ToSKColor(this._AutoColor ? items.STNodeTypeColor : this._InfoButtonColor), Style = SKPaintStyle.Stroke, StrokeWidth = 1, IsAntialias = true }) {
m_canvas.DrawRect(items.InfoRectangle.X, items.InfoRectangle.Y, items.InfoRectangle.Width - 1, items.InfoRectangle.Height - 1, box);
}
} else {
using (var stroke = new SKPaint { Style = SKPaintStyle.Stroke, StrokeWidth = 1, IsAntialias = true }) {
if (items.IsLibraryRoot) {
Rectangle rect_box = new Rectangle(rect.Left - 15, rect.Top + m_nItemHeight / 2 - 5, 11, 10);
stroke.Color = SKColors.Gray;
m_canvas.DrawRect(rect_box.Left, rect_box.Top, rect_box.Width, rect_box.Height, stroke);
stroke.Color = SKColors.Cyan;
m_canvas.DrawLine(rect_box.X - 2, rect_box.Top, rect_box.X + 2, rect_box.Top, stroke);
m_canvas.DrawLine(rect_box.X, rect_box.Y - 2, rect_box.X, rect_box.Y + 2, stroke);
m_canvas.DrawLine(rect_box.Right - 2, rect_box.Bottom, rect_box.Right + 2, rect_box.Bottom, stroke);
m_canvas.DrawLine(rect_box.Right, rect_box.Bottom - 2, rect_box.Right, rect_box.Bottom + 2, stroke);
} else {
stroke.Color = SKColors.Goldenrod;
m_canvas.DrawRect(rect.Left - 16, rect.Top + m_nItemHeight / 2 - 6, 8, 3, stroke);
m_canvas.DrawRect(rect.Left - 16, rect.Top + m_nItemHeight / 2 - 3, 13, 9, stroke);
}
}
if (!this._ShowFolderCount) return;
using (var text = new SKPaint { Color = SkiaDrawingHelper.ToSKColor(this._FolderCountColor), TextSize = Math.Max(10f, this.Font.Size), IsAntialias = true }) {
string t = "[" + items.STNodeCount.ToString() + "]";
float w = text.MeasureText(t);
var fm = text.FontMetrics;
float y = rect.Top + (rect.Height - (fm.Descent - fm.Ascent)) / 2 - fm.Ascent;
m_canvas.DrawText(t, rect.Right - w - 4, y, text);
}
}
}
#endregion
#region public method ==========
/// <summary>
/// 在控件中检索STNode
/// </summary>
/// <param name="strText">需要检索的文本</param>
public void Search(string strText) {
if (strText == null) return;
if (strText.Trim() == string.Empty) return;
m_tbx.Text = strText.Trim();
}
/// <summary>
/// 向控件中添加一个STNode类型
/// </summary>
/// <param name="stNodeType">STNode类型</param>
/// <returns>是否添加成功</returns>
public bool AddNode(Type stNodeType) { return this.AddSTNode(stNodeType, m_items_source, null, true); }
/// <summary>
/// 从文件中向控件添加STNode类型
/// </summary>
/// <param name="strFile">指定文件路径</param>
/// <returns>添加成功个数</returns>
public int LoadAssembly(string strFile) {
strFile = System.IO.Path.GetFullPath(strFile);
var items = this.AddAssemblyPrivate(strFile);
if (items.STNodeCount == 0) return 0;
items.IsLibraryRoot = true;
m_items_source[items.Name] = items;
return items.STNodeCount;
}
/// <summary>
/// 清空控件中所有STNode类型
/// </summary>
public void Clear() {
m_items_source.Clear();
m_items_draw.Clear();
m_dic_all_type.Clear();
this.Invalidate();
}
/// <summary>
/// 向控件中移除一个STNode类型
/// </summary>
/// <param name="stNodeType">STNode类型</param>
/// <returns>是否移除成功</returns>
public bool RemoveNode(Type stNodeType) {
if (!m_dic_all_type.ContainsKey(stNodeType)) return false;
string strPath = m_dic_all_type[stNodeType];
STNodeTreeCollection items = m_items_source;
if (!string.IsNullOrEmpty(strPath)) {
string[] strKeys = strPath.Split(m_chr_splitter);
for (int i = 0; i < strKeys.Length; i++) {
items = items[strKeys[i]];
if (items == null) return false;
}
}
try {
STNode node = (STNode)Activator.CreateInstance(stNodeType);
if (items[node.Title] == null) return false;
items.Remove(node.Title, true);
m_dic_all_type.Remove(stNodeType);
} catch { return false; }
this.Invalidate();
return true;
}
#endregion
//=================================================================================================
/// <summary>
/// STNodeTreeView控件中每一项的集合
/// </summary>
protected class STNodeTreeCollection : IEnumerable
{
private string _Name;
/// <summary>
/// 获取当前树节点显示名称
/// </summary>
public string Name {
get {
return _Name;
}
}
/// <summary>
/// 获取当前树节点显示名称的小写字符串
/// </summary>
public string NameLower { get; private set; }
/// <summary>
/// 获取当前树节点对应的STNode类型
/// </summary>
public Type STNodeType { get; internal set; }
/// <summary>
/// 获取当前树节点的父级树节点
/// </summary>
public STNodeTreeCollection Parent { get; internal set; }
/// <summary>
/// 获取当前树节点下拥有的STNode类型个数
/// </summary>
public int STNodeCount { get; internal set; }
/// <summary>
/// 获取当前树节点对应STNode类型在树控件中对应路径
/// </summary>
public string Path { get; internal set; }
/// <summary>
/// 获取当前或设置树节点是否为打开状态
/// </summary>
public bool IsOpen { get; set; }
/// <summary>
/// 获取当前树节点是否为加载模块的根路劲节点
/// </summary>
public bool IsLibraryRoot { get; internal set; }
/// <summary>
/// 获取当前树节点在控件中的显示区域
/// </summary>
public Rectangle DisplayRectangle { get; internal set; }
/// <summary>
/// 获取当前树节点在控件中的开关按钮区域
/// </summary>
public Rectangle SwitchRectangle { get; internal set; }
/// <summary>
/// 获取当前树节点在控件中的信息按钮区域
/// </summary>
public Rectangle InfoRectangle { get; internal set; }
/// <summary>
/// 获取当前树节点对应STNode类型的标题颜色
/// </summary>
public Color STNodeTypeColor { get; internal set; }
/// <summary>
/// 获取当前树节点所包含子节点个数
/// </summary>
public int Count { get { return m_dic.Count; } }
/// <summary>
/// 获取或设置指定名称的集合
/// </summary>
/// <param name="strKey">指定名称</param>
/// <returns>集合</returns>
public STNodeTreeCollection this[string strKey] {
get {
if (string.IsNullOrEmpty(strKey)) return null;
if (m_dic.ContainsKey(strKey)) return m_dic[strKey];
return null;
}
set {
if (string.IsNullOrEmpty(strKey)) return;
if (value == null) return;
if (m_dic.ContainsKey(strKey)) {
m_dic[strKey] = value;
} else {
m_dic.Add(strKey, value);
}
value.Parent = this;
}
}
private SortedDictionary<string, STNodeTreeCollection> m_dic = new SortedDictionary<string, STNodeTreeCollection>();
/// <summary>
/// 构造一颗树节点集合
/// </summary>
/// <param name="strName">当前树节点在控件中的显示名称</param>
public STNodeTreeCollection(string strName) {
if (strName == null || strName.Trim() == string.Empty)
throw new ArgumentNullException("显示名称不能为空");
this._Name = strName.Trim();
this.NameLower = this._Name.ToLower();
}
/// <summary>
/// 向当前树节点中添加一个子节点
/// </summary>
/// <param name="strName">节点显示名称</param>
/// <returns>添加后的子节点集合</returns>
public STNodeTreeCollection Add(string strName) {
if (!m_dic.ContainsKey(strName))
m_dic.Add(strName, new STNodeTreeCollection(strName) { Parent = this });
return m_dic[strName];
}
/// <summary>
/// 向当前树节点中删除一个子集合
/// </summary>
/// <param name="strName">子集合名称</param>
/// <param name="isAutoDelFolder">是否递归向上自动清空无用节点</param>
/// <returns>是否删除成功</returns>
public bool Remove(string strName, bool isAutoDelFolder) {
if (!m_dic.ContainsKey(strName)) return false;
bool b = m_dic.Remove(strName);
var temp = this;
while (temp != null) {
temp.STNodeCount--;
temp = temp.Parent;
}
if (isAutoDelFolder && m_dic.Count == 0 && this.Parent != null)
return b && this.Parent.Remove(this.Name, isAutoDelFolder);
return b;
}
/// <summary>
/// 清空当前树节点中所有子节点
/// </summary>
public void Clear() { this.Clear(this); }
private void Clear(STNodeTreeCollection items) {
foreach (STNodeTreeCollection v in items) v.Clear(v);
m_dic.Clear();
}
/// <summary>
/// 获取当前树节点中所有的名称数组
/// </summary>
/// <returns></returns>
public string[] GetKeys() { return m_dic.Keys.ToArray(); }
/// <summary>
/// 拷贝当前树节点集合中所有数据
/// </summary>
/// <returns>拷贝的副本</returns>
public STNodeTreeCollection Copy() {
STNodeTreeCollection items = new STNodeTreeCollection("COPY");
this.Copy(this, items);
return items;
}
private void Copy(STNodeTreeCollection items_src, STNodeTreeCollection items_dst) {
foreach (STNodeTreeCollection v in items_src) {
this.Copy(v, items_dst.Add(v.Name));
}
items_dst.Path = items_src.Path;
items_dst.STNodeType = items_src.STNodeType;
items_dst.IsLibraryRoot = items_src.IsLibraryRoot;
items_dst.STNodeCount = items_src.STNodeCount;
items_dst.STNodeTypeColor = items_src.STNodeTypeColor;
}
/// <summary>
/// 返回 System.Collections.IEnumerator 的 Array
/// </summary>
/// <returns></returns>
public IEnumerator GetEnumerator() {
foreach (var v in m_dic.Values) yield return v;
}
IEnumerator IEnumerable.GetEnumerator() {
return this.GetEnumerator();
}
}
}
}