yangyin
2024-10-25 1e65c49ba929103e79d87322ca1a3573c2b532e2
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
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Drawing;
using System.Drawing.Drawing2D;
using System.Runtime.InteropServices;
using System.Windows.Forms;
using DPumpHydr.WinFrmUI.WenSkin.Properties;
 
namespace DPumpHydr.WinFrmUI.WenSkin.Controls
{
    public partial class TreeViewEx : TreeView
    {
        #region 私有属性
 
        private const int WS_VSCROLL = 2097152;    
        private const int GWL_STYLE = -16;
        private static readonly Font font = new Font("Arial Unicode MS", 12f);
        private int _nodeHeight = 50;
        private SizeF treeFontSize = SizeF.Empty;
        private bool blnHasVBar = false;
        private Color nodeForeColor = Color.White;        
 
        #endregion
 
        #region 公有属性
        public Dictionary<string, string> LstTips { get; set; } = new Dictionary<string, string>();           
        [Category("自定义属性"), Description("角标文字字体")]
        public Font TipFont { get; set; } = font;
        [Category("自定义属性"), Description("是否显示角标")]
        public Image TipImage { get; set; } = Resources.tips;
        [Category("自定义属性"), Description("是否显示角标")]
        public bool IsShowTip { get; set; } = false;
        [Category("自定义属性"), Description("使用自定义模式")]
        public bool IsShowByCustomModel { get; set; } = true;
        [Category("自定义属性"), Description("节点高度(IsShowByCustomModel=true时生效)")]
        public int NodeHeight
        {
            get
            {
                return this._nodeHeight;
            }
            set
            {
                this._nodeHeight = value;
                base.ItemHeight = value;
            }
        }
        [Category("自定义属性"), Description("下翻图标(IsShowByCustomModel=true时生效)")]
        public Image NodeDownPic { get; set; } = Resources.list_add;
        [Category("自定义属性"), Description("上翻图标(IsShowByCustomModel=true时生效)")]
        public Image NodeUpPic { get; set; } = Resources.list_subtract;
        [Category("自定义属性"), Description("节点背景颜色(IsShowByCustomModel=true时生效)")]
        public Color NodeBackgroundColor { get; set; } = Color.White;
        [Category("自定义属性"), Description("节点字体颜色(IsShowByCustomModel=true时生效)"),DefaultValue(typeof(Color),"White")]
        public Color NodeForeColor { get => nodeForeColor; set { base.ForeColor = value; nodeForeColor = value; } }
        [Category("自定义属性"), Description("节点是否显示分割线(IsShowByCustomModel=true时生效)")]
        public bool NodeIsShowSplitLine { get; set; } = false;
        [Category("自定义属性"), Description("节点分割线颜色(IsShowByCustomModel=true时生效)")]
        public Color NodeSplitLineColor { get; set; } = Color.FromArgb(232, 232, 232);
        [Category("自定义属性"), Description("选中节点背景颜色(IsShowByCustomModel=true时生效)")]
        public Color NodeSelectedColor { get; set; } = Color.FromArgb(255, 77, 59);
        [Category("自定义属性"), Description("选中节点字体颜色(IsShowByCustomModel=true时生效)")]
        public Color NodeSelectedForeColor { get; set; } = Color.White;
        [Category("自定义属性"), Description("父节点是否可选中")]
        public bool ParentNodeCanSelect { get; set; } = true;
        #endregion
        public TreeViewEx()
        {
            base.HideSelection = false;
            base.DrawMode = TreeViewDrawMode.OwnerDrawAll;
            base.DrawNode += new DrawTreeNodeEventHandler(this.treeview_DrawNode);
            base.NodeMouseClick += new TreeNodeMouseClickEventHandler(this.TreeViewEx_NodeMouseClick);
            base.SizeChanged += new EventHandler(this.TreeViewEx_SizeChanged);
            base.AfterSelect += new TreeViewEventHandler(this.TreeViewEx_AfterSelect);
            base.FullRowSelect = true;
            base.ShowLines = false;
            base.ShowPlusMinus = false;
            base.ShowRootLines = false;
            this.BackColor = Color.FromArgb(37, 37, 38);
            this.BorderStyle = System.Windows.Forms.BorderStyle.None;
            DoubleBuffered = true;
            ForeColor = Color.White;
        }   
        
        protected override void WndProc(ref Message m)
        {
 
            if (m.Msg == 0x0014) // 禁掉清除背景消息WM_ERASEBKGND
 
                return;
 
            base.WndProc(ref m);
 
        }
        #region 重画
        private void TreeViewEx_AfterSelect(object sender, TreeViewEventArgs e)
        {
            try
            {
                if (e.Node != null)
                {
                    if (!this.ParentNodeCanSelect)
                    {
                        if (e.Node.Nodes.Count > 0)
                        {
                            e.Node.Expand();
                            base.SelectedNode = e.Node.Nodes[0];
                        }
                    }
                }
            }
            catch (Exception ex)
            {
                throw ex;
            }
        }
        private void TreeViewEx_SizeChanged(object sender, EventArgs e)
        {
            this.Refresh();
        }
        private void TreeViewEx_NodeMouseClick(object sender, TreeNodeMouseClickEventArgs e)
        {
            try
            {
                if (e.Node != null)
                {
                    if (e.Node.Nodes.Count > 0)
                    {
                        if (e.Node.IsExpanded)
                        {
                            e.Node.Collapse();
                        }
                        else
                        {
                            e.Node.Expand();
                        }
                    }
                    if (base.SelectedNode != null)
                    {
                        if (base.SelectedNode == e.Node && e.Node.IsExpanded)
                        {
                            if (!this.ParentNodeCanSelect)
                            {
                                if (e.Node.Nodes.Count > 0)
                                {
                                    base.SelectedNode = e.Node.Nodes[0];
                                }
                            }
                        }
                    }
                }
            }
            catch (Exception ex)
            {
                throw ex;
            }
        }
        private void treeview_DrawNode(object sender, DrawTreeNodeEventArgs e)
        {
            try
            {
 
                if (e.Node == null || !this.IsShowByCustomModel || (e.Node.Bounds.Width <= 0 && e.Node.Bounds.Height <= 0 && e.Node.Bounds.X <= 0 && e.Node.Bounds.Y <= 0))
                {
                    e.DrawDefault = true;
                }
                else
                {
                    e.Graphics.SetGDIHigh();
                    if (base.Nodes.IndexOf(e.Node) == 0)
                    {
                        this.blnHasVBar = this.IsVerticalScrollBarVisible();
                    }
                    Font font = e.Node.NodeFont;
                    if (font == null)
                    {
                        font = ((TreeView)sender).Font;
                    }
                    if (this.treeFontSize == SizeF.Empty)
                    {
                        this.treeFontSize = this.GetFontSize(font, e.Graphics);
                    }
                    bool flag = false;
                    int intLeft = 0;
                    if (CheckBoxes)
                    {
                        intLeft = 20;
                    }
                    int num = 0;
                    if (base.ImageList != null && base.ImageList.Images.Count > 0 && e.Node.ImageIndex >= 0 && e.Node.ImageIndex < base.ImageList.Images.Count)
                    {
                        flag = true;
                        num = (e.Bounds.Height - base.ImageList.ImageSize.Height) / 2;
                        intLeft += base.ImageList.ImageSize.Width;
                    }
 
                    intLeft += e.Node.Level * Indent;
 
                    if ((e.State == TreeNodeStates.Selected || e.State == TreeNodeStates.Focused || e.State == (TreeNodeStates.Focused | TreeNodeStates.Selected)) && (this.ParentNodeCanSelect || e.Node.Nodes.Count <= 0))
                    {
                        e.Graphics.FillRectangle(new SolidBrush(this.NodeSelectedColor), new Rectangle(new Point(0, e.Node.Bounds.Y), new Size(base.Width, e.Node.Bounds.Height)));
                        e.Graphics.DrawString(e.Node.Text, font, new SolidBrush(this.NodeSelectedForeColor), (float)e.Bounds.X + intLeft, (float)e.Bounds.Y + ((float)this._nodeHeight - this.treeFontSize.Height) / 2f);
                    }
                    else
                    {
                        //非选中状态画背景和文字
                        e.Graphics.FillRectangle(new SolidBrush(e.Node.BackColor), new Rectangle(new Point(0, e.Node.Bounds.Y), new Size(base.Width, e.Node.Bounds.Height)));
                        e.Graphics.DrawString(e.Node.Text, font, new SolidBrush(NodeForeColor), (float)e.Bounds.X + intLeft, (float)e.Bounds.Y + ((float)this._nodeHeight - this.treeFontSize.Height) / 2f);
                    }
                    if (CheckBoxes)
                    {
                        Rectangle rectCheck = new Rectangle(e.Bounds.X + 3 + e.Node.Level * Indent, e.Bounds.Y + (e.Bounds.Height - 16) / 2, 16, 16);
                        GraphicsPath pathCheck = rectCheck.CreateRoundedRectanglePath(3);
                        e.Graphics.FillPath(new SolidBrush(Color.FromArgb(247, 247, 247)), pathCheck);
                        if (e.Node.Checked)
                        {
                            e.Graphics.DrawLines(new Pen(new SolidBrush(NodeSelectedColor), 2), new Point[]
                            {
                                new Point(rectCheck.Left+2,rectCheck.Top+8),
                                new Point(rectCheck.Left+6,rectCheck.Top+12),
                                new Point(rectCheck.Right-4,rectCheck.Top+4)
                            });
                        }
 
                        e.Graphics.DrawPath(new Pen(new SolidBrush(Color.FromArgb(200, 200, 200))), pathCheck);
                    }
                    if (flag)
                    {
                        int num2 = e.Bounds.X - num - base.ImageList.ImageSize.Width;
                        if (num2 < 0)
                        {
                            num2 = 3;
                        }
                        e.Graphics.DrawImage(base.ImageList.Images[e.Node.ImageIndex], new Rectangle(new Point(num2 + intLeft - base.ImageList.ImageSize.Width, e.Bounds.Y + num), base.ImageList.ImageSize));
                    }
                    if (this.NodeIsShowSplitLine)
                    {
                        e.Graphics.DrawLine(new Pen(this.NodeSplitLineColor, 1f), new Point(0, e.Bounds.Y + this._nodeHeight - 1), new Point(base.Width, e.Bounds.Y + this._nodeHeight - 1));
                    }
                    bool flag2 = false;
                    if (e.Node.Nodes.Count > 0)
                    {
                        if (e.Node.IsExpanded && this.NodeUpPic != null)
                        {
                            e.Graphics.DrawImage(this.NodeUpPic, new Rectangle(base.Width - (this.blnHasVBar ? 50 : 30), e.Bounds.Y + (this._nodeHeight - 20) / 2, 20, 20));
                        }
                        else if (this.NodeDownPic != null)
                        {
                            e.Graphics.DrawImage(this.NodeDownPic, new Rectangle(base.Width - (this.blnHasVBar ? 50 : 30), e.Bounds.Y + (this._nodeHeight - 20) / 2, 20, 20));
                        }
                        flag2 = true;
                    }
                    if (this.IsShowTip && this.LstTips.ContainsKey(e.Node.Name) && !string.IsNullOrWhiteSpace(this.LstTips[e.Node.Name]))
                    {
                        int num3 = base.Width - (this.blnHasVBar ? 50 : 30) - (flag2 ? 20 : 0);
                        int num4 = e.Bounds.Y + (this._nodeHeight - 20) / 2;
                        e.Graphics.DrawImage(this.TipImage, new Rectangle(num3, num4, 20, 20));
                        SizeF sizeF = e.Graphics.MeasureString(this.LstTips[e.Node.Name], this.TipFont, 100, StringFormat.GenericTypographic);
                        e.Graphics.DrawString(this.LstTips[e.Node.Name], this.TipFont, new SolidBrush(Color.White), (float)(num3 + 10) - sizeF.Width / 2f - 3f, (float)(num4 + 10) - sizeF.Height / 2f);
                    }
                }
            }
            catch (Exception ex)
            {
                throw ex;
            }
        }
        private SizeF GetFontSize(Font font, Graphics g = null)
        {
            SizeF result;
            try
            {
                bool flag = false;
                if (g == null)
                {
                    g = base.CreateGraphics();
                    flag = true;
                }
                SizeF sizeF = g.MeasureString("a", font, 100, StringFormat.GenericTypographic);
                if (flag)
                {
                    g.Dispose();
                }
                result = sizeF;
            }
            catch (Exception ex)
            {
                throw ex;
            }
            return result;
        }
        [DllImport("user32", CharSet = CharSet.Auto)]
        private static extern int GetWindowLong(IntPtr hwnd, int nIndex);
        private bool IsVerticalScrollBarVisible()
        {
            return base.IsHandleCreated && (TreeViewEx.GetWindowLong(base.Handle, -16) & 2097152) != 0;
        }
        #endregion
 
        #region 鼠标移动事件
 
        private TreeNode mouseNode;
 
        protected override void OnMouseMove(MouseEventArgs e)
        {
            base.OnMouseMove(e);
            TreeNode node = this.GetNodeAt(e.X, e.Y);
            if (node != mouseNode)
            {
                if (mouseNode != null)
                    mouseNode.BackColor = Color.Transparent;
                node.BackColor = Color.FromArgb(51, 51, 52);
                mouseNode = node;
            }
        }
        protected override void OnMouseLeave(EventArgs e)
        {
            base.OnMouseLeave(e);
            if (mouseNode is null)
                return;
            mouseNode.BackColor = Color.Transparent;
            mouseNode = null;
        }
 
        #endregion
    }
}