📄 treelistviewitem.cs
字号:
using System;
using System.Drawing;
using System.Runtime.InteropServices;
using System.ComponentModel;
using System.Runtime.InteropServices.APIs;
using System.Diagnostics;
namespace System.Windows.Forms
{
#region TreeListViewItemBoundsPortion
/// <summary>
/// Specifies a portion of the tree list view item from which to retrieve the bounding rectangle
/// </summary>
[Serializable]
public enum TreeListViewItemBoundsPortion
{
/// <summary>
/// The bounding rectangle of the entire item, including the icon, the item text, and the subitem text (if displayed), should be retrieved
/// </summary>
Entire = (int)ItemBoundsPortion.Entire,
/// <summary>
/// The bounding rectangle of the icon or small icon should be retrieved
/// </summary>
Icon = (int)ItemBoundsPortion.Icon,
/// <summary>
/// The bounding rectangle specified by the Entire value without the subitems
/// </summary>
ItemOnly = (int)ItemBoundsPortion.ItemOnly,
/// <summary>
/// The bounding rectangle of the item text should be retrieved
/// </summary>
Label = (int)ItemBoundsPortion.Label,
/// <summary>
/// The bounding rectangle of the item plus minus
/// </summary>
PlusMinus = 4
}
#endregion
/// <summary>
/// Represents an item in a TreeListView control
/// </summary>
public class TreeListViewItem : ListViewItem
{
#region Private delegates
private delegate void ChangeChildrenCheckStateRecursivelyHandler(CheckState state);
private delegate TreeListViewItemCollection GetCollectionHandler();
private delegate string GetStringHandler();
private delegate bool GetBoolHandler();
private delegate int GetIntHandler();
private delegate TreeListViewItem GetTreeListViewItemHandler();
#endregion
#region Events
/// <summary>
/// TreeListViewItemHandler delegate
/// </summary>
public delegate void TreeListViewItemHanlder(object sender);
/// <summary>
/// TreeListViewItemCheckedHandler delegate
/// </summary>
public delegate void TreeListViewItemCheckedHandler(object sender, bool ischecked);
/// <summary>
/// Occurs after the tree node is collapsed
/// </summary>
public event TreeListViewItemHanlder AfterCollapse;
/// <summary>
/// Occurs after the tree node is expanded
/// </summary>
public event TreeListViewItemHanlder AfterExpand;
#endregion
#region Properties
#region NextVisibleItem
/// <summary>
/// Gets the next visible item in the TreeListView
/// </summary>
public TreeListViewItem NextVisibleItem
{
get
{
if(!IsInATreeListView || !Visible) return null;
ListView listview = (ListView) TreeListView;
if(Index >= listview.Items.Count-1) return null;
return (TreeListViewItem)listview.Items[Index+1];
}
}
#endregion
#region PrevVisibleItem
/// <summary>
/// Gets the previous visible item in the TreeListView
/// </summary>
public TreeListViewItem PrevVisibleItem
{
get
{
if(!IsInATreeListView || !Visible) return null;
ListView listview = (ListView) TreeListView;
if(Index < 1) return null;
return (TreeListViewItem)listview.Items[Index-1];
}
}
#endregion
#region Checked properties
/// <summary>
/// Gets or sets a value indicating whether the item is checked.
/// </summary>
public new bool Checked
{
get
{
try
{
return (base.Checked);
}
catch
{
return false;
}
}
set
{
if(IsInATreeListView)
if(TreeListView.InvokeRequired)
throw(new Exception("Invoke required"));
try
{
// Check downwards recursively
if(ListView != null &&
ListView._checkDirection == CheckDirection.Downwards &&
_items.Count > 0)
{
foreach(TreeListViewItem childItem in _items)
childItem.Checked = value;
}
if(base.Checked == value) return;
base.Checked = value;
}
catch{}
}
}
#endregion
#region CheckStatus
/// <summary>
/// Gets the check state of this item
/// </summary>
public CheckState CheckStatus
{
get
{
if(_items.Count <= 0)
{
if(this.Checked)
return CheckState.Checked;
else
return CheckState.Unchecked;
}
else
{
bool allChecked = true;
bool allUnChecked = true;
TreeListViewItem[] items = Items.ToArray();
foreach(TreeListViewItem item in items)
{
if (item.CheckStatus == CheckState.Indeterminate)
return CheckState.Indeterminate;
else if (item.CheckStatus == CheckState.Checked)
allUnChecked = false;
else
allChecked = false;
}
Debug.Assert(!(allChecked && allUnChecked));
if (allChecked)
return CheckState.Checked;
else if (allUnChecked)
return CheckState.Unchecked;
else
return CheckState.Indeterminate;
}
}
}
#endregion
#region ParentsInHierarch
/// <summary>
/// Gets a collection of the parent of this item
/// </summary>
[Browsable(false)]
public TreeListViewItem[] ParentsInHierarch
{
get
{
TreeListViewItemCollection items = GetParentsInHierarch();
return(items.ToArray());
}
}
private TreeListViewItemCollection GetParentsInHierarch()
{
TreeListViewItemCollection temp = Parent != null ?
Parent.GetParentsInHierarch() : new TreeListViewItemCollection();
if(Parent != null) temp.Add(Parent);
return temp;
}
#endregion
#region FullPath
/// <summary>
/// Gets the fullpath of an item (Parents.Text + \ + this.Text)
/// </summary>
[Browsable(false)]
public string FullPath
{
get
{
if(Parent != null)
{
string pathSeparator = IsInATreeListView ? TreeListView.PathSeparator : "\\";
string strPath = Parent.FullPath + pathSeparator + Text;
return(strPath.Replace(pathSeparator + pathSeparator, pathSeparator));
}
else
return(Text);
}
}
#endregion
#region Text
/// <summary>
/// Get or Set the Text property
/// </summary>
new public string Text
{
get
{
return(base.Text);
}
set
{
base.Text = value;
TreeListViewItemCollection collection = Container;
if(collection != null) collection.Sort(false);}
}
#endregion
#region Container
/// <summary>
/// Get the collection that contains this item
/// </summary>
public TreeListViewItemCollection Container
{
get
{
if(Parent != null) return(Parent.Items);
if(IsInATreeListView) return(TreeListView.Items);
return(null);
}
}
#endregion
#region IsInATreeListView
internal bool IsInATreeListView
{
get
{
return(TreeListView != null);
}
}
#endregion
#region LastChildIndexInListView
/// <summary>
/// Get the biggest index in the listview of the visible childs of this item
/// including this item
/// </summary>
[Browsable(false)]
public int LastChildIndexInListView
{
get
{
if(!IsInATreeListView)
throw(new Exception("No ListView control"));
int index = this.Index, temp;
foreach(TreeListViewItem item in Items)
if(item.Visible)
{
temp = item.LastChildIndexInListView;
if(temp > index) index = temp;
}
return(index);
}
}
#endregion
#region ChildrenCount
/// <summary>
/// Get the children count recursively
/// </summary>
[Browsable(false)]
public int ChildrenCount
{
get
{
TreeListViewItem[] items = _items.ToArray();
int count = items.Length;
foreach(TreeListViewItem item in items) count += item.ChildrenCount;
return(count);
}
}
#endregion
#region IsExpanded
private bool _isexpanded;
/// <summary>
/// Returns true if this item is expanded
/// </summary>
public bool IsExpanded
{
get
{
return(_isexpanded);
}
set
{
if(_isexpanded == value) return;
if(value) Expand();
else Collapse();
}
}
#endregion
#region Level
/// <summary>
/// Get the level of the item in the treelistview
/// </summary>
[Browsable(false)]
public int Level
{
get
{
return(Parent == null ? 0 : Parent.Level + 1);
}
}
#endregion
#region Items
private TreeListViewItemCollection _items;
/// <summary>
/// Get the items contained in this item
/// </summary>
public TreeListViewItemCollection Items
{
get
{
return(_items);
}
}
#endregion
#region Parent
private TreeListViewItem _parent;
/// <summary>
/// Get the parent of this item
/// </summary>
public TreeListViewItem Parent
{
get
{
return(_parent);
}
}
#endregion
#region TreeListView
/// <summary>
/// Gets the TreeListView containing this item
/// </summary>
public new TreeListView ListView
{
get
{
if(base.ListView != null) return((TreeListView) base.ListView);
if(Parent != null) return(Parent.ListView);
return(null);
}
}
/// <summary>
/// Gets the TreeListView containing this item
/// </summary>
public TreeListView TreeListView
{
get
{
return (TreeListView) ListView;
}
}
#endregion
#region Visible
/// <summary>
/// Returns true if this item is visible in the TreeListView
/// </summary>
public bool Visible
{
get
{
return(base.Index > -1);
}
}
#endregion
#endregion
#region Constructors
/// <summary>
/// Create a new instance of a TreeListViewItem
/// </summary>
public TreeListViewItem()
{
_items = new TreeListViewItemCollection(this);
}
/// <summary>
/// Create a new instance of a TreeListViewItem
/// </summary>
public TreeListViewItem(string value) : this()
{
this.Text = value;
}
/// <summary>
/// Create a new instance of a TreeListViewItem
/// </summary>
public TreeListViewItem(string value, int imageindex) : this(value)
{
this.ImageIndex = imageindex;
}
#endregion
#region Functions
internal void GetCheckedItems(ref TreeListViewItemCollection items)
{
if(Checked) items.Add(this);
foreach(TreeListViewItem item in Items)
item.GetCheckedItems(ref items);
}
/// <summary>
/// Places the subitem into edit mode
/// </summary>
/// <param name="column">Number of the subitem to edit</param>
public void BeginEdit(int column)
{
if(TreeListView == null)
throw(new Exception("The item is not associated with a TreeListView"));
if(!TreeListView.Visible)
throw(new Exception("The item is not visible"));
if(column + 1 > TreeListView.Columns.Count)
throw(new Exception("The column is greater the number of columns in the TreeListView"));
TreeListView.Focus();
Focused = true;
TreeListView._lastitemclicked = new EditItemInformations(this, column, this.SubItems[column].Text);
base.BeginEdit();
}
/// <summary>
/// Places the item into edit mode
/// </summary>
new public void BeginEdit()
{
BeginEdit(0);
}
/// <summary>
/// Asks the associated TreeListView control to redraw this item
/// </summary>
public void Redraw()
{
if(ListView == null || !Visible) return;
try
{
APIsUser32.SendMessage(
ListView.Handle,
(int)APIsEnums.ListViewMessages.REDRAWITEMS,
Index, Index);}
catch{}
}
/// <summary>
/// Retrieves the specified portion of the bounding rectangle for the item
/// </summary>
/// <param name="portion">One of the TreeListViewItemBoundsPortion values that represents a portion of the item for which to retrieve the bounding rectangle</param>
/// <returns>A Rectangle that represents the bounding rectangle for the specified portion of the item</returns>
public Rectangle GetBounds(TreeListViewItemBoundsPortion portion)
{
switch((int)portion)
{
case (int) TreeListViewItemBoundsPortion.PlusMinus:
if(TreeListView == null)
throw(new Exception("This item is not associated with a TreeListView control"));
Point pos = base.GetBounds(ItemBoundsPortion.Entire).Location;
Point position = new Point(
Level*SystemInformation.SmallIconSize.Width + 1 + pos.X,
TreeListView.GetItemRect(Index, ItemBoundsPortion.Entire).Top + 1);
return new Rectangle(position, TreeListView.ShowPlusMinus ? SystemInformation.SmallIconSize : new Size(0, 0));
default:
ItemBoundsPortion lviPortion = (ItemBoundsPortion)(int) portion;
return base.GetBounds(lviPortion);
}
}
internal void SetParent(TreeListViewItem parent)
{
_parent = parent;
}
/// <summary>
/// Remove this item from its associated collection
/// </summary>
public new void Remove()
{
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -