⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 treeviewex.cs

📁 是一款式CSDN阅读器,可以方便CSDN用户阅读自己感兴趣的内容!
💻 CS
字号:
namespace feiyun0112.cnblogs.com.CSDNReader.Controls
{
	using System;
	using System.Drawing;
	using System.Runtime.CompilerServices;
	using System.Text;
	using System.Windows.Forms;
	using System.Runtime.InteropServices;

	public class TreeViewEx : TreeView
	{

		[StructLayout(LayoutKind.Sequential)]
			internal struct NMCUSTOMDRAW
		{
			public NMHDR hdr;
			public int dwDrawStage;
			public IntPtr hdc;
			public Rectangle rc;
			public int dwItemSpec;
			public int uItemState;
			public IntPtr lItemlParam;
		}

		
		[StructLayout(LayoutKind.Sequential)]
			internal struct NMHDR
		{
			public IntPtr hwndFrom;
			public int idfrom;
			public int code;
		}

		public delegate string GetNodeInfoDelegate(TreeNode node);
		public event GetNodeInfoDelegate GetNodeInfo;

		public  void  BeginUpdate()
		{
			base.BeginUpdate();
			this._updateCount++;
		}

		private void BuildState(TreeNode node, StringBuilder sb)
		{
			if (node.IsSelected)
			{
				sb.Append('S');
			}
			if (!node.IsExpanded)
			{
				sb.Append('-');
			}
			else
			{
				sb.Append('+');
				foreach (TreeNode node1 in node.Nodes)
				{
					this.BuildState(node1, sb);
				}
			}
		}

		public  void EndUpdate()
		{
			if (this._updateCount > 0)
			{
				this._updateCount--;
			}
			base.EndUpdate();
		}

		public string GetState()
		{
			StringBuilder builder1 = new StringBuilder();
			this.BuildState(base.Nodes[0], builder1);
			return builder1.ToString();
		}

		protected override void OnCreateControl()
		{
			base.OnCreateControl();
			Message message1 = Message.Create(base.Handle, CCM_SETVERSION, new IntPtr(5), IntPtr.Zero);
			this.DefWndProc(ref message1);
		}

		public void RestoreState(string state)
		{
			if (state != null)
			{
				int num1 = 0;
				this.RestoreState(state, ref num1, base.Nodes[0]);
				if (base.SelectedNode != null)
				{
					base.SelectedNode.EnsureVisible();
				}
			}
		}

		private void RestoreState(string state, ref int stateIndex, TreeNode node)
		{
			if (stateIndex < state.Length)
			{
				char ch1 = state[stateIndex++];
				if (ch1 == 'S')
				{
					base.SelectedNode = node;
					ch1 = state[stateIndex++];
				}
				if (ch1 == '-')
				{
					node.Collapse();
				}
				else if (ch1 == '+')
				{
					node.Expand();
					foreach (TreeNode node1 in node.Nodes)
					{
						this.RestoreState(state, ref stateIndex, node1);
					}
				}
			}
		}

		public void treeViewPaintNodeInfo(Rectangle rc)
		{
			if (rc != Rectangle.Empty)
			{
				TreeNode node1 = base.GetNodeAt(rc.Location.X + 1, rc.Location.Y + 1);
				if ((node1 != null) && !node1.IsEditing)
				{
					string text1 = this.GetNodeInfo(node1);
					if (text1 != null && text1!="")
					{
						long num1 = node1.Bounds.X + node1.Bounds.Width;
						long num2 = rc.X + num1;
						long num3 = rc.Y;
						Brush brush1 = new SolidBrush(this.BackColor);
						using (Graphics graphics1 = Graphics.FromHwnd(base.Handle))
						{
							graphics1.FillRectangle(brush1, (float) num2, (float) num3, (float) (base.Width - num1), (float) node1.Bounds.Height);
							graphics1.DrawString(" (" + text1 + ")", this.Font, Brushes.Blue, (float) num2, (float) num3, StringFormat.GenericTypographic);
						}
					}
				}
			}
		}

		protected override void WndProc(ref Message m)
		{
			if (((m.Msg == OCM_NOTIFY) && (this.GetNodeInfo != null)) && (this._updateCount == 0))
			{
				NMHDR nmhdr1 = (NMHDR) m.GetLParam(typeof(NMHDR));
				if ((nmhdr1.code == NM_CUSTOMDRAW) && (nmhdr1.hwndFrom == base.Handle))
				{
					NMCUSTOMDRAW nmcustomdraw1 = (NMCUSTOMDRAW) m.GetLParam(typeof(NMCUSTOMDRAW));
					if (nmcustomdraw1.dwDrawStage ==CDDS_ITEMPREPAINT)
					{
						this.treeViewPaintNodeInfo(nmcustomdraw1.rc);
						m.Result = IntPtr.Zero;
					}
					else
					{
						m.Result = (IntPtr) CDRF_NOTIFYSUBITEMDRAW;
					}
				}
			}
			base.WndProc(ref m);
		}


		private int _updateCount;
		private const int CCM_FIRST = 0x2000;
		private const int CCM_SETVERSION = 0x2007;
		private const int CDDS_ITEMPREPAINT = 0x10001;
		private const int CDRF_NOTIFYSUBITEMDRAW = 0x20;
		private const int NM_CUSTOMDRAW = -12;
		private const int NM_FIRST = 0;
		private const int OCM_NOTIFY = 0x204e;
	}
}

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -