UCNavigationMenu.cs 20 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553
  1. // ***********************************************************************
  2. // Assembly : HZH_Controls
  3. // Created : 2019-10-08
  4. //
  5. // ***********************************************************************
  6. // <copyright file="UCNavigationMenu.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 UCNavigationMenu.
  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 UCNavigationMenu : 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 NavigationMenuItem selectItem = null;
  44. /// <summary>
  45. /// Gets the select item.
  46. /// </summary>
  47. /// <value>The select item.</value>
  48. [Description("选中的节点"), Category("自定义")]
  49. public NavigationMenuItem SelectItem
  50. {
  51. get { return selectItem; }
  52. private set { selectItem = value; }
  53. }
  54. /// <summary>
  55. /// The items
  56. /// </summary>
  57. NavigationMenuItem[] items;
  58. /// <summary>
  59. /// Gets or sets the items.
  60. /// </summary>
  61. /// <value>The items.</value>
  62. [Description("节点列表"), Category("自定义")]
  63. public NavigationMenuItem[] 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<NavigationMenuItem, FrmAnchor> m_lstAnchors = new Dictionary<NavigationMenuItem, FrmAnchor>();
  137. /// <summary>
  138. /// Initializes a new instance of the <see cref="UCNavigationMenu"/> class.
  139. /// </summary>
  140. public UCNavigationMenu()
  141. {
  142. InitializeComponent();
  143. items = new NavigationMenuItem[0];
  144. if (ControlHelper.IsDesignMode())
  145. {
  146. items = new NavigationMenuItem[4];
  147. for (int i = 0; i < 4; i++)
  148. {
  149. items[i] = new NavigationMenuItem()
  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 = (NavigationMenuItem)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 = (NavigationMenuItem)lbl.Tag;
  211. if (menu.Items == null || menu.Items.Length <= 0)
  212. {
  213. selectItem = menu;
  214. while (m_lstAnchors.Count > 0)
  215. {
  216. try
  217. {
  218. foreach (var item in m_lstAnchors)
  219. {
  220. item.Value.Close();
  221. m_lstAnchors.Remove(item.Key);
  222. }
  223. }
  224. catch { }
  225. }
  226. if (ClickItemed != null)
  227. {
  228. ClickItemed(this, e);
  229. }
  230. }
  231. else
  232. {
  233. CloseList(menu);
  234. if (m_lstAnchors.ContainsKey(menu))
  235. {
  236. if (m_lstAnchors[menu] != null && !m_lstAnchors[menu].IsDisposed)
  237. {
  238. m_lstAnchors[menu].Close();
  239. }
  240. m_lstAnchors.Remove(menu);
  241. }
  242. ShowMoreMenu(lbl);
  243. }
  244. }
  245. }
  246. /// <summary>
  247. /// Handles the MouseEnter event of the lbl control.
  248. /// </summary>
  249. /// <param name="sender">The source of the event.</param>
  250. /// <param name="e">The <see cref="EventArgs"/> instance containing the event data.</param>
  251. void lbl_MouseEnter(object sender, EventArgs e)
  252. {
  253. Label lbl = sender as Label;
  254. ShowMoreMenu(lbl);
  255. }
  256. /// <summary>
  257. /// Checks the show.
  258. /// </summary>
  259. /// <param name="menu">The menu.</param>
  260. /// <returns><c>true</c> if XXXX, <c>false</c> otherwise.</returns>
  261. private bool CheckShow(NavigationMenuItem menu)
  262. {
  263. //检查已经打开的节点
  264. if (m_lstAnchors.ContainsKey(menu))
  265. {
  266. CloseList(menu);
  267. return false;
  268. }
  269. if (HasInCacheChild(menu))
  270. {
  271. if (m_lstAnchors.ContainsKey(menu.ParentItem))
  272. {
  273. CloseList(menu.ParentItem);
  274. return true;
  275. }
  276. return false;
  277. }
  278. else
  279. {
  280. for (int i = 0; i < 1; )
  281. {
  282. try
  283. {
  284. foreach (var item in m_lstAnchors)
  285. {
  286. if (m_lstAnchors[item.Key] != null && !m_lstAnchors[item.Key].IsDisposed)
  287. {
  288. m_lstAnchors[item.Key].Close();
  289. }
  290. }
  291. }
  292. catch
  293. {
  294. continue;
  295. }
  296. i++;
  297. }
  298. m_lstAnchors.Clear();
  299. return true;
  300. }
  301. }
  302. /// <summary>
  303. /// Determines whether [has in cache child] [the specified menu].
  304. /// </summary>
  305. /// <param name="menu">The menu.</param>
  306. /// <returns><c>true</c> if [has in cache child] [the specified menu]; otherwise, <c>false</c>.</returns>
  307. private bool HasInCacheChild(NavigationMenuItem menu)
  308. {
  309. foreach (var item in m_lstAnchors)
  310. {
  311. if (item.Key == menu)
  312. {
  313. return true;
  314. }
  315. else
  316. {
  317. if (item.Key.Items != null)
  318. {
  319. if (item.Key.Items.Contains(menu))
  320. return true;
  321. }
  322. }
  323. }
  324. return false;
  325. }
  326. /// <summary>
  327. /// Closes the list.
  328. /// </summary>
  329. /// <param name="menu">The menu.</param>
  330. private void CloseList(NavigationMenuItem menu)
  331. {
  332. if (menu.Items != null)
  333. {
  334. foreach (var item in menu.Items)
  335. {
  336. CloseList(item);
  337. if (m_lstAnchors.ContainsKey(item))
  338. {
  339. if (m_lstAnchors[item] != null && !m_lstAnchors[item].IsDisposed)
  340. {
  341. m_lstAnchors[item].Close();
  342. m_lstAnchors[item] = null;
  343. m_lstAnchors.Remove(item);
  344. }
  345. }
  346. }
  347. }
  348. }
  349. /// <summary>
  350. /// Shows the more menu.
  351. /// </summary>
  352. /// <param name="lbl">The label.</param>
  353. private void ShowMoreMenu(Label lbl)
  354. {
  355. var menu = (NavigationMenuItem)lbl.Tag;
  356. if (CheckShow(menu))
  357. {
  358. if (menu.Items != null && menu.Items.Length > 0)
  359. {
  360. Panel panel = new Panel();
  361. panel.BackColor = Color.White;
  362. panel.Paint += panel_Paint;
  363. panel.Padding = new System.Windows.Forms.Padding(1);
  364. Size size = GetItemsSize(menu.Items);
  365. var height = size.Height * menu.Items.Length + 2;
  366. height += menu.Items.Count(p => p.HasSplitLintAtTop);//分割线
  367. if (size.Width < lbl.Width)
  368. size.Width = lbl.Width;
  369. panel.Size = new Size(size.Width, height);
  370. foreach (var item in menu.Items)
  371. {
  372. if (item.HasSplitLintAtTop)
  373. {
  374. UCSplitLine_H line = new UCSplitLine_H();
  375. line.Dock = DockStyle.Top;
  376. panel.Controls.Add(line);
  377. line.BringToFront();
  378. }
  379. Label _lbl = new Label();
  380. _lbl.Font = Font;
  381. _lbl.ForeColor = this.BackColor;
  382. _lbl.AutoSize = false;
  383. _lbl.TextAlign = ContentAlignment.MiddleCenter;
  384. _lbl.Height = size.Height;
  385. _lbl.Text = item.Text;
  386. _lbl.Dock = DockStyle.Top;
  387. _lbl.BringToFront();
  388. _lbl.Paint += lbl_Paint;
  389. _lbl.MouseEnter += lbl_MouseEnter;
  390. _lbl.Tag = item;
  391. _lbl.Click += lbl_Click;
  392. _lbl.Size = new System.Drawing.Size(size.Width, size.Height);
  393. panel.Controls.Add(_lbl);
  394. _lbl.BringToFront();
  395. }
  396. Point point = Point.Empty;
  397. if (menu.ParentItem != null)
  398. {
  399. Point p = lbl.Parent.PointToScreen(lbl.Location);
  400. if (p.X + lbl.Width + panel.Width > Screen.PrimaryScreen.Bounds.Width)
  401. {
  402. point = new Point(-1 * panel.Width - 2, -1 * lbl.Height);
  403. }
  404. else
  405. {
  406. point = new Point(panel.Width + 2, -1 * lbl.Height);
  407. }
  408. }
  409. m_lstAnchors[menu] = new FrmAnchor(lbl, panel, point);
  410. m_lstAnchors[menu].FormClosing += UCNavigationMenu_FormClosing;
  411. m_lstAnchors[menu].Show(this);
  412. m_lstAnchors[menu].Size = new Size(size.Width, height);
  413. }
  414. }
  415. }
  416. /// <summary>
  417. /// Handles the FormClosing event of the UCNavigationMenu control.
  418. /// </summary>
  419. /// <param name="sender">The source of the event.</param>
  420. /// <param name="e">The <see cref="FormClosingEventArgs"/> instance containing the event data.</param>
  421. void UCNavigationMenu_FormClosing(object sender, FormClosingEventArgs e)
  422. {
  423. FrmAnchor frm = sender as FrmAnchor;
  424. if (m_lstAnchors.ContainsValue(frm))
  425. {
  426. foreach (var item in m_lstAnchors)
  427. {
  428. if (item.Value == frm)
  429. {
  430. m_lstAnchors.Remove(item.Key);
  431. return;
  432. }
  433. }
  434. }
  435. }
  436. /// <summary>
  437. /// Handles the Paint event of the panel control.
  438. /// </summary>
  439. /// <param name="sender">The source of the event.</param>
  440. /// <param name="e">The <see cref="PaintEventArgs"/> instance containing the event data.</param>
  441. void panel_Paint(object sender, PaintEventArgs e)
  442. {
  443. e.Graphics.SetGDIHigh();
  444. Rectangle rect = new Rectangle(0, 0, e.ClipRectangle.Width - 1, e.ClipRectangle.Height - 1);
  445. var path = rect.CreateRoundedRectanglePath(2);
  446. e.Graphics.DrawPath(new Pen(new SolidBrush(LineColors.Light)), path);
  447. }
  448. /// <summary>
  449. /// Gets the size of the items.
  450. /// </summary>
  451. /// <param name="items">The items.</param>
  452. /// <returns>Size.</returns>
  453. private Size GetItemsSize(NavigationMenuItem[] items)
  454. {
  455. Size size = Size.Empty;
  456. if (items != null && items.Length > 0)
  457. {
  458. using (var g = this.CreateGraphics())
  459. {
  460. foreach (NavigationMenuItem item in items)
  461. {
  462. var s = g.MeasureString(item.Text, Font);
  463. if (s.Width + 25 > size.Width)
  464. {
  465. size.Width = (int)s.Width + 25;
  466. }
  467. if (s.Height + 10 > size.Height)
  468. {
  469. size.Height = (int)s.Height + 10;
  470. }
  471. }
  472. }
  473. }
  474. return size;
  475. }
  476. /// <summary>
  477. /// Handles the Paint event of the lbl control.
  478. /// </summary>
  479. /// <param name="sender">The source of the event.</param>
  480. /// <param name="e">The <see cref="PaintEventArgs"/> instance containing the event data.</param>
  481. void lbl_Paint(object sender, PaintEventArgs e)
  482. {
  483. Label lbl = sender as Label;
  484. if (lbl.Tag != null)
  485. {
  486. var menu = (NavigationMenuItem)lbl.Tag;
  487. e.Graphics.SetGDIHigh();
  488. if (menu.ParentItem == null)//顶级节点支持图标和角标
  489. {
  490. if (menu.ShowTip)
  491. {
  492. if (!string.IsNullOrEmpty(menu.TipText))
  493. {
  494. var rect = new Rectangle(lbl.Width - 25, lbl.Height / 2 - 10, 20, 20);
  495. var path = rect.CreateRoundedRectanglePath(5);
  496. e.Graphics.FillPath(new SolidBrush(tipColor), path);
  497. e.Graphics.DrawString(menu.TipText, new Font("微软雅黑", 8f), new SolidBrush(Color.White), rect, new StringFormat() { Alignment = StringAlignment.Center, LineAlignment = StringAlignment.Center });
  498. }
  499. else
  500. {
  501. e.Graphics.FillEllipse(new SolidBrush(tipColor), new Rectangle(lbl.Width - 20, lbl.Height / 2 - 10, 10, 10));
  502. }
  503. }
  504. if (menu.Icon != null)
  505. {
  506. e.Graphics.DrawImage(menu.Icon, new Rectangle(1, (lbl.Height - 25) / 2, 25, 25), 0, 0, menu.Icon.Width, menu.Icon.Height, GraphicsUnit.Pixel);
  507. }
  508. }
  509. if (menu.ParentItem != null && menu.Items != null && menu.Items.Length > 0)
  510. {
  511. ControlHelper.PaintTriangle(e.Graphics, new SolidBrush(this.BackColor), new Point(lbl.Width - 11, (lbl.Height - 5) / 2), 5, GraphDirection.Rightward);
  512. }
  513. }
  514. }
  515. }
  516. }