📄 treelistview.cs
字号:
using System;
using System.Collections;
using System.ComponentModel;
using System.Drawing;
using System.Data;
using System.Windows.Forms;
using System.Runtime.InteropServices;
using System.Runtime.InteropServices.APIs;
using System.Diagnostics;
namespace System.Windows.Forms
{
/// <summary>
/// TreeListView control (simulates a TreeView in a ListView)
/// </summary>
public class TreeListView : System.Windows.Forms.ListView
{
#region Private delegates
private delegate int IntHandler();
private delegate TreeListViewItem[] ItemArrayHandler();
#endregion
#region Events, Delegates, and internal calls
#region Events
/// <summary>
/// Occurs when the label for an item is edited by the user.
/// </summary>
[Description("Occurs when the label for an item is edited by the user.")]
public new event TreeListViewLabelEditEventHandler AfterLabelEdit;
/// <summary>
/// Occurs when the user starts editing the label of an item.
/// </summary>
[Description("Occurs when the user starts editing the label of an item."),Browsable(true)]
public new event TreeListViewBeforeLabelEditEventHandler BeforeLabelEdit;
/// <summary>
/// Occurs before the tree node is collapsed.
/// </summary>
[Description("Occurs before the tree node is collapsed")]
public event TreeListViewCancelEventHandler BeforeExpand;
/// <summary>
/// Occurs before the tree node is collapsed.
/// </summary>
[Description("Occurs before the tree node is collapsed")]
public event TreeListViewCancelEventHandler BeforeCollapse;
/// <summary>
/// Occurs after the tree node is expanded
/// </summary>
[Description("Occurs after the tree node is expanded")]
public event TreeListViewEventHandler AfterExpand;
/// <summary>
/// Occurs after the tree node is collapsed
/// </summary>
[Description("Occurs after the tree node is collapsed")]
public event TreeListViewEventHandler AfterCollapse;
#endregion
#region On???
/// <summary>
/// Raises the AfterLabelEdit event.
/// </summary>
/// <param name="e"></param>
protected virtual void OnAfterLabelEdit(TreeListViewLabelEditEventArgs e)
{
if(AfterLabelEdit != null) AfterLabelEdit(this, e);
}
/// <summary>
/// Please use OnAfterLabelEdit(TreeListViewLabelEditEventArgs e)
/// </summary>
/// <param name="e"></param>
protected override void OnAfterLabelEdit(LabelEditEventArgs e)
{
throw(new Exception("Please use OnAfterLabelEdit(TreeListViewLabelEditEventArgs e)"));
}
/// <summary>
/// Raises the BeforeLabelEdit event.
/// </summary>
/// <param name="e"></param>
protected virtual void OnBeforeLabelEdit(TreeListViewBeforeLabelEditEventArgs e)
{
if(BeforeLabelEdit != null) BeforeLabelEdit(this, e);
}
/// <summary>
/// Please use OnBeforeLabelEdit(TreeListViewLabelEditEventArgs e)
/// </summary>
/// <param name="e"></param>
protected override void OnBeforeLabelEdit(LabelEditEventArgs e)
{
throw(new Exception("Please use OnBeforeLabelEdit(TreeListViewLabelEditEventArgs e)"));
}
/// <summary>
/// Raises the BeforeExpand event.
/// </summary>
/// <param name="e"></param>
protected virtual void OnBeforeExpand(TreeListViewCancelEventArgs e)
{
if(BeforeExpand != null) BeforeExpand(this, e);
}
/// <summary>
/// Raises the AfterExpand event.
/// </summary>
/// <param name="e"></param>
protected virtual void OnAfterExpand(TreeListViewEventArgs e)
{
if(AfterExpand != null) AfterExpand(this, e);
}
/// <summary>
/// Raises the BeforeCollapse event.
/// </summary>
/// <param name="e"></param>
protected virtual void OnBeforeCollapse(TreeListViewCancelEventArgs e)
{
if(BeforeCollapse != null) BeforeCollapse(this, e);
}
/// <summary>
/// Raises the AfterCollapse event.
/// </summary>
/// <param name="e"></param>
protected virtual void OnAfterCollapse(TreeListViewEventArgs e)
{
if(AfterCollapse != null) AfterCollapse(this, e);
}
#endregion
#region Internal calls
internal void RaiseBeforeExpand(TreeListViewCancelEventArgs e)
{
OnBeforeExpand(e);
}
/// <summary>
/// Raises the MouseDown event
/// </summary>
/// <param name="e">A MouseEventArgs that contains the event data</param>
protected override void OnMouseDown(MouseEventArgs e)
{
if(!_skipMouseDownEvent)
base.OnMouseDown(e);
}
internal void RaiseBeforeCollapse(TreeListViewCancelEventArgs e)
{
OnBeforeCollapse(e);
}
internal void RaiseAfterExpand(TreeListViewEventArgs e)
{
OnAfterExpand(e);
}
internal void RaiseAfterCollapse(TreeListViewEventArgs e)
{
OnAfterCollapse(e);
}
#endregion
#endregion
#region Modified properties
#region Scrollable
private bool _scrollable = true;
/// <summary>
/// Gets or sets a value indicating whether a scroll bar is added to the control when there is not enough room to display all items
/// </summary>
[DesignerSerializationVisibility(DesignerSerializationVisibility.Visible), DefaultValue(true)]
[Browsable(true), Description("Gets or sets a value indicating whether a scroll bar is added to the control when there is not enough room to display all items")]
new public bool Scrollable
{
get
{
return _scrollable;
}
set
{
_scrollable = value;
}
}
#endregion
#region CheckBoxes
private CheckBoxesTypes _checkboxes = CheckBoxesTypes.None;
/// <summary>
/// Gets or sets a value indicating whether a check box appears next to each item in the control
/// </summary>
[DesignerSerializationVisibility(DesignerSerializationVisibility.Visible), DefaultValue(typeof(CheckBoxesTypes), "None")]
[Browsable(true), Description("Gets or sets a value indicating whether a check box appears next to each item in the control")]
new public CheckBoxesTypes CheckBoxes
{
get
{
return _checkboxes;
}
set
{
if(_checkboxes == value) return;
_checkboxes = value;
_checkDirection = value == CheckBoxesTypes.Recursive ? CheckDirection.All : CheckDirection.None;
base.CheckBoxes = value == CheckBoxesTypes.None ? false : true;
if(Created)
Invalidate();
}
}
#endregion
#region FullRowSelect
/// <summary>
/// Gets or sets a value indicating whether clicking an item selects all its subitems
/// </summary>
[Browsable(true), Description("Gets or sets a value indicating whether clicking an item selects all its subitems"),
DefaultValue(true)]
new public bool FullRowSelect
{
get
{
return base.FullRowSelect;
}
set
{
base.FullRowSelect = value;
}
}
#endregion
#region StateImageList
/// <summary>
/// Not supported
/// </summary>
[Browsable(false)]
new public ImageList StateImageList
{
get{return base.StateImageList;}
set{base.StateImageList = value;}
}
#endregion
#region LargeImageList
/// <summary>
/// Not supported
/// </summary>
[Browsable(false)]
new public ImageList LargeImageList
{
get{return base.LargeImageList;}
set{base.LargeImageList = value;}
}
#endregion
#region SmallImageList
private ImageList _smallimaglist = null;
/// <summary>
/// Gets or sets the ImageList to use when displaying items as small icons in the control (must be filled in)
/// </summary>
[DesignerSerializationVisibility(DesignerSerializationVisibility.Visible), DefaultValue(null)]
[Browsable(true), Description("Gets or sets the ImageList to use when displaying items as small icons in the control")]
new public ImageList SmallImageList
{
get
{
return _smallimaglist;
}
set
{
_smallimaglist = value;
}
}
#endregion
#region Sorting
private SortOrder _sorting = SortOrder.Ascending;
/// <summary>
/// Get or set the sort order
/// </summary>
[DesignerSerializationVisibility(DesignerSerializationVisibility.Visible)]
[Browsable(true), Description("Get or Set the sort order"), DefaultValue(typeof(SortOrder), "Ascending")]
new public SortOrder Sorting
{
get{return(_sorting);}
set{if(_sorting == value) return;
_sorting = value;
Items.SortOrderRecursively = value;}
}
#endregion
#region ExpandMethod
private TreeListViewExpandMethod _expandmethod = TreeListViewExpandMethod.EntireItemDbleClick;
/// <summary>
/// Get or set the expand method
/// </summary>
[Browsable(true), DefaultValue(typeof(TreeListViewExpandMethod), "EntireItemDbleClick"),
Description("Get or Set the expand method")]
public TreeListViewExpandMethod ExpandMethod
{
get{return(_expandmethod);}
set{_expandmethod = value;}
}
#endregion
#region View
/// <summary>
/// View (always Details)
/// </summary>
[DesignerSerializationVisibility(DesignerSerializationVisibility.Hidden)]
[Browsable(false)]
public new View View
{
get{return(base.View);}
set{base.View = View.Details;}
}
#endregion
#region Items
/// <summary>
/// Items of the TreeListView
/// </summary>
// [Browsable(true),
// Editor(typeof(TreeListViewItemsEditor), typeof(System.Drawing.Design.UITypeEditor))]
[Browsable(false)]
[Description("Items of the TreeListView")]
new public TreeListViewItemCollection Items
{
get{return(_items);}
}
#endregion
#region SelectedItems
/// <summary>
/// Get currently selected items
/// </summary>
[Browsable(false)]
new public SelectedTreeListViewItemCollection SelectedItems
{
get
{
SelectedTreeListViewItemCollection sel = new SelectedTreeListViewItemCollection(this);
return(sel);
}
}
#endregion
#region CheckedItems
/// <summary>
/// Get currently checked items
/// </summary>
[Browsable(false)]
public new TreeListViewItem[] CheckedItems
{
get
{
return (TreeListViewItem[]) Invoke(new ItemArrayHandler(GetCheckedItems));
}
}
private TreeListViewItem[] GetCheckedItems()
{
if(InvokeRequired)
throw(new Exception("Invoke required"));
TreeListViewItemCollection items = new TreeListViewItemCollection();
foreach(TreeListViewItem item in Items)
item.GetCheckedItems(ref items);
return(items.ToArray());
}
#endregion
#region FocusedItem
/// <summary>
/// Gets the item in the control that currently has focus.
/// </summary>
[Browsable(false)]
new public TreeListViewItem FocusedItem
{
get{return (TreeListViewItem) base.FocusedItem;}
}
#endregion
#endregion
#region Properties
#region Private Properties
internal TreeListViewItem _selectionMark = null;
internal bool _updating = false;
internal bool _skipMouseDownEvent = false;
internal CheckDirection _checkDirection = CheckDirection.None;
internal int _comctl32Version;
private DateTime _lastdoubleclick;
internal EditItemInformations _lastitemclicked;
private CustomEdit _customedit;
private TreeListViewItemCollection _items;
private System.ComponentModel.IContainer components;
private System.Windows.Forms.ImageList imageList1;
internal bool FreezeCheckBoxes = false;
private Point _mousescrollposition = new Point(0, 0);
private DateTime _dblclicktime = DateTime.Now;
internal System.Windows.Forms.ImageList plusMinusImageList;
#endregion
#region HasMarquee
private bool _hasMarquee = false;
/// <summary>
/// Gets whether the marquee selection tool is curently being used
/// </summary>
[Browsable(false)]
public bool HasMarquee
{
get
{
return _hasMarquee;
}
}
#endregion
#region EditedItem
internal EditItemInformations _editeditem = new EditItemInformations();
/// <summary>
/// Gets the informations of the current edited item
/// </summary>
[Browsable(false)]
public EditItemInformations EditedItem
{
get
{
return _editeditem;
}
}
#endregion
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -