TvwTreeView.cs 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160
  1. 
  2. using System.ComponentModel;
  3. using System.Drawing;
  4. using System.Drawing.Design;
  5. using System.Windows.Forms;
  6. namespace Dongke.WinForm.Controls
  7. {
  8. /// <summary>
  9. /// 显示标记项的分层集合,每个标记项用一个 TreeNode 来表示。
  10. /// </summary>
  11. [ToolboxBitmap(typeof(TreeView))]
  12. public partial class TvwTreeView : TreeView, IDKControl, IAsyncControl
  13. {
  14. #region 成员变量
  15. /// <summary>
  16. /// 展开节点的图像的图像列表索引值。
  17. /// </summary>
  18. private int _expandImageIndex = -1;
  19. private int _expandSelectedImageIndex = -1;
  20. #endregion
  21. #region 构造函数
  22. /// <summary>
  23. /// 显示标记项的分层集合,每个标记项用一个 TreeNode 来表示。
  24. /// </summary>
  25. public TvwTreeView()
  26. {
  27. }
  28. #endregion
  29. #region 属性
  30. ///// <summary>
  31. ///// 获取或设置树展开节点显示的默认图像的图像列表索引值。
  32. ///// </summary>
  33. //[DefaultValue(-1)]
  34. //[Editor("System.Windows.Forms.Design.ImageIndexEditor, System.Design, Version=2.0.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a", typeof(UITypeEditor))]
  35. //[Localizable(true)]
  36. //[RefreshProperties(RefreshProperties.Repaint)]
  37. //[TypeConverter(typeof(NoneExcludedImageIndexConverter))]
  38. //[RelatedImageList("ImageList")]
  39. //public int ExpandImageIndex
  40. //{
  41. // get
  42. // {
  43. // return this._expandImageIndex;
  44. // }
  45. // set
  46. // {
  47. // if (this._expandImageIndex != value)
  48. // {
  49. // this._expandImageIndex = value;
  50. // this.SetImageIndex();
  51. // }
  52. // }
  53. //}
  54. #endregion
  55. #region 重写事件
  56. /// <summary>
  57. /// 在选中树节点复选框后发生。
  58. /// </summary>
  59. /// <param name="e"></param>
  60. protected override void OnAfterCheck(TreeViewEventArgs e)
  61. {
  62. base.OnAfterCheck(e);
  63. //if (e.Node.Nodes.Count > 0)
  64. //{
  65. // e.Node
  66. //}
  67. }
  68. #endregion
  69. #region 私有方法
  70. /// <summary>
  71. /// 设置节点的图像的图像列表索引值
  72. /// </summary>
  73. private void SetImageIndex()
  74. {
  75. this.BeginUpdate();
  76. this.SetImageIndex(null);
  77. this.EndUpdate();
  78. }
  79. private void SetImageIndex(TreeNode node)
  80. {
  81. if (node != null)
  82. {
  83. if (node.IsExpanded)
  84. {
  85. node.ImageIndex = this._expandImageIndex;
  86. node.SelectedImageIndex = this._expandSelectedImageIndex;
  87. }
  88. else
  89. {
  90. node.ImageIndex = -1;
  91. node.SelectedImageIndex = -1;
  92. }
  93. foreach (TreeNode item in node.Nodes)
  94. {
  95. this.SetImageIndex(item);
  96. }
  97. }
  98. else
  99. {
  100. foreach (TreeNode item in this.Nodes)
  101. {
  102. this.SetImageIndex(item);
  103. }
  104. }
  105. }
  106. #endregion
  107. #region
  108. #endregion
  109. #region IAsyncControl 成员
  110. #region 成员变量
  111. /// <summary>
  112. /// 异步处理开始时,控件状态
  113. /// </summary>
  114. private bool _asyncBeginStatus = false;
  115. private bool _asyncBeginFocused = false;
  116. #endregion
  117. #region 公有方法
  118. /// <summary>
  119. /// 开始异步处理
  120. /// </summary>
  121. /// <param name="doFocus">是否处理焦点</param>
  122. public virtual void BeginAsync(ref bool doFocus)
  123. {
  124. if (doFocus && this.Focused)
  125. {
  126. this._asyncBeginFocused = true;
  127. doFocus = false;
  128. }
  129. this._asyncBeginStatus = this.Enabled;
  130. this.Enabled = false;
  131. }
  132. /// <summary>
  133. /// 结束异步处理
  134. /// </summary>
  135. public virtual void EndAsync()
  136. {
  137. this.Enabled = this._asyncBeginStatus;
  138. if (this._asyncBeginFocused)
  139. {
  140. this.Focus();
  141. }
  142. }
  143. #endregion
  144. #endregion
  145. }
  146. }