ningshuxia
2023-02-07 d027501182b03a0c036af712a2b7f31d55144562
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
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Drawing;
using System.Data;
using System.Text;
using System.Linq;
using System.Windows.Forms;
using System.Drawing.Drawing2D;
using System.Drawing.Imaging;
 
namespace IStation.WinFormUI
{
    /// <summary>
    /// 自定义TreeList控件
    /// </summary>
    public partial class CTreeList : TreeView
    {
        public CTreeList()
        {
            InitializeComponent();
            this.ItemHeight = 200;
            this.SetStyle(ControlStyles.OptimizedDoubleBuffer, true);
            this.DrawMode = TreeViewDrawMode.OwnerDrawAll;
            this.ShowLines = true; 
            
        }
 
        #region 属性
        
        private bool _isDrawBorder = false;
        /// <summary>
        /// 是否绘制边框
        /// </summary>
        [Description("是否绘制边框"), Category("XOProperty")]
        public bool IsDrawBorder
        {
            get
            {
                return _isDrawBorder;
            }
            set
            {
                if (_isDrawBorder != value)
                {
                    _isDrawBorder = value;
                    this.Invalidate();
                }
            }
        }
 
 
        private Color _borderColor = Color.Transparent;
        /// <summary>
        /// 绘制节点边框的颜色
        /// </summary>
        [Description("绘制节点边框的颜色"), Category("XOProperty")]
        public Color BorderColor
        {
            get
            {
                return _borderColor;
            }
            set
            {
                if (_borderColor != value)
                {
                    _borderColor = value;
                    this.Invalidate(); 
                }
            }
        }
 
 
        private Color _nodeBgColor = Color.FromArgb(242, 242, 242);
        /// <summary>
        /// 绘制Node的背景色
        /// </summary>
        [Description("绘制Node的背景色"), Category("XOProperty")]
        public Color NodeBgColor
        {
            get
            {
                return _nodeBgColor;
            }
            set
            {
                if (_nodeBgColor != value)
                {
                    _nodeBgColor = value;
                    this.Invalidate();
 
                }
            }
        }
         
        private Color _textColor = Color.White;
 
        /// <summary>
        /// 绘制文本的颜色
        /// </summary>
        [Description("绘制Text的背景色"), Category("XOProperty")]
        public Color TextColor
        {
            get
            {
                return _textColor;
            }
            set
            {
                if (_textColor != value)
                {
                    _textColor = value;
                    this.Invalidate();
 
                }
            }
        }
 
        private Color _selectedBorderColor = Color.Transparent;
        /// <summary>
        /// 绘制选中节点边框的颜色
        /// </summary>
        [Description("绘制选中节点边框的颜色"), Category("XOProperty")]
        public Color SelectedBorderColor
        {
            get
            {
                return _selectedBorderColor;
            }
            set
            {
                if (_selectedBorderColor != value)
                {
                    _selectedBorderColor = value;
                    this.Invalidate();
                }
            }
        }
 
         
        private Color _selectedTextColor = Color.White; 
        /// <summary>
        /// 选中Node文字颜色
        /// </summary>
        [Description("选中Node文字颜色"), Category("XOProperty")]
        public Color SelectedTextColor
        {
            get
            {
                return _selectedTextColor;
            }
            set
            {
                if (_selectedTextColor != value)
                {
                    _selectedTextColor = value;
                    this.Invalidate();
                }
            }
        }
 
 
        private Color _selectedBgColor = Color.FromArgb(129, 211, 248);
        /// <summary>
        /// 选中Node的背景颜色
        /// </summary>
        [Description("选中Node背景颜色"), Category("XOProperty")]
        public Color SelectedBgColor
        {
            get
            {
                return _selectedBgColor;
            }
            set
            {
                if (_selectedBgColor != value)
                {
                    _selectedBgColor = value;
                    this.Invalidate();
                }
            }
        }
 
 
        private int _nodelevelWidth = 30;
        /// <summary>
        /// 节点缩进宽度
        /// </summary>
        [Description("节点缩进宽度"), Category("XOProperty")]
        public int ChildNodeLevelWidth
        {
            get
            {
                return _nodelevelWidth;
            }
            set
            {
                if (_nodelevelWidth != value)
                {
                    _nodelevelWidth = value;
                    this.Invalidate();
                }
            }
        }
 
        private int _spaceText = 10;
        /// <summary>
        /// 图片与文字的间距
        /// </summary>
        [Description("图片与文字的间距"), Category("XOProperty")]
        public int SpaceText
        {
            get
            {
                return _spaceText;
            }
            set
            {
                if (_spaceText != value)
                {
                    _spaceText = value;
                    this.Invalidate();
                }
            }
        }
 
 
        private int _leftSpaceImg = 10;
        /// <summary>
        /// 图片与左边框的间距
        /// </summary>
        [Description("图片与文字的间距"), Category("XOProperty")]
        public int LeftSpaceImg
        {
            get
            {
                return _leftSpaceImg;
            }
            set
            {
                if (_leftSpaceImg != value)
                {
                    _leftSpaceImg = value;
                    this.Invalidate();
                }
            }
        }
 
 
        private int _leftSpace = 30;
        /// <summary>
        /// 左边距
        /// </summary>
        [Description("左边距"), Category("XOProperty")]
        public int LeftSpace
        {
            get
            {
                return _leftSpace;
            }
            set
            {
                if (_leftSpace != value)
                {
                    _leftSpace = value;
                    this.Invalidate();
                }
            }
        }
 
        private int _drawNodeHeight = 180;
        /// <summary>
        /// 绘制的节点的高度
        /// </summary>
        [Description("绘制的节点的高度"), Category("XOProperty")]
        public int DrawNodeHeight
        {
            get
            {
                return _drawNodeHeight;
            }
            set
            {
                if (_drawNodeHeight > ItemHeight)
                    _drawNodeHeight = ItemHeight;
                if (_drawNodeHeight != value)
                {
                    _drawNodeHeight = value;
                    this.Invalidate();
                }
            }
        }
        
 
        private Image _butExpandImage = null;
        /// <summary>
        /// 下拉按钮图片
        /// </summary>
        [Description("下拉按钮图片"), Category("XOProperty")]
        public Image ButImage
        {
            get
            {
                return _butExpandImage;
            }
            set
            {
                if (_butExpandImage != value)
                {
                    _butExpandImage = value;
                    this.Invalidate();
                }
            }
        }
         
        #endregion
 
        private List<CTreeNode> _treeNodeList;
 
        /// <summary>
        /// 节点改变事件
        /// </summary>
        public event Action<CTreeNode> FocusedNodeChangedEvent = null;
         
        //设置属性
        public void SetNodes(List<CTreeNode> tree)
        {
            if (tree == null||tree.Count<1)
                return;
            _treeNodeList = tree;
            _treeNodeList.ForEach(node =>
            {
                var rootNode = new System.Windows.Forms.TreeNode();
                rootNode.Text = node.Caption;
                rootNode.Tag = node; 
                this.Nodes.Add(rootNode);
            }); 
        }
         
        private Size ExpandButtonSize = new Size(16, 16);
 
        /// <summary>
        /// 自定义绘制节点
        /// </summary>
        /// <param name="e"></param>
        private void DrawNodeItem(DrawTreeNodeEventArgs e)
        {
            System.Windows.Forms.TreeNode node = e.Node;  
            var model = node.Tag as CTreeNode;
            if (model == null)
                return; 
            using (Graphics g = e.Graphics)
            {
             Rectangle rect;
             var am_height = this.DrawNodeHeightAugment(); 
             rect = new Rectangle(e.Bounds.X, e.Bounds.Y + am_height, (int)(this.Width * model.DrawNodeWidthRatio), DrawNodeHeight);
 
                var path = BuildGraphicsPath(e.Bounds.Location, (int)(this.Width * model.DrawNodeWidthRatio), DrawNodeHeight);
                //绘制分组的背景
                RenderBackgroundInternalRate(node, g, path);
 
                Point p_cap = new Point(rect.X + LeftSpace, rect.Y + rect.Height / 2);
                if (node.IsSelected)
                {
                    TextRenderer.DrawText(g, model.Caption, new Font(this.Font.FontFamily.Name, this.Font.Size, FontStyle.Regular), p_cap, this.SelectedTextColor, TextFormatFlags.Left | TextFormatFlags.VerticalCenter);
                }
                else
                {
                    TextRenderer.DrawText(g, model.Caption, new Font(this.Font.FontFamily.Name, this.Font.Size, FontStyle.Regular), p_cap, this.TextColor, TextFormatFlags.Left | TextFormatFlags.VerticalCenter);
                }
 
                Point p_value = new Point(rect.Width- (int)(rect.Width * 0.2), rect.Y+rect.Height/2);
 
                /*if (node.IsSelected)
                {
                    TextRenderer.DrawText(g, model.Range, new Font(this.Font.FontFamily.Name, this.Font.Size, FontStyle.Regular), p_value, this.SelectedTextColor, TextFormatFlags.Left | TextFormatFlags.VerticalCenter);
                }
                else
                {
                    TextRenderer.DrawText(g, model.Range, new Font(this.Font.FontFamily.Name, this.Font.Size, FontStyle.Regular), p_value, this.TextColor, TextFormatFlags.Left | TextFormatFlags.VerticalCenter);
                }*/
            }
        }
         
        #region override
        protected override void OnDrawNode(DrawTreeNodeEventArgs e)
        {
            DrawNodeItem(e);
        }
 
        protected override void WndProc(ref Message m)
        {
            if (m.Msg == 0x0201)//单击
            {
                int wparam = m.LParam.ToInt32();
                Point point = new Point(LOWORD(wparam), HIWORD(wparam));
                System.Windows.Forms.TreeNode tn = this.GetNodeAt(point);
                if (tn == null)
                {
                    base.WndProc(ref m);
                    return;
                }
                if (IsChlckNode(tn, point))
                {
                    this.SelectedNode = tn; 
                }
 
            }
            base.WndProc(ref m);
 
        }
        #endregion
 
        //判断鼠标是否点击在绘制的节点上
        private bool IsChlckNode(System.Windows.Forms.TreeNode node, Point p) 
        {
            var model = node.Tag as CTreeNode;
            var am_height = this.DrawNodeHeightAugment();
            var rect = new Rectangle(1, node.Bounds.Y + am_height, this.Width, DrawNodeHeight);
            var a = node.TreeView.Nodes[0];
            return rect.Contains(p);
        }
         
        //绘制多边形
        private GraphicsPath BuildGraphicsPath(Point  basePoint, float width, float height)
        {
            GraphicsPath path = new GraphicsPath();
            path.AddLine(basePoint.X, basePoint.Y, basePoint.X + width, basePoint.Y);
            path.AddLine(basePoint.X + width, basePoint.Y, basePoint.X + width + height/2, basePoint.Y + height/2);
            path.AddLine(basePoint.X + width + height / 2, basePoint.Y + height / 2, basePoint.X + width  , basePoint.Y + height  );
            path.AddLine(basePoint.X + width  , basePoint.Y + height , basePoint.X , basePoint.Y+height );
            path.AddLine(basePoint.X , basePoint.Y + height, basePoint.X, basePoint.Y );
            return path;
        }
        
        //绘制节点
        internal void RenderBackgroundInternalRate(System.Windows.Forms.TreeNode node, Graphics g, GraphicsPath rect)
        {
            var model = node.Tag as CTreeNode;
             
            if (node.IsSelected)
            {
                using (var brushAlpha = new SolidBrush(SelectedBgColor))
                {
                    g.FillPath(brushAlpha, rect); ; 
                }
            }
            else
            {
                using (var brushAlpha = new SolidBrush(model.Color))
                {
                    g.FillPath(brushAlpha, rect);  
                }
            }
            
        }
 
        //绘制节点高度的增量
        private int DrawNodeHeightAugment() 
        {
            if (DrawNodeHeight >= ItemHeight) 
                return 0;
            return (ItemHeight - DrawNodeHeight) / 2;
        }
 
        private static int LOWORD(int value)
        {
            return value & 0xFFFF;
        }
 
        private static int HIWORD(int value)
        {
            return value >> 16;
        }
 
        private CTreeNode _currentClickNode;
 
        private void CTreeList_AfterSelect(object sender, TreeViewEventArgs e)
        {
            if (e.Node == null)
                return;
            var nodeData = e.Node.Tag as CTreeNode;
            if (nodeData == null)
                return;
            if (nodeData.IsReadOnly)
                return;
            _currentClickNode = nodeData;
            if (FocusedNodeChangedEvent != null)
                FocusedNodeChangedEvent.Invoke(nodeData); 
        }
 
        public CTreeNode GetFocusedRow()
        {
            return _currentClickNode;
        }
         
        /// <summary>
        /// 自定义创建图片的副本(若PixelFormat为索引像素格式会抛异常))
        /// </summary>
        /// <param name="img"></param>
        /// <param name="format">默认Format32bppArgb</param>
        /// <returns></returns>
        private static Image CloneC(Image img, PixelFormat format = PixelFormat.Format32bppArgb)
        {
            var bmp = new Bitmap(img.Width, img.Height, format);
            using (var g = Graphics.FromImage(bmp))
            {
                g.InterpolationMode = System.Drawing.Drawing2D.InterpolationMode.HighQualityBicubic;
                g.SmoothingMode = System.Drawing.Drawing2D.SmoothingMode.HighQuality;
                g.CompositingQuality = System.Drawing.Drawing2D.CompositingQuality.HighQuality;
                g.DrawImage(img, new Rectangle(0, 0, bmp.Width, bmp.Height));
            }
            return bmp;
        }
    }
}