UCNavigationMenuExt.cs 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294
  1. // ***********************************************************************
  2. // Assembly : HZH_Controls
  3. // Created : 2019-10-11
  4. //
  5. // ***********************************************************************
  6. // <copyright file="UCNavigationMenuExt.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 HZH_Controls.Forms;
  25. namespace HZH_Controls.Controls
  26. {
  27. /// <summary>
  28. /// Class UCNavigationMenuExt.
  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 UCNavigationMenuExt : UserControl
  34. {
  35. /// <summary>
  36. /// Occurs when [click itemed].
  37. /// </summary>
  38. [Description("点击节点事件"), Category("自定义")]
  39. public event EventHandler ClickItemed;
  40. /// <summary>
  41. /// The select item
  42. /// </summary>
  43. private NavigationMenuItemExt selectItem = null;
  44. /// <summary>
  45. /// Gets the select item.
  46. /// </summary>
  47. /// <value>The select item.</value>
  48. [Description("选中的节点"), Category("自定义")]
  49. public NavigationMenuItemExt SelectItem
  50. {
  51. get { return selectItem; }
  52. private set { selectItem = value; }
  53. }
  54. /// <summary>
  55. /// The items
  56. /// </summary>
  57. NavigationMenuItemExt[] items;
  58. /// <summary>
  59. /// Gets or sets the items.
  60. /// </summary>
  61. /// <value>The items.</value>
  62. [Description("节点列表"), Category("自定义")]
  63. public NavigationMenuItemExt[] Items
  64. {
  65. get { return items; }
  66. set
  67. {
  68. items = value;
  69. ReloadMenu();
  70. }
  71. }
  72. /// <summary>
  73. /// The tip color
  74. /// </summary>
  75. private Color tipColor = Color.FromArgb(255, 87, 34);
  76. /// <summary>
  77. /// Gets or sets the color of the tip.
  78. /// </summary>
  79. /// <value>The color of the tip.</value>
  80. [Description("角标颜色"), Category("自定义")]
  81. public Color TipColor
  82. {
  83. get { return tipColor; }
  84. set { tipColor = value; }
  85. }
  86. /// <summary>
  87. /// 获取或设置控件的前景色。
  88. /// </summary>
  89. /// <value>The color of the fore.</value>
  90. /// <PermissionSet>
  91. /// <IPermission class="System.Security.Permissions.FileIOPermission, mscorlib, Version=2.0.3600.0, Culture=neutral, PublicKeyToken=b77a5c561934e089" version="1" Unrestricted="true" />
  92. /// </PermissionSet>
  93. public override System.Drawing.Color ForeColor
  94. {
  95. get
  96. {
  97. return base.ForeColor;
  98. }
  99. set
  100. {
  101. base.ForeColor = value;
  102. foreach (Control c in this.Controls)
  103. {
  104. c.ForeColor = value;
  105. }
  106. }
  107. }
  108. /// <summary>
  109. /// 获取或设置控件显示的文字的字体。
  110. /// </summary>
  111. /// <value>The font.</value>
  112. /// <PermissionSet>
  113. /// <IPermission class="System.Security.Permissions.EnvironmentPermission, mscorlib, Version=2.0.3600.0, Culture=neutral, PublicKeyToken=b77a5c561934e089" version="1" Unrestricted="true" />
  114. /// <IPermission class="System.Security.Permissions.FileIOPermission, mscorlib, Version=2.0.3600.0, Culture=neutral, PublicKeyToken=b77a5c561934e089" version="1" Unrestricted="true" />
  115. /// <IPermission class="System.Security.Permissions.SecurityPermission, mscorlib, Version=2.0.3600.0, Culture=neutral, PublicKeyToken=b77a5c561934e089" version="1" Flags="UnmanagedCode, ControlEvidence" />
  116. /// <IPermission class="System.Diagnostics.PerformanceCounterPermission, System, Version=2.0.3600.0, Culture=neutral, PublicKeyToken=b77a5c561934e089" version="1" Unrestricted="true" />
  117. /// </PermissionSet>
  118. public override Font Font
  119. {
  120. get
  121. {
  122. return base.Font;
  123. }
  124. set
  125. {
  126. base.Font = value;
  127. foreach (Control c in this.Controls)
  128. {
  129. c.Font = value;
  130. }
  131. }
  132. }
  133. /// <summary>
  134. /// The m LST anchors
  135. /// </summary>
  136. Dictionary<NavigationMenuItemExt, FrmAnchor> m_lstAnchors = new Dictionary<NavigationMenuItemExt, FrmAnchor>();
  137. /// <summary>
  138. /// Initializes a new instance of the <see cref="UCNavigationMenuExt"/> class.
  139. /// </summary>
  140. public UCNavigationMenuExt()
  141. {
  142. InitializeComponent();
  143. items = new NavigationMenuItemExt[0];
  144. if (ControlHelper.IsDesignMode())
  145. {
  146. items = new NavigationMenuItemExt[4];
  147. for (int i = 0; i < 4; i++)
  148. {
  149. items[i] = new NavigationMenuItemExt()
  150. {
  151. Text = "菜单" + (i + 1),
  152. AnchorRight = i >= 2
  153. };
  154. }
  155. }
  156. }
  157. /// <summary>
  158. /// Reloads the menu.
  159. /// </summary>
  160. private void ReloadMenu()
  161. {
  162. try
  163. {
  164. ControlHelper.FreezeControl(this, true);
  165. this.Controls.Clear();
  166. if (items != null && items.Length > 0)
  167. {
  168. foreach (var item in items)
  169. {
  170. var menu = (NavigationMenuItemExt)item;
  171. Label lbl = new Label();
  172. lbl.AutoSize = false;
  173. lbl.TextAlign = ContentAlignment.MiddleCenter;
  174. lbl.Width = menu.ItemWidth;
  175. lbl.Text = menu.Text;
  176. lbl.Font = Font;
  177. lbl.ForeColor = ForeColor;
  178. lbl.Paint += lbl_Paint;
  179. lbl.MouseEnter += lbl_MouseEnter;
  180. lbl.Tag = menu;
  181. lbl.Click += lbl_Click;
  182. if (menu.AnchorRight)
  183. {
  184. lbl.Dock = DockStyle.Right;
  185. }
  186. else
  187. {
  188. lbl.Dock = DockStyle.Left;
  189. }
  190. this.Controls.Add(lbl);
  191. lbl.BringToFront();
  192. }
  193. }
  194. }
  195. finally
  196. {
  197. ControlHelper.FreezeControl(this, false);
  198. }
  199. }
  200. /// <summary>
  201. /// Handles the Click event of the lbl control.
  202. /// </summary>
  203. /// <param name="sender">The source of the event.</param>
  204. /// <param name="e">The <see cref="EventArgs"/> instance containing the event data.</param>
  205. void lbl_Click(object sender, EventArgs e)
  206. {
  207. Label lbl = sender as Label;
  208. if (lbl.Tag != null)
  209. {
  210. var menu = (NavigationMenuItemExt)lbl.Tag;
  211. if (menu.ShowControl == null)
  212. {
  213. selectItem = menu;
  214. if (ClickItemed != null)
  215. {
  216. ClickItemed(this, e);
  217. }
  218. }
  219. }
  220. }
  221. /// <summary>
  222. /// Handles the MouseEnter event of the lbl control.
  223. /// </summary>
  224. /// <param name="sender">The source of the event.</param>
  225. /// <param name="e">The <see cref="EventArgs" /> instance containing the event data.</param>
  226. void lbl_MouseEnter(object sender, EventArgs e)
  227. {
  228. Label lbl = sender as Label;
  229. var menu = lbl.Tag as NavigationMenuItemExt;
  230. foreach (var item in m_lstAnchors)
  231. {
  232. m_lstAnchors[item.Key].Hide();
  233. }
  234. if (menu.ShowControl != null)
  235. {
  236. if (!m_lstAnchors.ContainsKey(menu))
  237. {
  238. m_lstAnchors[menu] = new FrmAnchor(lbl, menu.ShowControl);
  239. }
  240. m_lstAnchors[menu].Show(this);
  241. m_lstAnchors[menu].Size = menu.ShowControl.Size;
  242. }
  243. }
  244. /// <summary>
  245. /// Handles the Paint event of the lbl control.
  246. /// </summary>
  247. /// <param name="sender">The source of the event.</param>
  248. /// <param name="e">The <see cref="PaintEventArgs" /> instance containing the event data.</param>
  249. void lbl_Paint(object sender, PaintEventArgs e)
  250. {
  251. Label lbl = sender as Label;
  252. if (lbl.Tag != null)
  253. {
  254. var menu = (NavigationMenuItemExt)lbl.Tag;
  255. e.Graphics.SetGDIHigh();
  256. if (menu.ShowTip)
  257. {
  258. if (!string.IsNullOrEmpty(menu.TipText))
  259. {
  260. var rect = new Rectangle(lbl.Width - 25, lbl.Height / 2 - 10, 20, 20);
  261. var path = rect.CreateRoundedRectanglePath(5);
  262. e.Graphics.FillPath(new SolidBrush(tipColor), path);
  263. e.Graphics.DrawString(menu.TipText, new Font("微软雅黑", 8f), new SolidBrush(Color.White), rect, new StringFormat() { Alignment = StringAlignment.Center, LineAlignment = StringAlignment.Center });
  264. }
  265. else
  266. {
  267. e.Graphics.FillEllipse(new SolidBrush(tipColor), new Rectangle(lbl.Width - 20, lbl.Height / 2 - 10, 10, 10));
  268. }
  269. }
  270. if (menu.Icon != null)
  271. {
  272. e.Graphics.DrawImage(menu.Icon, new Rectangle(1, (lbl.Height - 25) / 2, 25, 25), 0, 0, menu.Icon.Width, menu.Icon.Height, GraphicsUnit.Pixel);
  273. }
  274. }
  275. }
  276. }
  277. }