UCNavigationMenuOffice.cs 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428
  1. // ***********************************************************************
  2. // Assembly : HZH_Controls
  3. // Created : 2019-10-12
  4. //
  5. // ***********************************************************************
  6. // <copyright file="UCNavigationMenuOffice.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 UCNavigationMenuOffice.
  29. /// Implements the <see cref="System.Windows.Forms.UserControl" />
  30. /// </summary>
  31. /// <seealso cref="System.Windows.Forms.UserControl" />
  32. public partial class UCNavigationMenuOffice : UserControl
  33. {
  34. /// <summary>
  35. /// The main menu height
  36. /// </summary>
  37. private int mainMenuHeight = 25;
  38. /// <summary>
  39. /// Gets or sets the height of the main menu.
  40. /// </summary>
  41. /// <value>The height of the main menu.</value>
  42. [Description("主菜单高度,大于20的值"), Category("自定义")]
  43. public int MainMenuHeight
  44. {
  45. get { return mainMenuHeight; }
  46. set
  47. {
  48. if (value < 20)
  49. return;
  50. mainMenuHeight = value;
  51. this.panMenu.Height = value;
  52. }
  53. }
  54. /// <summary>
  55. /// The expand height
  56. /// </summary>
  57. private int expandHeight = 125;
  58. /// <summary>
  59. /// Gets or sets the height of the expand.
  60. /// </summary>
  61. /// <value>The height of the expand.</value>
  62. [Description("展开后高度"), Category("自定义")]
  63. public int ExpandHeight
  64. {
  65. get { return expandHeight; }
  66. set { expandHeight = value; }
  67. }
  68. /// <summary>
  69. /// The is expand
  70. /// </summary>
  71. private bool isExpand = true;
  72. /// <summary>
  73. /// Gets or sets a value indicating whether this instance is expand.
  74. /// </summary>
  75. /// <value><c>true</c> if this instance is expand; otherwise, <c>false</c>.</value>
  76. [Description("是否展开"), Category("自定义")]
  77. public bool IsExpand
  78. {
  79. get { return isExpand; }
  80. set
  81. {
  82. isExpand = value;
  83. if (value)
  84. {
  85. this.Height = expandHeight;
  86. ResetChildControl();
  87. }
  88. else
  89. {
  90. this.Height = this.panMenu.Height;
  91. this.panChilds.Controls.Clear();
  92. }
  93. }
  94. }
  95. /// <summary>
  96. /// Occurs when [click itemed].
  97. /// </summary>
  98. [Description("点击节点事件"), Category("自定义")]
  99. public event EventHandler ClickItemed;
  100. /// <summary>
  101. /// The select item
  102. /// </summary>
  103. private NavigationMenuItemExt selectItem = null;
  104. /// <summary>
  105. /// Gets the select item.
  106. /// </summary>
  107. /// <value>The select item.</value>
  108. [Description("选中的节点"), Category("自定义")]
  109. public NavigationMenuItemExt SelectItem
  110. {
  111. get { return selectItem; }
  112. private set { selectItem = value; }
  113. }
  114. /// <summary>
  115. /// The items
  116. /// </summary>
  117. NavigationMenuItemExt[] items;
  118. /// <summary>
  119. /// Gets or sets the items.
  120. /// </summary>
  121. /// <value>The items.</value>
  122. [Description("节点列表"), Category("自定义")]
  123. public NavigationMenuItemExt[] Items
  124. {
  125. get { return items; }
  126. set
  127. {
  128. items = value;
  129. ReloadMenu();
  130. if (value != null && value.Length > 0)
  131. {
  132. selectItem = value[0];
  133. ResetChildControl();
  134. }
  135. }
  136. }
  137. /// <summary>
  138. /// The tip color
  139. /// </summary>
  140. private Color tipColor = Color.FromArgb(255, 87, 34);
  141. /// <summary>
  142. /// Gets or sets the color of the tip.
  143. /// </summary>
  144. /// <value>The color of the tip.</value>
  145. [Description("角标颜色"), Category("自定义")]
  146. public Color TipColor
  147. {
  148. get { return tipColor; }
  149. set { tipColor = value; }
  150. }
  151. /// <summary>
  152. /// 获取或设置控件的前景色。
  153. /// </summary>
  154. /// <value>The color of the fore.</value>
  155. /// <PermissionSet>
  156. /// <IPermission class="System.Security.Permissions.FileIOPermission, mscorlib, Version=2.0.3600.0, Culture=neutral, PublicKeyToken=b77a5c561934e089" version="1" Unrestricted="true" />
  157. /// </PermissionSet>
  158. public override System.Drawing.Color ForeColor
  159. {
  160. get
  161. {
  162. return base.ForeColor;
  163. }
  164. set
  165. {
  166. base.ForeColor = value;
  167. foreach (Control c in this.Controls)
  168. {
  169. c.ForeColor = value;
  170. }
  171. }
  172. }
  173. /// <summary>
  174. /// 获取或设置控件显示的文字的字体。
  175. /// </summary>
  176. /// <value>The font.</value>
  177. /// <PermissionSet>
  178. /// <IPermission class="System.Security.Permissions.EnvironmentPermission, mscorlib, Version=2.0.3600.0, Culture=neutral, PublicKeyToken=b77a5c561934e089" version="1" Unrestricted="true" />
  179. /// <IPermission class="System.Security.Permissions.FileIOPermission, mscorlib, Version=2.0.3600.0, Culture=neutral, PublicKeyToken=b77a5c561934e089" version="1" Unrestricted="true" />
  180. /// <IPermission class="System.Security.Permissions.SecurityPermission, mscorlib, Version=2.0.3600.0, Culture=neutral, PublicKeyToken=b77a5c561934e089" version="1" Flags="UnmanagedCode, ControlEvidence" />
  181. /// <IPermission class="System.Diagnostics.PerformanceCounterPermission, System, Version=2.0.3600.0, Culture=neutral, PublicKeyToken=b77a5c561934e089" version="1" Unrestricted="true" />
  182. /// </PermissionSet>
  183. public override Font Font
  184. {
  185. get
  186. {
  187. return base.Font;
  188. }
  189. set
  190. {
  191. base.Font = value;
  192. foreach (Control c in this.Controls)
  193. {
  194. c.Font = value;
  195. }
  196. }
  197. }
  198. /// <summary>
  199. /// The m LST anchors
  200. /// </summary>
  201. Dictionary<NavigationMenuItemExt, FrmAnchor> m_lstAnchors = new Dictionary<NavigationMenuItemExt, FrmAnchor>();
  202. /// <summary>
  203. /// Initializes a new instance of the <see cref="UCNavigationMenuOffice"/> class.
  204. /// </summary>
  205. public UCNavigationMenuOffice()
  206. {
  207. InitializeComponent();
  208. this.SizeChanged += UCNavigationMenuOffice_SizeChanged;
  209. items = new NavigationMenuItemExt[0];
  210. if (ControlHelper.IsDesignMode())
  211. {
  212. items = new NavigationMenuItemExt[4];
  213. for (int i = 0; i < 4; i++)
  214. {
  215. items[i] = new NavigationMenuItemExt()
  216. {
  217. Text = "菜单" + (i + 1),
  218. AnchorRight = i >= 2
  219. };
  220. }
  221. }
  222. }
  223. /// <summary>
  224. /// Handles the SizeChanged event of the UCNavigationMenuOffice control.
  225. /// </summary>
  226. /// <param name="sender">The source of the event.</param>
  227. /// <param name="e">The <see cref="EventArgs"/> instance containing the event data.</param>
  228. void UCNavigationMenuOffice_SizeChanged(object sender, EventArgs e)
  229. {
  230. if (isExpand)
  231. {
  232. expandHeight = this.Height;
  233. }
  234. }
  235. /// <summary>
  236. /// Resets the child control.
  237. /// </summary>
  238. public void ResetChildControl()
  239. {
  240. if (isExpand)
  241. {
  242. if (selectItem != null)
  243. {
  244. try
  245. {
  246. ControlHelper.FreezeControl(this, true);
  247. this.panChilds.Controls.Clear();
  248. if (selectItem.ShowControl != null)
  249. {
  250. HZH_Controls.Controls.UCSplitLine_H split = new UCSplitLine_H();
  251. split.BackColor = Color.FromArgb(50, 197, 197, 197);
  252. split.Dock = DockStyle.Top;
  253. this.panChilds.Controls.Add(split);
  254. split.BringToFront();
  255. this.panChilds.Controls.Add(selectItem.ShowControl);
  256. selectItem.ShowControl.Dock = DockStyle.Fill;
  257. }
  258. }
  259. finally
  260. {
  261. ControlHelper.FreezeControl(this, false);
  262. }
  263. }
  264. }
  265. }
  266. /// <summary>
  267. /// Reloads the menu.
  268. /// </summary>
  269. private void ReloadMenu()
  270. {
  271. try
  272. {
  273. ControlHelper.FreezeControl(this, true);
  274. this.panMenu.Controls.Clear();
  275. if (items != null && items.Length > 0)
  276. {
  277. foreach (var item in items)
  278. {
  279. var menu = (NavigationMenuItemExt)item;
  280. Label lbl = new Label();
  281. lbl.AutoSize = false;
  282. lbl.TextAlign = ContentAlignment.MiddleCenter;
  283. lbl.Width = menu.ItemWidth;
  284. lbl.Text = menu.Text;
  285. lbl.Font = Font;
  286. lbl.ForeColor = ForeColor;
  287. lbl.Paint += lbl_Paint;
  288. lbl.MouseEnter += lbl_MouseEnter;
  289. lbl.Tag = menu;
  290. lbl.Click += lbl_Click;
  291. lbl.DoubleClick += lbl_DoubleClick;
  292. if (menu.AnchorRight)
  293. {
  294. lbl.Dock = DockStyle.Right;
  295. }
  296. else
  297. {
  298. lbl.Dock = DockStyle.Left;
  299. }
  300. this.panMenu.Controls.Add(lbl);
  301. lbl.BringToFront();
  302. }
  303. }
  304. }
  305. finally
  306. {
  307. ControlHelper.FreezeControl(this, false);
  308. }
  309. }
  310. /// <summary>
  311. /// Handles the DoubleClick event of the lbl control.
  312. /// </summary>
  313. /// <param name="sender">The source of the event.</param>
  314. /// <param name="e">The <see cref="EventArgs"/> instance containing the event data.</param>
  315. void lbl_DoubleClick(object sender, EventArgs e)
  316. {
  317. IsExpand = !IsExpand;
  318. }
  319. /// <summary>
  320. /// Handles the Click event of the lbl control.
  321. /// </summary>
  322. /// <param name="sender">The source of the event.</param>
  323. /// <param name="e">The <see cref="EventArgs" /> instance containing the event data.</param>
  324. void lbl_Click(object sender, EventArgs e)
  325. {
  326. Label lbl = sender as Label;
  327. if (lbl.Tag != null)
  328. {
  329. var menu = (NavigationMenuItemExt)lbl.Tag;
  330. if (menu.ShowControl == null)
  331. {
  332. selectItem = menu;
  333. if (ClickItemed != null)
  334. {
  335. ClickItemed(this, e);
  336. }
  337. }
  338. else
  339. {
  340. if (IsExpand)
  341. {
  342. selectItem = menu;
  343. ResetChildControl();
  344. }
  345. }
  346. }
  347. }
  348. /// <summary>
  349. /// Handles the MouseEnter event of the lbl control.
  350. /// </summary>
  351. /// <param name="sender">The source of the event.</param>
  352. /// <param name="e">The <see cref="EventArgs" /> instance containing the event data.</param>
  353. void lbl_MouseEnter(object sender, EventArgs e)
  354. {
  355. if (!IsExpand)
  356. {
  357. Label lbl = sender as Label;
  358. var menu = lbl.Tag as NavigationMenuItemExt;
  359. foreach (var item in m_lstAnchors)
  360. {
  361. m_lstAnchors[item.Key].Hide();
  362. }
  363. if (menu.ShowControl != null)
  364. {
  365. if (!m_lstAnchors.ContainsKey(menu))
  366. {
  367. m_lstAnchors[menu] = new FrmAnchor(panMenu, menu.ShowControl, isNotFocus: false);
  368. }
  369. m_lstAnchors[menu].BackColor = this.BackColor;
  370. m_lstAnchors[menu].Show(this);
  371. m_lstAnchors[menu].Size = new Size(this.panChilds.Width, expandHeight - mainMenuHeight);
  372. }
  373. }
  374. }
  375. /// <summary>
  376. /// Handles the Paint event of the lbl control.
  377. /// </summary>
  378. /// <param name="sender">The source of the event.</param>
  379. /// <param name="e">The <see cref="PaintEventArgs" /> instance containing the event data.</param>
  380. void lbl_Paint(object sender, PaintEventArgs e)
  381. {
  382. Label lbl = sender as Label;
  383. if (lbl.Tag != null)
  384. {
  385. var menu = (NavigationMenuItemExt)lbl.Tag;
  386. e.Graphics.SetGDIHigh();
  387. if (menu.ShowTip)
  388. {
  389. if (!string.IsNullOrEmpty(menu.TipText))
  390. {
  391. var rect = new Rectangle(lbl.Width - 25, lbl.Height / 2 - 10, 20, 20);
  392. var path = rect.CreateRoundedRectanglePath(5);
  393. e.Graphics.FillPath(new SolidBrush(tipColor), path);
  394. e.Graphics.DrawString(menu.TipText, new Font("微软雅黑", 8f), new SolidBrush(Color.White), rect, new StringFormat() { Alignment = StringAlignment.Center, LineAlignment = StringAlignment.Center });
  395. }
  396. else
  397. {
  398. e.Graphics.FillEllipse(new SolidBrush(tipColor), new Rectangle(lbl.Width - 20, lbl.Height / 2 - 10, 10, 10));
  399. }
  400. }
  401. if (menu.Icon != null)
  402. {
  403. e.Graphics.DrawImage(menu.Icon, new Rectangle(1, (lbl.Height - 25) / 2, 25, 25), 0, 0, menu.Icon.Width, menu.Icon.Height, GraphicsUnit.Pixel);
  404. }
  405. }
  406. }
  407. }
  408. }