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

📄 treelistview.cs

📁 树形列表控件
💻 CS
📖 第 1 页 / 共 5 页
字号:
				EndUpdate();
			}
			#endregion
			#region ExitEdit
			internal void ExitEdit(bool Cancel, string Text)
			{
				if(!InEdit || EditedItem.Item == null) return;
				// Mouse position
				Point pos = EditedItem.Item != null ?
					EditedItem.Item.GetBounds(TreeListViewItemBoundsPortion.Icon).Location :
					new Point(0, 0);
				pos.Offset(1,1);
				EditItemInformations editedItem = EditedItem;

				Message m = Message.Create(Handle, (int) APIsEnums.WindowMessages.LBUTTONDOWN, (IntPtr)1, (IntPtr) ((pos.Y << 16) + pos.X));
				_skipMouseDownEvent = true;
				base.WndProc(ref m);
				_skipMouseDownEvent = false;
				if(!Cancel)
				{
					TreeListViewLabelEditEventArgs e = new TreeListViewLabelEditEventArgs(EditedItem.Item, EditedItem.ColumnIndex, Text);
					OnAfterLabelEdit(e);
					if(!e.Cancel)
						editedItem.Item.SubItems[
							editedItem.ColumnIndex].Text = Text;
				}
				_inedit = false;
				_editeditem = new EditItemInformations(null, 0, "");
			}
			#endregion
			#region GetItemRect
			/// <summary>
			/// Retrieves the specified portion of the bounding rectangle for a specific item within the list view control
			/// </summary>
			/// <param name="index">The zero-based index of the item within the ListView.ListViewItemCollection whose bounding rectangle you want to return</param>
			/// <param name="portion">One of the TreeListViewItemBoundsPortion values that represents a portion of the TreeListViewItem for which to retrieve the bounding rectangle</param>
			/// <returns>A Rectangle that represents the bounding rectangle for the specified portion of the specified TreeListViewItem</returns>
			public Rectangle GetItemRect(int index, TreeListViewItemBoundsPortion portion)
			{
				if(index >= base.Items.Count || index < 0)
					throw(new Exception("Out of range exception"));
				TreeListViewItem item = (TreeListViewItem) base.Items[index];
				return item.GetBounds(portion);
			}
			#endregion
			#region KillFocus
			/// <summary>
			/// Kill the focus of the control
			/// </summary>
			public void KillFocus()
			{
				APIsUser32.SendMessage(
					Handle,
					(int) APIsEnums.WindowMessages.KILLFOCUS,
					IntPtr.Zero,
					IntPtr.Zero);
			}
			#endregion
			#region OnItemCheck
			/// <summary>
			/// Raises the ItemCheck event
			/// </summary>
			/// <param name="e">An ItemCheckEventArgs that contains the event data</param>
			protected override void OnItemCheck(System.Windows.Forms.ItemCheckEventArgs e)
			{
				base.OnItemCheck(e);
				ListView.ListViewItemCollection baseItems = base.Items;
				if(e.Index >= base.Items.Count || e.Index < 0)
					return;
				TreeListViewItem item = (TreeListViewItem) base.Items[e.Index];
				if(item == null) return; 
				if(this._checkDirection == CheckDirection.None) return; 
				CheckDirection oldDirection = _checkDirection; 

				TreeListViewItem parentItem = item.Parent;
				if(parentItem != null && (oldDirection & CheckDirection.Upwards) == CheckDirection.Upwards)
				{
					_checkDirection = CheckDirection.Upwards;
					while(parentItem != null)
					{
						if(e.NewValue == CheckState.Checked)
						{
							if(!parentItem.Checked)
							{
								parentItem.Checked = true;
								break;
							}
							else
							{
								bool allChecked = true;
								foreach(TreeListViewItem childItem in parentItem.Items)
								{
									if(childItem == item) continue;
									if(!childItem.Checked)
									{
										allChecked = false;
										break;
									}
								} 
								if(allChecked) parentItem.Redraw();
							}
						}
						else
						{
							bool allUnChecked = true;
							foreach(TreeListViewItem childItem in parentItem.Items)
							{
								if(childItem == item) continue;
								if(childItem.Checked)
								{
									allUnChecked = false;
									break;
								}
							} 
							if(allUnChecked && parentItem.Checked)
							{
								parentItem.Checked = false;
								break;
							}
						}
						parentItem = parentItem.Parent;
					}
				}

				if((oldDirection & CheckDirection.Downwards) == CheckDirection.Downwards)
				{
					_checkDirection = CheckDirection.Downwards;
					foreach(TreeListViewItem childItem in item.Items)
						childItem.Checked = e.NewValue == CheckState.Checked;
				}
				_checkDirection = oldDirection;
			}
			#endregion
			#region OnColumnClick
			/// <summary>
			/// Raises the ColumnClick event
			/// </summary>
			/// <param name="e">A ColumnClickEventArgs that contains the event data</param>
			protected override void OnColumnClick(System.Windows.Forms.ColumnClickEventArgs e) 
			{ 
				base.OnColumnClick(e);
				Cursor = Cursors.WaitCursor;
				ListViewItem[] selItems = new ListViewItem[base.SelectedItems.Count];
				base.SelectedItems.CopyTo(selItems, 0);

				// Must set ListView.checkDirection to CheckDirection.None. 
				// Forbid recursively checking. 
				CheckDirection oldDirection = _checkDirection;
				_checkDirection = CheckDirection.None;
				BeginUpdate();
				if(Comparer.Column == e.Column)
					Sorting = (Sorting == SortOrder.Ascending ? SortOrder.Descending : SortOrder.Ascending);
				else
				{
					Comparer.Column = e.Column;
					Items.SortOrderRecursivelyWithoutSort = SortOrder.Ascending;
					try{Items.Sort(true);}
					catch{}
				}

				if(FocusedItem != null) FocusedItem.EnsureVisible();
				foreach(ListViewItem item in selItems)
					if(item.Index > -1) item.Selected = true;
				EndUpdate();
				// Reset ListView.checkDirection
				_checkDirection = oldDirection;
				Cursor = Cursors.Default;
			}
			#endregion
			#region OnVisibleChanged
			/// <summary>
			/// Raises the VisibleChanged event
			/// </summary>
			/// <param name="e"></param>
			protected override void OnVisibleChanged(EventArgs e)
			{
				base.OnVisibleChanged(e);
				if(base.SmallImageList != _smallimaglist)
					base.SmallImageList = _smallimaglist;
				VisChanged();
			}
			internal void VisChanged()
			{
				if(!Visible) return;
				BeginUpdate();
				try
				{
					foreach(TreeListViewItem item in this.Items)
						item.RefreshIndentation(true);
				}
				catch{}
				if(FocusedItem != null) FocusedItem.EnsureVisible();
				EndUpdate();
			}
			#endregion
			#region GetItemAt
			/// <summary>
			/// Gets an item at the specified coordinates
			/// </summary>
			/// <param name="p">Mouse position</param>
			/// <returns></returns>
			public TreeListViewItem GetItemAt(Point p)
			{
				return(GetItemAt(p.X, p.Y));
			}
			/// <summary>
			/// Gets an item at the specified coordinates (fullrow)
			/// </summary>
			/// <param name="p">Mouse position</param>
			/// <returns></returns>
			public TreeListViewItem GetItemAtFullRow(Point p)
			{
				if(FullRowSelect) return(GetItemAt(p));
				TreeListViewItemCollection items = GetVisibleItems();
				foreach(TreeListViewItem item in items)
					if(item.GetBounds(TreeListViewItemBoundsPortion.Entire).Contains(p))
						return item;
				return null;
			}
			/// <summary>
			/// Gets an item at the specified coordinates.
			/// </summary>
			/// <param name="x"></param>
			/// <param name="y"></param>
			/// <returns></returns>
			new public TreeListViewItem GetItemAt(int x, int y)
			{
				return (TreeListViewItem) base.GetItemAt(x, y);
			}
			#endregion
			#region GetTreeListViewItemFromIndex
			/// <summary>
			/// Gets the TreeListViewItem from the ListView index of the item
			/// </summary>
			/// <param name="index">Index of the Item</param>
			/// <returns></returns>
			public TreeListViewItem GetTreeListViewItemFromIndex(int index)
			{
				if(base.Items.Count < index + 1) return(null);
				return((TreeListViewItem) base.Items[index]);
			}
			#endregion
			#region Sort
			/// <summary>
			/// Not supported (use items.Sort)
			/// </summary>
			new public void Sort()
			{
				if(InvokeRequired)
					throw(new Exception("Invoke required"));
				Items.Sort(true);
			}
			#endregion
			#region Dispose
			/// <summary>
			/// Nettoyage des ressources utilis閑s.
			/// </summary>
			protected override void Dispose( bool disposing )
			{
				if(disposing)
					if( components != null )
						components.Dispose();
				base.Dispose( disposing );
			}
			#endregion
			#region BeginUpdate / EndUpdate
			/// <summary>
			/// Prevents the control from drawing until the EndUpdate method is called
			/// </summary>
			new public void BeginUpdate()
			{
				_updating = true;
				base.BeginUpdate();
			}
			/// <summary>
			/// Resumes drawing of the list view control after drawing is suspended by the BeginUpdate method
			/// </summary>
			new public void EndUpdate()
			{
				_updating = false;
				base.EndUpdate();
			}
			#endregion
		#endregion
		#region Component Designer generated code
		private void InitializeComponent()
		{
			this.components = new System.ComponentModel.Container();
			System.Resources.ResourceManager resources = new System.Resources.ResourceManager(typeof(TreeListView));
			this.imageList1 = new System.Windows.Forms.ImageList(this.components);
			this.plusMinusImageList = new System.Windows.Forms.ImageList(this.components);
			// 
			// imageList1
			// 
			this.imageList1.ColorDepth = System.Windows.Forms.ColorDepth.Depth32Bit;
			this.imageList1.ImageSize = new System.Drawing.Size(16, 16);
			this.imageList1.TransparentColor = System.Drawing.Color.Transparent;
			// 
			// plusMinusImageList
			// 
			this.plusMinusImageList.ColorDepth = System.Windows.Forms.ColorDepth.Depth32Bit;
			this.plusMinusImageList.ImageSize = new System.Drawing.Size(16, 16);
			this.plusMinusImageList.ImageStream = ((System.Windows.Forms.ImageListStreamer)(resources.GetObject("plusMinusImageList.ImageStream")));
			this.plusMinusImageList.TransparentColor = System.Drawing.Color.Transparent;
			// 
			// TreeListView
			// 
			this.FullRowSelect = true;
			this.View = System.Windows.Forms.View.Details;

		}
		#endregion

		#region Column Order
		/// <summary>
		/// Get the index of the specified column from its physical position
		/// </summary>
		/// <param name="columnorder"></param>
		/// <returns></returns>
		public int GetColumnIndex(int columnorder)
		{
			if(columnorder < 0 || columnorder > Columns.Count - 1) return(-1);
			return APIsUser32.SendMessage(Handle, (int)APIsEnums.HeaderControlMessages.ORDERTOINDEX, columnorder, 0);
		}
		/// <summary>
		/// Gets the order of a specified column
		/// </summary>
		/// <param name="columnindex"></param>
		/// <returns></returns>
		public int GetColumnOrder(int columnindex)
		{
			if(this.Columns.Count == 0) return(-1);
			if(columnindex < 0 || columnindex > this.Columns.Count - 1) return(-1);
			IntPtr[] colorderarray = new IntPtr[this.Columns.Count];
			APIsUser32.SendMessage(this.Handle, (int) APIsEnums.ListViewMessages.GETCOLUMNORDERARRAY, (IntPtr) this.Columns.Count, ref colorderarray[0]);
			return((int) colorderarray[columnindex]);
		}
		/// <summary>
		/// Gets the columns order
		/// </summary>
		/// <returns>Example {3,1,4,2}</returns>
		public int[] GetColumnsOrder()
		{
			if(this.Columns.Count == 0) return(new int[] {});
			IntPtr[] colorderarray = new IntPtr[this.Columns.Count];
			try
			{
				APIsUser32.SendMessage(this.Handle, (int) APIsEnums.ListViewMessages.GETCOLUMNORDERARRAY, (IntPtr) this.Columns.Count, ref colorderarray[0]);
			}
			catch{}
			int[] colorderarrayint = new int[this.Columns.Count];
			for(int i = 0 ; i < this.Columns.Count ; i ++)
				colorderarrayint[i] = (int) colorderarray[i];
			return(colorderarrayint);
		}
		/// <summary>
		/// Indicates the column order (for example : {0,1,3,2})
		/// </summary>
		/// <param name="colorderarray"></param>
		public void SetColumnsOrder(int[] colorderarray)
		{
			if(this.Columns.Count == 0) return;
			if(colorderarray.Length != this.Columns.Count) return;
			if(colorderarray[0] != 0) return;
			IntPtr[] colorderarrayintptr = new IntPtr[this.Columns.Count];
			for(int i = 0 ; i < this.Columns.Count ; i ++)
				colorderarrayintptr[i] = (IntPtr) colorderarray[i];
			try
			{
				APIsUser32.SendMessage(this.Handle, (int) APIsEnums.ListViewMessages.SETCOLUMNORDERARRAY, (IntPtr) this.Columns.Count, ref colorderarrayintptr[0]);
			}
			catch{}
			Refresh();
		}

		private void _scroll()
		{
			while(MouseButtons == MouseButtons.Middle)
			{
				int dx = MousePosition.Y - _mousescrollposition.Y;
				int dy = MousePosition.Y - _mousescrollposition.Y;
				Scroll(
					dx,
					dy);
				Threading.Thread.Sleep(100);
			}
			Cursor = Cursors.Default;
		}
		
		/// <summary>
		/// Scrolls the control
		/// </summary>
		/// <param name="x"></param>
		/// <param name="y"></param>
		public void Scroll(int x, int y)
		{
			APIsUser32.SendMessage(Handle, (int) APIsEnums.ListViewMessages.SCROLL, x, y);
		}
		/// <summary>

⌨️ 快捷键说明

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