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

📄 treelistview.cs

📁 树形列表控件
💻 CS
📖 第 1 页 / 共 5 页
字号:
		/// Indicates the column order (for example : "3142")
		/// </summary>
		/// <param name="colorder"></param>
		public void SetColumnsOrder(string colorder)
		{
			if(colorder == null) return;
			int[] colorderarray = new int[colorder.Length];
			for(int i = 0 ; i < colorder.Length ; i++)
				colorderarray[i] = int.Parse(new String(colorder[i], 1));
			SetColumnsOrder(colorderarray);
		}
		#endregion

		#region Item Region
		/// <summary>
		/// Gets the items that are visible in the TreeListView
		/// </summary>
		/// <returns>A collection of items</returns>
		public TreeListViewItemCollection GetVisibleItems()
		{
			TreeListViewItemCollection visibleItems = new TreeListViewItemCollection();
			if(base.Items.Count == 0) return visibleItems;
			int firstItemIndex = TopItem.Index;
			int itemsPerPageCount =
				APIsUser32.SendMessage(Handle, (int) APIsEnums.ListViewMessages.GETITEMCOUNT, IntPtr.Zero, IntPtr.Zero);
			int lastVisibleItemIndex = firstItemIndex + itemsPerPageCount > base.Items.Count ?
				base.Items.Count : firstItemIndex + itemsPerPageCount;
			for(int i = firstItemIndex; i < lastVisibleItemIndex; i++)
				visibleItems.Add((TreeListViewItem) base.Items[i]);
			return visibleItems;
		}
		/// <summary>
		/// Gets the column at the specified position
		/// </summary>
		/// <param name="p">Point in client coordinates</param>
		/// <returns>The nul zero based index of the column (-1 if failed)</returns>
		public int GetColumnAt(Point p)
		{
			APIsStructs.LVHITTESTINFO hittest = new APIsStructs.LVHITTESTINFO();
			hittest.pt = new APIsStructs.POINTAPI(PointToClient(MousePosition));
			APIsUser32.SendMessage(
				Handle,
				(Int32) APIsEnums.ListViewMessages.SUBITEMHITTEST,
				0,
				ref hittest);
			return(hittest.iSubItem);
		}
		/// <summary>
		/// Get SubItem rectangle
		/// </summary>
		/// <param name="item"></param>
		/// <param name="column"></param>
		/// <returns></returns>
		public Rectangle GetSubItemRect(TreeListViewItem item, int column)
		{
			ListViewItem lvitem = (ListViewItem) item;
			return GetSubItemRect(lvitem.Index, column);
		}
		/// <summary>
		/// Get SubItem rectangle
		/// </summary>
		/// <param name="row"></param>
		/// <param name="col"></param>
		/// <returns></returns>
		public Rectangle GetSubItemRect(int row, int col)
		{
			APIsStructs.RECT rc = new APIsStructs.RECT();
			rc.top = col;
			rc.left = (int)APIsEnums.ListViewSubItemPortion.BOUNDS;
			APIsUser32.SendMessage(Handle, (int)APIsEnums.ListViewMessages.GETSUBITEMRECT,  row, ref rc);
			
			if ( col == 0 )
			{
				// The LVM_GETSUBITEMRECT message does not give us the rectangle for the first subitem
				// since it is not considered a subitem
				// obtain the rectangle for the header control and calculate from there
				Rectangle headerRect = GetHeaderItemRect(col);
				return new Rectangle((int)rc.left, (int)rc.top, (int)headerRect.Width, (int)(rc.bottom-rc.top));
			}
			
			return new Rectangle((int)rc.left, (int)rc.top, (int)(rc.right-rc.left), (int)(rc.bottom-rc.top));
		}
		/// <summary>
		/// Get HeaderItem text
		/// </summary>
		/// <param name="index"></param>
		/// <returns></returns>
		public string GetHeaderItemText(int index)
		{
			APIsStructs.HDITEM hdi = new APIsStructs.HDITEM();
			hdi.mask = APIsEnums.HeaderItemFlags.TEXT;
			hdi.cchTextMax =  255;
			hdi.pszText = Marshal.AllocHGlobal(255);
			APIsUser32.SendMessage(Handle, APIsEnums.HeaderControlMessages.GETITEMW, index, ref hdi);
			string text = Marshal.PtrToStringAuto(hdi.pszText);
			return text;
		}
		/// <summary>
		/// Get HeaderItem rect
		/// </summary>
		/// <param name="index"></param>
		/// <returns></returns>
		protected Rectangle GetHeaderItemRect(int index)
		{
			APIsStructs.RECT rc = new APIsStructs.RECT();
			IntPtr header = APIsUser32.GetDlgItem(Handle, 0);
			APIsUser32.SendMessage(header, (int)APIsEnums.HeaderControlMessages.GETITEMRECT, index, ref rc);
			return new Rectangle((int)rc.left, (int)rc.top, (int)(rc.right-rc.left), (int)(rc.bottom-rc.top));
		}
		/// <summary>
		/// Get row rect
		/// </summary>
		/// <param name="row"></param>
		/// <returns></returns>
		public Rectangle GetRowRect(int row)
		{
			APIsStructs.RECT rc = new APIsStructs.RECT();
			rc.top = 0;
			rc.left = (int)APIsEnums.ListViewSubItemPortion.BOUNDS;
			APIsUser32.SendMessage(Handle, (int)APIsEnums.ListViewMessages.GETSUBITEMRECT,  row, ref rc);
			return new Rectangle((int)rc.left, (int)rc.top, (int)(rc.right-rc.left), (int)(rc.bottom-rc.top));
		}
		#endregion
	}
	#region TreeListViewItemCheckDirection Enum
	/// <summary>
	/// Check boxes direction in the TreeListView
	/// </summary>
	[Flags]
	[Serializable]
	internal enum CheckDirection
	{
		/// <summary>
		/// Simply check the item
		/// </summary>
		None = 0,
		/// <summary>
		/// Set the indeterminate state to the parent items
		/// </summary>
		Upwards = 1,
		/// <summary>
		/// Check children items recursively
		/// </summary>
		Downwards = 2,
		/// <summary>
		/// Upwards + Downwards
		/// </summary>
		All = 3,
	}
	#endregion 
	#region EditItemInformations
	/// <summary>
	/// Class that contains all informations on an edited item
	/// </summary>
	public struct EditItemInformations
	{
		#region Properties
		internal DateTime CreationTime;
		private string _label;
		/// <summary>
		/// Gets the label of the subitem
		/// </summary>
		public string Label
		{
			get{return _label;}
		}
		private TreeListViewItem _item;
		/// <summary>
		/// Gets the item being edited
		/// </summary>
		public TreeListViewItem Item
		{
			get{return _item;}
		}
		private int _colindex;
		/// <summary>
		/// Gets the number of the subitem
		/// </summary>
		public int ColumnIndex
		{
			get{return _colindex;}
		}
		#endregion
		#region Constructor
		/// <summary>
		/// Creates a new instance of EditItemInformations
		/// </summary>
		/// <param name="item"></param>
		/// <param name="column"></param>
		/// <param name="label"></param>
		public EditItemInformations(TreeListViewItem item, int column, string label)
		{
			_item = item; _colindex = column; _label = label; CreationTime = DateTime.Now;
		}
		#endregion
	}
	#endregion

	#region Event Handlers
	/// <summary>
	/// TreeListViewBeforeLabelEditEventHandler delegate
	/// </summary>
	public delegate void TreeListViewBeforeLabelEditEventHandler(object sender, TreeListViewBeforeLabelEditEventArgs e);
	/// <summary>
	/// TreeListViewItemLabelEditHandler delegate
	/// </summary>
	public delegate void TreeListViewLabelEditEventHandler(object sender, TreeListViewLabelEditEventArgs e);
	/// <summary>
	/// TreeListViewCancelEventHandler delegate
	/// </summary>
	public delegate void TreeListViewCancelEventHandler(object sender, TreeListViewCancelEventArgs e);
	/// <summary>
	/// TreeListViewEventHandler delegate
	/// </summary>
	public delegate void TreeListViewEventHandler(object sender, TreeListViewEventArgs e);
	#endregion
	#region TreeListViewLabelEditEventArgs & TreeListViewBeforeLabelEditEventArgs
	/// <summary>
	/// Arguments of a TreeListViewLabelEdit event.
	/// </summary>
	[Serializable]
	public class TreeListViewLabelEditEventArgs : CancelEventArgs
	{
		#region Properties
		private string _label;
		/// <summary>
		/// Gets the label of the subitem
		/// </summary>
		public string Label
		{
			get{return _label;}
		}
		private TreeListViewItem _item;
		/// <summary>
		/// Gets the item being edited
		/// </summary>
		public TreeListViewItem Item
		{
			get{return _item;}
		}
		internal int _colindex;
		/// <summary>
		/// Gets the number of the subitem
		/// </summary>
		public int ColumnIndex
		{
			get{return _colindex;}
		}
		#endregion
		#region Constructor
		/// <summary>
		/// Creates a new instance of TreeListViewLabelEditEventArgs
		/// </summary>
		/// <param name="item"></param>
		/// <param name="column"></param>
		/// <param name="label"></param>
		public TreeListViewLabelEditEventArgs(TreeListViewItem item, int column, string label) : base()
		{
			_item = item; _colindex = column; _label = label;
		}
		#endregion
	}
	/// <summary>
	/// Arguments of a TreeListViewBeforeLabelEdit event.
	/// </summary>
	[Serializable]
	public class TreeListViewBeforeLabelEditEventArgs : TreeListViewLabelEditEventArgs
	{
		#region Properties
		/// <summary>
		/// Gets or sets the index of the subitem
		/// </summary>
		new public int ColumnIndex
		{
			get{return _colindex;}
			set{_colindex = value;}
		}
		private Control _editor;
		/// <summary>
		/// Gets or sets the editor (a TextBox will be displayed if null)
		/// </summary>
		public Control Editor
		{
			get{return _editor;}
			set{_editor = value;}
		}
		#endregion
		#region Constructor
		/// <summary>
		/// Creates a new instance of TreeListViewBeforeLabelEditEventArgs
		/// </summary>
		/// <param name="item"></param>
		/// <param name="column"></param>
		/// <param name="label"></param>
		public TreeListViewBeforeLabelEditEventArgs(TreeListViewItem item, int column, string label) : base(item, column, label)
		{}
		#endregion
	}
	#endregion
	#region TreeListViewEventArgs
	/// <summary>
	/// Arguments of a TreeListViewEvent
	/// </summary>
	[Serializable]
	public class TreeListViewEventArgs : EventArgs
	{
		private TreeListViewItem _item;
		/// <summary>
		/// Item that will be expanded
		/// </summary>
		public TreeListViewItem Item{get{return(_item);}}
		private TreeListViewAction _action;
		/// <summary>
		/// Action returned by the event
		/// </summary>
		public TreeListViewAction Action{get{return(_action);}}
		/// <summary>
		/// Create a new instance of TreeListViewEvent arguments
		/// </summary>
		/// <param name="item"></param>
		/// <param name="action"></param>
		public TreeListViewEventArgs(TreeListViewItem item, TreeListViewAction action)
		{
			_item = item;
			_action = action;
		}
	}
	/// <summary>
	/// Arguments of a TreeListViewCancelEventArgs
	/// </summary>
	[Serializable]
	public class TreeListViewCancelEventArgs : TreeListViewEventArgs
	{
		private bool _cancel = false;
		/// <summary>
		/// True -> the operation is canceled
		/// </summary>
		public bool Cancel
		{
			get{return(_cancel);}
			set{_cancel = value;}
		}
		/// <summary>
		/// Create a new instance of TreeListViewCancelEvent arguments
		/// </summary>
		/// <param name="item"></param>
		/// <param name="action"></param>
		public TreeListViewCancelEventArgs(TreeListViewItem item, TreeListViewAction action) :
			base(item, action)
		{}
	}
	#endregion

	#region TreeListViewAction
	/// <summary>
	/// TreeListView actions
	/// </summary>
	[Serializable]
	public enum TreeListViewAction
	{
		/// <summary>
		/// By Keyboard
		/// </summary>
		ByKeyboard,
		/// <summary>
		/// ByMouse
		/// </summary>
		ByMouse,
		/// <summary>
		/// Collapse
		/// </summary>
		Collapse,
		/// <summary>
		/// Expand
		/// </summary>
		Expand,
		/// <summary>
		/// Unknown
		/// </summary>
		Unknown
	}
	#endregion
	#region TreeListViewExpandMethod
	/// <summary>
	/// Expand / Collapse method
	/// </summary>
	[Serializable]
	public enum TreeListViewExpandMethod
	{
		/// <summary>
		/// Expand when double clicking on the icon
		/// </summary>
		IconDbleClick,
		/// <summary>
		/// Expand when double clicking 

⌨️ 快捷键说明

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