UCCrumbNavigation.cs 9.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228
  1. // ***********************************************************************
  2. // Assembly : HZH_Controls
  3. // Created : 08-19-2019
  4. //
  5. // ***********************************************************************
  6. // <copyright file="UCCrumbNavigation.cs">
  7. // Copyright by Huang Zhenghui(黄正辉) All, QQ group:568015492 QQ:623128629 Email:623128629@qq.com
  8. // </copyright>
  9. //
  10. // Blog: https://www.cnblogs.com/bfyx
  11. // GitHub:https://github.com/kwwwvagaa/NetWinformControl
  12. // gitee:https://gitee.com/kwwwvagaa/net_winform_custom_control.git
  13. //
  14. // If you use this code, please keep this note.
  15. // ***********************************************************************
  16. using System;
  17. using System.Collections.Generic;
  18. using System.ComponentModel;
  19. using System.Drawing;
  20. using System.Data;
  21. using System.Linq;
  22. using System.Text;
  23. using System.Windows.Forms;
  24. using System.Drawing.Drawing2D;
  25. namespace HZH_Controls.Controls
  26. {
  27. /// <summary>
  28. /// Class UCCrumbNavigation.
  29. /// Implements the <see cref="System.Windows.Forms.UserControl" />
  30. /// </summary>
  31. /// <seealso cref="System.Windows.Forms.UserControl" />
  32. [DefaultEvent("ClickItemed")]
  33. public partial class UCCrumbNavigation : UserControl
  34. {
  35. /// <summary>
  36. /// The m nav color
  37. /// </summary>
  38. private Color m_navColor = Color.FromArgb(255, 77, 59);
  39. /// <summary>
  40. /// Gets or sets the color of the nav.
  41. /// </summary>
  42. /// <value>The color of the nav.</value>
  43. public Color NavColor
  44. {
  45. get { return m_navColor; }
  46. set
  47. {
  48. if (value == Color.Empty || value == Color.Transparent)
  49. return;
  50. m_navColor = value;
  51. Refresh();
  52. }
  53. }
  54. /// <summary>
  55. /// The m paths
  56. /// </summary>
  57. GraphicsPath[] m_paths;
  58. private CrumbNavigationItem[] items;
  59. public CrumbNavigationItem[] Items
  60. {
  61. get { return items; }
  62. set
  63. {
  64. items = value;
  65. if (value == null)
  66. m_paths = new GraphicsPath[0];
  67. else
  68. m_paths = new GraphicsPath[value.Length];
  69. Refresh();
  70. }
  71. }
  72. /// <summary>
  73. /// 获取或设置控件显示的文字的字体。
  74. /// </summary>
  75. /// <value>The font.</value>
  76. /// <PermissionSet>
  77. /// <IPermission class="System.Security.Permissions.EnvironmentPermission, mscorlib, Version=2.0.3600.0, Culture=neutral, PublicKeyToken=b77a5c561934e089" version="1" Unrestricted="true" />
  78. /// <IPermission class="System.Security.Permissions.FileIOPermission, mscorlib, Version=2.0.3600.0, Culture=neutral, PublicKeyToken=b77a5c561934e089" version="1" Unrestricted="true" />
  79. /// <IPermission class="System.Security.Permissions.SecurityPermission, mscorlib, Version=2.0.3600.0, Culture=neutral, PublicKeyToken=b77a5c561934e089" version="1" Flags="UnmanagedCode, ControlEvidence" />
  80. /// <IPermission class="System.Diagnostics.PerformanceCounterPermission, System, Version=2.0.3600.0, Culture=neutral, PublicKeyToken=b77a5c561934e089" version="1" Unrestricted="true" />
  81. /// </PermissionSet>
  82. public override Font Font
  83. {
  84. get
  85. {
  86. return base.Font;
  87. }
  88. set
  89. {
  90. base.Font = value;
  91. Refresh();
  92. }
  93. }
  94. /// <summary>
  95. /// 获取或设置控件的前景色。
  96. /// </summary>
  97. /// <value>The color of the fore.</value>
  98. /// <PermissionSet>
  99. /// <IPermission class="System.Security.Permissions.FileIOPermission, mscorlib, Version=2.0.3600.0, Culture=neutral, PublicKeyToken=b77a5c561934e089" version="1" Unrestricted="true" />
  100. /// </PermissionSet>
  101. public override System.Drawing.Color ForeColor
  102. {
  103. get
  104. {
  105. return base.ForeColor;
  106. }
  107. set
  108. {
  109. base.ForeColor = value;
  110. Refresh();
  111. }
  112. }
  113. public delegate void CrumbNavigationEventHander(object sender, CrumbNavigationClickEventArgs e);
  114. public event CrumbNavigationEventHander ClickItemed;
  115. /// <summary>
  116. /// Initializes a new instance of the <see cref="UCCrumbNavigation" /> class.
  117. /// </summary>
  118. public UCCrumbNavigation()
  119. {
  120. InitializeComponent();
  121. this.SetStyle(ControlStyles.AllPaintingInWmPaint, true);
  122. this.SetStyle(ControlStyles.DoubleBuffer, true);
  123. this.SetStyle(ControlStyles.ResizeRedraw, true);
  124. this.SetStyle(ControlStyles.Selectable, true);
  125. this.SetStyle(ControlStyles.SupportsTransparentBackColor, true);
  126. this.SetStyle(ControlStyles.UserPaint, true);
  127. this.MouseDown += UCCrumbNavigation_MouseDown;
  128. Items = new CrumbNavigationItem[0];
  129. if (ControlHelper.IsDesignMode())
  130. {
  131. Items = new CrumbNavigationItem[3];
  132. for (int i = 0; i < 3; i++)
  133. {
  134. Items[i] = new CrumbNavigationItem() { Text = "item" + (i + 1), Key = i.ToString() };
  135. }
  136. }
  137. }
  138. /// <summary>
  139. /// Handles the MouseDown event of the UCCrumbNavigation control.
  140. /// </summary>
  141. /// <param name="sender">The source of the event.</param>
  142. /// <param name="e">The <see cref="MouseEventArgs" /> instance containing the event data.</param>
  143. void UCCrumbNavigation_MouseDown(object sender, MouseEventArgs e)
  144. {
  145. if (ClickItemed != null)
  146. {
  147. if (!DesignMode)
  148. {
  149. if (m_paths != null && m_paths.Length > 0)
  150. {
  151. for (int i = 0; i < m_paths.Length; i++)
  152. {
  153. if (m_paths[i].IsVisible(e.Location))
  154. {
  155. ClickItemed(this, new CrumbNavigationClickEventArgs() { Index = i, Item = items[i] });
  156. }
  157. }
  158. }
  159. }
  160. }
  161. }
  162. /// <summary>
  163. /// Handles the <see cref="E:Paint" /> event.
  164. /// </summary>
  165. /// <param name="e">The <see cref="PaintEventArgs" /> instance containing the event data.</param>
  166. protected override void OnPaint(PaintEventArgs e)
  167. {
  168. base.OnPaint(e);
  169. if (items != null && items.Length > 0)
  170. {
  171. var g = e.Graphics;
  172. g.SetGDIHigh();
  173. int intLastX = 0;
  174. int intLength = items.Length;
  175. for (int i = 0; i < items.Length; i++)
  176. {
  177. GraphicsPath path = new GraphicsPath();
  178. string strText = items[i].Text;
  179. System.Drawing.SizeF sizeF = g.MeasureString(strText.Replace(" ", "A"), Font);
  180. int intTextWidth = (int)sizeF.Width + 1;
  181. path.AddLine(new Point(intLastX + 1, 1), new Point(intLastX + 1 + (i == 0 ? 0 : 10) + intTextWidth, 1));
  182. //if (i != (intLength - 1))
  183. //{
  184. path.AddLine(new Point(intLastX + 1 + (i == 0 ? 0 : 10) + intTextWidth, 1), new Point(intLastX + 1 + (i == 0 ? 0 : 10) + intTextWidth + 10, this.Height / 2));
  185. path.AddLine(new Point(intLastX + 1 + (i == 0 ? 0 : 10) + intTextWidth + 10, this.Height / 2), new Point(intLastX + 1 + (i == 0 ? 0 : 10) + intTextWidth - 1, this.Height - 1));
  186. //}
  187. //else
  188. //{
  189. // path.AddLine(new Point(intLastX + 1 + (i == 0 ? 0 : 10) + intTextWidth, 1), new Point(intLastX + 1 + (i == 0 ? 0 : 10) + intTextWidth, this.Height - 1));
  190. //}
  191. path.AddLine(new Point(intLastX + 1 + (i == 0 ? 0 : 10) + intTextWidth, this.Height - 1), new Point(intLastX + 1, this.Height - 1));
  192. if (i != 0)
  193. {
  194. path.AddLine(new Point(intLastX, this.Height - 1), new Point(intLastX + 1 + 10, this.Height / 2));
  195. path.AddLine(new Point(intLastX + 1 + 10, this.Height / 2), new Point(intLastX + 1, 1));
  196. }
  197. else
  198. {
  199. path.AddLine(new Point(intLastX + 1, this.Height - 1), new Point(intLastX + 1, 1));
  200. }
  201. g.FillPath(new SolidBrush((items[i].ItemColor == null || items[i].ItemColor == Color.Empty || items[i].ItemColor == Color.Transparent) ? m_navColor : items[i].ItemColor.Value), path);
  202. g.DrawString(strText, this.Font, new SolidBrush(this.ForeColor), new PointF(intLastX + 2 + (i == 0 ? 0 : 10), (this.Height - sizeF.Height) / 2 + 1));
  203. m_paths[i] = path;
  204. intLastX += ((i == 0 ? 0 : 10) + intTextWidth + (i == (intLength - 1) ? 0 : 10));
  205. }
  206. }
  207. }
  208. }
  209. }