(七十九)c#Winform自定义控件-导航菜单

前提

入行已经7,8年了,一直想做一套漂亮点的自定义控件,于是就有了本系列文章。

官网:https://www.hzhcontrols.cn

GitHub:https://github.com/kwwwvagaa/NetWinformControl

码云:HZHControls控件库: HZHControls控件库,c#的winform自定义控件,对触屏具有更好的操作支持,项目是基于framework4.0,完全原生控件开发,没有使用任何第三方控件,你可以放心的用在你的项目中(winfromcontrol/winformcontrol/.net)。还有更丰富的工业控件持续增加中~~~

如果觉得写的还行,请点个 star 支持一下吧

欢迎前来交流探讨: 企鹅群568015492 企鹅群568015492

来都来了,点个【推荐】再走吧,谢谢

NuGet

Install-Package HZH_Controls

目录

c#Winform自定义控件-目录_c#winform自定义控件-有图标的按钮-CSDN博客

用处及效果

准备工作

主要用的就是停靠窗体了,(十九)c#Winform自定义控件-停靠窗体,不了解的可以先去看一下

思路:

通过实体对象设置的对齐方式来实现左右对齐,

当鼠标进入一项的时候,判断是否弹出下拉列表,或关闭其他列表

开始

添加一个类用来设置节点信息

复制代码

  1   public class NavigationMenuItem
  2     {
  3         /// <summary>
  4         /// The icon
  5         /// </summary>
  6         private Image icon;
  7         /// <summary>
  8         /// Gets or sets the icon.
  9         /// </summary>
 10         /// <value>The icon.</value>
 11         [Description("图标,仅顶级节点有效")]
 12         public Image Icon
 13         {
 14             get { return icon; }
 15             set { icon = value; }
 16         }
 17 
 18         /// <summary>
 19         /// The text
 20         /// </summary>
 21         private string text;
 22         /// <summary>
 23         /// Gets or sets the text.
 24         /// </summary>
 25         /// <value>The text.</value>
 26 
 27         [Description("文本")]
 28         public string Text
 29         {
 30             get { return text; }
 31             set { text = value; }
 32         }
 33 
 34         /// <summary>
 35         /// The show tip
 36         /// </summary>
 37         private bool showTip;
 38         /// <summary>
 39         /// Gets or sets a value indicating whether [show tip].当TipText为空时只显示一个小圆点,否则显示TipText文字
 40         /// </summary>
 41         /// <value><c>true</c> if [show tip]; otherwise, <c>false</c>.</value>
 42         [Description("是否显示角标,仅顶级节点有效")]
 43         public bool ShowTip
 44         {
 45             get { return showTip; }
 46             set { showTip = value; }
 47         }
 48 
 49         /// <summary>
 50         /// The tip text
 51         /// </summary>
 52         private string tipText;
 53         /// <summary>
 54         /// Gets or sets the tip text
 55         /// </summary>
 56         /// <value>The tip text.</value>
 57         [Description("角标文字,仅顶级节点有效")]
 58         public string TipText
 59         {
 60             get { return tipText; }
 61             set { tipText = value; }
 62         }
 63         /// <summary>
 64         /// The items
 65         /// </summary>
 66         private NavigationMenuItem[] items;
 67         /// <summary>
 68         /// Gets or sets the items.
 69         /// </summary>
 70         /// <value>The items.</value>
 71         [Description("子项列表")]
 72         public NavigationMenuItem[] Items
 73         {
 74             get { return items; }
 75             set
 76             {
 77                 items = value;
 78                 if (value != null)
 79                 {
 80                     foreach (var item in value)
 81                     {
 82                         item.ParentItem = this;
 83                     }
 84                 }
 85             }
 86         }
 87 
 88         /// <summary>
 89         /// The anchor right
 90         /// </summary>
 91         private bool anchorRight;
 92 
 93         /// <summary>
 94         /// Gets or sets a value indicating whether [anchor right].
 95         /// </summary>
 96         /// <value><c>true</c> if [anchor right]; otherwise, <c>false</c>.</value>
 97         [Description("是否靠右对齐")]
 98         public bool AnchorRight
 99         {
100             get { return anchorRight; }
101             set { anchorRight = value; }
102         }
103 
104         /// <summary>
105         /// The item width
106         /// </summary>
107         private int itemWidth = 100;
108 
109         /// <summary>
110         /// Gets or sets the width of the item.
111         /// </summary>
112         /// <value>The width of the item.</value>
113         [Description("宽度")]
114         public int ItemWidth
115         {
116             get { return itemWidth; }
117             set { itemWidth = value; }
118         }
119 
120         /// <summary>
121         /// Gets or sets the data source.
122         /// </summary>
123         /// <value>The data source.</value>
124         [Description("数据源")]
125         public object DataSource { get; set; }
126         /// <summary>
127         /// Gets or sets a value indicating whether this instance has split lint at top.
128         /// </summary>
129         /// <value><c>true</c> if this instance has split lint at top; otherwise, <c>false</c>.</value>
130         [Description("是否在此项顶部显示一个分割线")]
131         public bool HasSplitLintAtTop { get; set; }
132 
133         /// <summary>
134         /// Gets the parent item.
135         /// </summary>
136         /// <value>The parent item.</value>
137         [Description("父节点")]
138         public NavigationMenuItem ParentItem { get; private set; }
139     }

复制代码

添加一个自定义控件UCNavigationMenu

添加一些属性

复制代码

  1 /// <summary>
  2         /// Occurs when [click itemed].
  3         /// </summary>
  4         [Description("点击节点事件"), Category("自定义")]
  5 
  6         public event EventHandler ClickItemed;
  7         /// <summary>
  8         /// The select item
  9         /// </summary>
 10         private NavigationMenuItem selectItem = null;
 11 
 12         /// <summary>
 13         /// Gets the select item.
 14         /// </summary>
 15         /// <value>The select item.</value>
 16         [Description("选中的节点"), Category("自定义")]
 17         public NavigationMenuItem SelectItem
 18         {
 19             get { return selectItem; }
 20             private set { selectItem = value; }
 21         }
 22 
 23 
 24         /// <summary>
 25         /// The items
 26         /// </summary>
 27         NavigationMenuItem[] items;
 28 
 29         /// <summary>
 30         /// Gets or sets the items.
 31         /// </summary>
 32         /// <value>The items.</value>
 33         [Description("节点列表"), Category("自定义")]
 34         public NavigationMenuItem[] Items
 35         {
 36             get { return items; }
 37             set
 38             {
 39                 items = value;
 40                 ReloadMenu();
 41             }
 42         }
 43 
 44         /// <summary>
 45         /// The tip color
 46         /// </summary>
 47         private Color tipColor = Color.FromArgb(255, 87, 34);
 48 
 49         /// <summary>
 50         /// Gets or sets the color of the tip.
 51         /// </summary>
 52         /// <value>The color of the tip.</value>
 53         [Description("角标颜色"), Category("自定义")]
 54         public Color TipColor
 55         {
 56             get { return tipColor; }
 57             set { tipColor = value; }
 58         }
 59 
 60         /// <summary>
 61         /// 获取或设置控件的前景色。
 62         /// </summary>
 63         /// <value>The color of the fore.</value>
 64         /// <PermissionSet>
 65         ///   <IPermission class="System.Security.Permissions.FileIOPermission, mscorlib, Version=2.0.3600.0, Culture=neutral, PublicKeyToken=b77a5c561934e089" version="1" Unrestricted="true" />
 66         /// </PermissionSet>
 67         public override System.Drawing.Color ForeColor
 68         {
 69             get
 70             {
 71                 return base.ForeColor;
 72             }
 73             set
 74             {
 75                 base.ForeColor = value;
 76                 foreach (Control c in this.Controls)
 77                 {
 78                     c.ForeColor = value;
 79                 }
 80             }
 81         }
 82         /// <summary>
 83         /// 获取或设置控件显示的文字的字体。
 84         /// </summary>
 85         /// <value>The font.</value>
 86         /// <PermissionSet>
 87         ///   <IPermission class="System.Security.Permissions.EnvironmentPermission, mscorlib, Version=2.0.3600.0, Culture=neutral, PublicKeyToken=b77a5c561934e089" version="1" Unrestricted="true" />
 88         ///   <IPermission class="System.Security.Permissions.FileIOPermission, mscorlib, Version=2.0.3600.0, Culture=neutral, PublicKeyToken=b77a5c561934e089" version="1" Unrestricted="true" />
 89         ///   <IPermission class="System.Security.Permissions.SecurityPermission, mscorlib, Version=2.0.3600.0, Culture=neutral, PublicKeyToken=b77a5c561934e089" version="1" Flags="UnmanagedCode, ControlEvidence" />
 90         ///   <IPermission class="System.Diagnostics.PerformanceCounterPermission, System, Version=2.0.3600.0, Culture=neutral, PublicKeyToken=b77a5c561934e089" version="1" Unrestricted="true" />
 91         /// </PermissionSet>
 92         public override Font Font
 93         {
 94             get
 95             {
 96                 return base.Font;
 97             }
 98             set
 99             {
100                 base.Font = value;
101                 foreach (Control c in this.Controls)
102                 {
103                     c.Font = value;
104                 }
105             }
106         }
107 
108         /// <summary>
109         /// The m LST anchors
110         /// </summary>
111         Dictionary<NavigationMenuItem, FrmAnchor> m_lstAnchors = new Dictionary<NavigationMenuItem, FrmAnchor>();

复制代码

重载菜单

复制代码

 1   private void ReloadMenu()
 2         {
 3             try
 4             {
 5                 ControlHelper.FreezeControl(this, true);
 6                 this.Controls.Clear();
 7                 if (items != null && items.Length > 0)
 8                 {
 9                     foreach (var item in items)
10                     {
11                         var menu = (NavigationMenuItem)item;
12                         Label lbl = new Label();
13                         lbl.AutoSize = false;
14                         lbl.TextAlign = ContentAlignment.MiddleCenter;
15                         lbl.Width = menu.ItemWidth;
16                         lbl.Text = menu.Text;
17 
18                         lbl.Font = Font;
19                         lbl.ForeColor = ForeColor;
20 
21                         lbl.Paint += lbl_Paint;
22                         lbl.MouseEnter += lbl_MouseEnter;
23                         lbl.Tag = menu;
24                         lbl.Click += lbl_Click;
25                         if (menu.AnchorRight)
26                         {
27                             lbl.Dock = DockStyle.Right;
28                         }
29                         else
30                         {
31                             lbl.Dock = DockStyle.Left;
32                         }
33                         this.Controls.Add(lbl);
34 
35                         lbl.BringToFront();
36                     }
37 
38 
39                 }
40             }
41             finally
42             {
43                 ControlHelper.FreezeControl(this, false);
44             }
45         }

复制代码

显示下级菜单

复制代码

 1 private void ShowMoreMenu(Label lbl)
 2         {
 3             var menu = (NavigationMenuItem)lbl.Tag;
 4             if (CheckShow(menu))
 5             {
 6                 if (menu.Items != null && menu.Items.Length > 0)
 7                 {
 8                     Panel panel = new Panel();
 9                     panel.BackColor = Color.White;
10                     panel.Paint += panel_Paint;
11                     panel.Padding = new System.Windows.Forms.Padding(1);
12                     Size size = GetItemsSize(menu.Items);
13                     var height = size.Height * menu.Items.Length + 2;
14                     height += menu.Items.Count(p => p.HasSplitLintAtTop);//分割线
15                     if (size.Width < lbl.Width)
16                         size.Width = lbl.Width;
17                     panel.Size = new Size(size.Width, height);
18 
19                     foreach (var item in menu.Items)
20                     {
21                         if (item.HasSplitLintAtTop)
22                         {
23                             UCSplitLine_H line = new UCSplitLine_H();
24                             line.Dock = DockStyle.Top;
25                             panel.Controls.Add(line);
26                             line.BringToFront();
27                         }
28                         Label _lbl = new Label();
29                         _lbl.Font = Font;
30                         _lbl.ForeColor = this.BackColor;
31                         _lbl.AutoSize = false;
32                         _lbl.TextAlign = ContentAlignment.MiddleCenter;
33                         _lbl.Height = size.Height;
34                         _lbl.Text = item.Text;
35                         _lbl.Dock = DockStyle.Top;
36                         _lbl.BringToFront();
37                         _lbl.Paint += lbl_Paint;
38                         _lbl.MouseEnter += lbl_MouseEnter;
39                         _lbl.Tag = item;
40                         _lbl.Click += lbl_Click;
41                         _lbl.Size = new System.Drawing.Size(size.Width, size.Height);
42                         panel.Controls.Add(_lbl);
43                         _lbl.BringToFront();
44                     }
45                     Point point = Point.Empty;
46 
47                     if (menu.ParentItem != null)
48                     {
49                         Point p = lbl.Parent.PointToScreen(lbl.Location);
50                         if (p.X + lbl.Width + panel.Width > Screen.PrimaryScreen.Bounds.Width)
51                         {
52                             point = new Point(-1 * panel.Width - 2, -1 * lbl.Height);
53                         }
54                         else
55                         {
56                             point = new Point(panel.Width + 2, -1 * lbl.Height);
57                         }
58                     }
59                     m_lstAnchors[menu] = new FrmAnchor(lbl, panel, point);
60                     m_lstAnchors[menu].FormClosing += UCNavigationMenu_FormClosing;
61                     m_lstAnchors[menu].Show();
62                     m_lstAnchors[menu].Size = new Size(size.Width, height);
63                 }
64             }
65 
66         }

复制代码

辅助函数

复制代码

 1   /// <summary>
 2         /// Checks the show.
 3         /// </summary>
 4         /// <param name="menu">The menu.</param>
 5         /// <returns><c>true</c> if XXXX, <c>false</c> otherwise.</returns>
 6         private bool CheckShow(NavigationMenuItem menu)
 7         {
 8             //检查已经打开的节点
 9             if (m_lstAnchors.ContainsKey(menu))
10             {
11                 CloseList(menu);
12                 return false;
13             }
14             if (HasInCacheChild(menu))
15             {
16                 if (m_lstAnchors.ContainsKey(menu.ParentItem))
17                 {
18                     CloseList(menu.ParentItem);
19                     return true;
20                 }
21                 return false;
22             }
23             else
24             {
25                 for (int i = 0; i < 1; )
26                 {
27                     try
28                     {
29                         foreach (var item in m_lstAnchors)
30                         {
31                             if (m_lstAnchors[item.Key] != null && !m_lstAnchors[item.Key].IsDisposed)
32                             {
33                                 m_lstAnchors[item.Key].Close();
34                             }
35                         }
36                     }
37                     catch
38                     {
39                         continue;
40                     }
41                     i++;
42                 }
43                 m_lstAnchors.Clear();
44                 return true;
45             }
46         }
47 
48         /// <summary>
49         /// Determines whether [has in cache child] [the specified menu].
50         /// </summary>
51         /// <param name="menu">The menu.</param>
52         /// <returns><c>true</c> if [has in cache child] [the specified menu]; otherwise, <c>false</c>.</returns>
53         private bool HasInCacheChild(NavigationMenuItem menu)
54         {
55             foreach (var item in m_lstAnchors)
56             {
57                 if (item.Key == menu)
58                 {
59                     return true;
60                 }
61                 else
62                 {
63                     if (item.Key.Items != null)
64                     {
65                         if (item.Key.Items.Contains(menu))
66                             return true;
67                     }
68                 }
69             }
70             return false;
71         }
72 
73         /// <summary>
74         /// Closes the list.
75         /// </summary>
76         /// <param name="menu">The menu.</param>
77         private void CloseList(NavigationMenuItem menu)
78         {
79             if (menu.Items != null)
80             {
81                 foreach (var item in menu.Items)
82                 {
83                     CloseList(item);
84                     if (m_lstAnchors.ContainsKey(item))
85                     {
86                         if (m_lstAnchors[item] != null && !m_lstAnchors[item].IsDisposed)
87                         {
88                             m_lstAnchors[item].Close();
89                             m_lstAnchors[item] = null;
90                             m_lstAnchors.Remove(item);
91                         }
92                     }
93                 }
94             }
95         }

复制代码

一些事件

复制代码

  1 /// <summary>
  2         /// Handles the Click event of the lbl control.
  3         /// </summary>
  4         /// <param name="sender">The source of the event.</param>
  5         /// <param name="e">The <see cref="EventArgs"/> instance containing the event data.</param>
  6         void lbl_Click(object sender, EventArgs e)
  7         {
  8             Label lbl = sender as Label;
  9             if (lbl.Tag != null)
 10             {
 11                 var menu = (NavigationMenuItem)lbl.Tag;
 12                 if (menu.Items == null || menu.Items.Length <= 0)
 13                 {
 14                     selectItem = menu;
 15 
 16                     while (m_lstAnchors.Count > 0)
 17                     {
 18                         try
 19                         {
 20                             foreach (var item in m_lstAnchors)
 21                             {
 22                                 item.Value.Close();
 23                                 m_lstAnchors.Remove(item.Key);
 24                             }
 25                         }
 26                         catch { }
 27                     }
 28 
 29                     if (ClickItemed != null)
 30                     {
 31                         ClickItemed(this, e);
 32                     }
 33                 }
 34                 else
 35                 {
 36                     CloseList(menu);
 37                     if (m_lstAnchors.ContainsKey(menu))
 38                     {
 39                         if (m_lstAnchors[menu] != null && !m_lstAnchors[menu].IsDisposed)
 40                         {
 41                             m_lstAnchors[menu].Close();
 42                         }
 43                         m_lstAnchors.Remove(menu);
 44                     }
 45                     ShowMoreMenu(lbl);
 46                 }
 47             }
 48         }
 49 
 50         /// <summary>
 51         /// Handles the MouseEnter event of the lbl control.
 52         /// </summary>
 53         /// <param name="sender">The source of the event.</param>
 54         /// <param name="e">The <see cref="EventArgs"/> instance containing the event data.</param>
 55         void lbl_MouseEnter(object sender, EventArgs e)
 56         {
 57             Label lbl = sender as Label;
 58             ShowMoreMenu(lbl);
 59         }
 60 /// <summary>
 61         /// Handles the FormClosing event of the UCNavigationMenu control.
 62         /// </summary>
 63         /// <param name="sender">The source of the event.</param>
 64         /// <param name="e">The <see cref="FormClosingEventArgs"/> instance containing the event data.</param>
 65         void UCNavigationMenu_FormClosing(object sender, FormClosingEventArgs e)
 66         {
 67             FrmAnchor frm = sender as FrmAnchor;
 68             if (m_lstAnchors.ContainsValue(frm))
 69             {
 70                 foreach (var item in m_lstAnchors)
 71                 {
 72                     if (item.Value == frm)
 73                     {
 74                         m_lstAnchors.Remove(item.Key);
 75                         return;
 76                     }
 77                 }
 78             }
 79         }
 80 
 81         /// <summary>
 82         /// Handles the Paint event of the panel control.
 83         /// </summary>
 84         /// <param name="sender">The source of the event.</param>
 85         /// <param name="e">The <see cref="PaintEventArgs"/> instance containing the event data.</param>
 86         void panel_Paint(object sender, PaintEventArgs e)
 87         {
 88             e.Graphics.SetGDIHigh();
 89             Rectangle rect = new Rectangle(0, 0, e.ClipRectangle.Width - 1, e.ClipRectangle.Height - 1);
 90             var path = rect.CreateRoundedRectanglePath(2);
 91             e.Graphics.DrawPath(new Pen(new SolidBrush(LineColors.Light)), path);
 92         }
 93 
 94 
 95 
 96         /// <summary>
 97         /// Gets the size of the items.
 98         /// </summary>
 99         /// <param name="items">The items.</param>
100         /// <returns>Size.</returns>
101         private Size GetItemsSize(NavigationMenuItem[] items)
102         {
103             Size size = Size.Empty;
104             if (items != null && items.Length > 0)
105             {
106                 using (var g = this.CreateGraphics())
107                 {
108                     foreach (NavigationMenuItem item in items)
109                     {
110                         var s = g.MeasureString(item.Text, Font);
111                         if (s.Width + 25 > size.Width)
112                         {
113                             size.Width = (int)s.Width + 25;
114                         }
115                         if (s.Height + 10 > size.Height)
116                         {
117                             size.Height = (int)s.Height + 10;
118                         }
119                     }
120                 }
121             }
122             return size;
123         }
124 
125 
126         /// <summary>
127         /// Handles the Paint event of the lbl control.
128         /// </summary>
129         /// <param name="sender">The source of the event.</param>
130         /// <param name="e">The <see cref="PaintEventArgs"/> instance containing the event data.</param>
131         void lbl_Paint(object sender, PaintEventArgs e)
132         {
133             Label lbl = sender as Label;
134             if (lbl.Tag != null)
135             {
136                 var menu = (NavigationMenuItem)lbl.Tag;
137                 e.Graphics.SetGDIHigh();
138                 if (menu.ParentItem == null)//顶级节点支持图标和角标
139                 {
140                     if (menu.ShowTip)
141                     {
142                         if (!string.IsNullOrEmpty(menu.TipText))
143                         {
144                             var rect = new Rectangle(lbl.Width - 25, lbl.Height / 2 - 10, 20, 20);
145                             var path = rect.CreateRoundedRectanglePath(5);
146                             e.Graphics.FillPath(new SolidBrush(tipColor), path);
147                             e.Graphics.DrawString(menu.TipText, new Font("微软雅黑", 8f), new SolidBrush(Color.White), rect, new StringFormat() { Alignment = StringAlignment.Center, LineAlignment = StringAlignment.Center });
148                         }
149                         else
150                         {
151                             e.Graphics.FillEllipse(new SolidBrush(tipColor), new Rectangle(lbl.Width - 20, lbl.Height / 2 - 10, 10, 10));
152                         }
153                     }
154                     if (menu.Icon != null)
155                     {
156                         e.Graphics.DrawImage(menu.Icon, new Rectangle(1, (lbl.Height - 25) / 2, 25, 25), 0, 0, menu.Icon.Width, menu.Icon.Height, GraphicsUnit.Pixel);
157                     }
158                 }
159                 if (menu.ParentItem != null && menu.Items != null && menu.Items.Length > 0)
160                 {
161                     ControlHelper.PaintTriangle(e.Graphics, new SolidBrush(this.BackColor), new Point(lbl.Width - 11, (lbl.Height - 5) / 2), 5, GraphDirection.Rightward);
162                 }
163             }
164         }

复制代码

完整代码

// ***********************************************************************
// Assembly         : HZH_Controls
// Created          : 2019-10-08
//
// ***********************************************************************
// <copyright file="UCNavigationMenu.cs">
//     Copyright by Huang Zhenghui(黄正辉) All, QQ group:568015492 QQ:623128629 Email:623128629@qq.com
// </copyright>
//
// Blog: https://www.cnblogs.com/bfyx
// GitHub:https://github.com/kwwwvagaa/NetWinformControl
// gitee:https://gitee.com/kwwwvagaa/net_winform_custom_control.git
//
// If you use this code, please keep this note.
// ***********************************************************************
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Drawing;
using System.Data;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using HZH_Controls.Forms;

namespace HZH_Controls.Controls
{
    /// <summary>
    /// Class UCNavigationMenu.
    /// Implements the <see cref="System.Windows.Forms.UserControl" />
    /// </summary>
    /// <seealso cref="System.Windows.Forms.UserControl" />
    [DefaultEvent("ClickItemed")]
    public partial class UCNavigationMenu : UserControl
    {
        /// <summary>
        /// Occurs when [click itemed].
        /// </summary>
        [Description("点击节点事件"), Category("自定义")]

        public event EventHandler ClickItemed;
        /// <summary>
        /// The select item
        /// </summary>
        private NavigationMenuItem selectItem = null;

        /// <summary>
        /// Gets the select item.
        /// </summary>
        /// <value>The select item.</value>
        [Description("选中的节点"), Category("自定义")]
        public NavigationMenuItem SelectItem
        {
            get { return selectItem; }
            private set { selectItem = value; }
        }


        /// <summary>
        /// The items
        /// </summary>
        NavigationMenuItem[] items;

        /// <summary>
        /// Gets or sets the items.
        /// </summary>
        /// <value>The items.</value>
        [Description("节点列表"), Category("自定义")]
        public NavigationMenuItem[] Items
        {
            get { return items; }
            set
            {
                items = value;
                ReloadMenu();
            }
        }

        /// <summary>
        /// The tip color
        /// </summary>
        private Color tipColor = Color.FromArgb(255, 87, 34);

        /// <summary>
        /// Gets or sets the color of the tip.
        /// </summary>
        /// <value>The color of the tip.</value>
        [Description("角标颜色"), Category("自定义")]
        public Color TipColor
        {
            get { return tipColor; }
            set { tipColor = value; }
        }

        /// <summary>
        /// 获取或设置控件的前景色。
        /// </summary>
        /// <value>The color of the fore.</value>
        /// <PermissionSet>
        ///   <IPermission class="System.Security.Permissions.FileIOPermission, mscorlib, Version=2.0.3600.0, Culture=neutral, PublicKeyToken=b77a5c561934e089" version="1" Unrestricted="true" />
        /// </PermissionSet>
        public override System.Drawing.Color ForeColor
        {
            get
            {
                return base.ForeColor;
            }
            set
            {
                base.ForeColor = value;
                foreach (Control c in this.Controls)
                {
                    c.ForeColor = value;
                }
            }
        }
        /// <summary>
        /// 获取或设置控件显示的文字的字体。
        /// </summary>
        /// <value>The font.</value>
        /// <PermissionSet>
        ///   <IPermission class="System.Security.Permissions.EnvironmentPermission, mscorlib, Version=2.0.3600.0, Culture=neutral, PublicKeyToken=b77a5c561934e089" version="1" Unrestricted="true" />
        ///   <IPermission class="System.Security.Permissions.FileIOPermission, mscorlib, Version=2.0.3600.0, Culture=neutral, PublicKeyToken=b77a5c561934e089" version="1" Unrestricted="true" />
        ///   <IPermission class="System.Security.Permissions.SecurityPermission, mscorlib, Version=2.0.3600.0, Culture=neutral, PublicKeyToken=b77a5c561934e089" version="1" Flags="UnmanagedCode, ControlEvidence" />
        ///   <IPermission class="System.Diagnostics.PerformanceCounterPermission, System, Version=2.0.3600.0, Culture=neutral, PublicKeyToken=b77a5c561934e089" version="1" Unrestricted="true" />
        /// </PermissionSet>
        public override Font Font
        {
            get
            {
                return base.Font;
            }
            set
            {
                base.Font = value;
                foreach (Control c in this.Controls)
                {
                    c.Font = value;
                }
            }
        }

        /// <summary>
        /// The m LST anchors
        /// </summary>
        Dictionary<NavigationMenuItem, FrmAnchor> m_lstAnchors = new Dictionary<NavigationMenuItem, FrmAnchor>();

        /// <summary>
        /// Initializes a new instance of the <see cref="UCNavigationMenu"/> class.
        /// </summary>
        public UCNavigationMenu()
        {
            InitializeComponent();
            items = new NavigationMenuItem[0];
            if (ControlHelper.IsDesignMode())
            {
                items = new NavigationMenuItem[4];
                for (int i = 0; i < 4; i++)
                {
                    items[i] = new NavigationMenuItem()
                    {
                        Text = "菜单" + (i + 1),
                        AnchorRight = i >= 2
                    };
                }
            }
        }

        /// <summary>
        /// Reloads the menu.
        /// </summary>
        private void ReloadMenu()
        {
            try
            {
                ControlHelper.FreezeControl(this, true);
                this.Controls.Clear();
                if (items != null && items.Length > 0)
                {
                    foreach (var item in items)
                    {
                        var menu = (NavigationMenuItem)item;
                        Label lbl = new Label();
                        lbl.AutoSize = false;
                        lbl.TextAlign = ContentAlignment.MiddleCenter;
                        lbl.Width = menu.ItemWidth;
                        lbl.Text = menu.Text;

                        lbl.Font = Font;
                        lbl.ForeColor = ForeColor;

                        lbl.Paint += lbl_Paint;
                        lbl.MouseEnter += lbl_MouseEnter;
                        lbl.Tag = menu;
                        lbl.Click += lbl_Click;
                        if (menu.AnchorRight)
                        {
                            lbl.Dock = DockStyle.Right;
                        }
                        else
                        {
                            lbl.Dock = DockStyle.Left;
                        }
                        this.Controls.Add(lbl);

                        lbl.BringToFront();
                    }


                }
            }
            finally
            {
                ControlHelper.FreezeControl(this, false);
            }
        }



        /// <summary>
        /// Handles the Click event of the lbl control.
        /// </summary>
        /// <param name="sender">The source of the event.</param>
        /// <param name="e">The <see cref="EventArgs"/> instance containing the event data.</param>
        void lbl_Click(object sender, EventArgs e)
        {
            Label lbl = sender as Label;
            if (lbl.Tag != null)
            {
                var menu = (NavigationMenuItem)lbl.Tag;
                if (menu.Items == null || menu.Items.Length <= 0)
                {
                    selectItem = menu;

                    while (m_lstAnchors.Count > 0)
                    {
                        try
                        {
                            foreach (var item in m_lstAnchors)
                            {
                                item.Value.Close();
                                m_lstAnchors.Remove(item.Key);
                            }
                        }
                        catch { }
                    }

                    if (ClickItemed != null)
                    {
                        ClickItemed(this, e);
                    }
                }
                else
                {
                    CloseList(menu);
                    if (m_lstAnchors.ContainsKey(menu))
                    {
                        if (m_lstAnchors[menu] != null && !m_lstAnchors[menu].IsDisposed)
                        {
                            m_lstAnchors[menu].Close();
                        }
                        m_lstAnchors.Remove(menu);
                    }
                    ShowMoreMenu(lbl);
                }
            }
        }

        /// <summary>
        /// Handles the MouseEnter event of the lbl control.
        /// </summary>
        /// <param name="sender">The source of the event.</param>
        /// <param name="e">The <see cref="EventArgs"/> instance containing the event data.</param>
        void lbl_MouseEnter(object sender, EventArgs e)
        {
            Label lbl = sender as Label;
            ShowMoreMenu(lbl);
        }

        /// <summary>
        /// Checks the show.
        /// </summary>
        /// <param name="menu">The menu.</param>
        /// <returns><c>true</c> if XXXX, <c>false</c> otherwise.</returns>
        private bool CheckShow(NavigationMenuItem menu)
        {
            //检查已经打开的节点
            if (m_lstAnchors.ContainsKey(menu))
            {
                CloseList(menu);
                return false;
            }
            if (HasInCacheChild(menu))
            {
                if (m_lstAnchors.ContainsKey(menu.ParentItem))
                {
                    CloseList(menu.ParentItem);
                    return true;
                }
                return false;
            }
            else
            {
                for (int i = 0; i < 1; )
                {
                    try
                    {
                        foreach (var item in m_lstAnchors)
                        {
                            if (m_lstAnchors[item.Key] != null && !m_lstAnchors[item.Key].IsDisposed)
                            {
                                m_lstAnchors[item.Key].Close();
                            }
                        }
                    }
                    catch
                    {
                        continue;
                    }
                    i++;
                }
                m_lstAnchors.Clear();
                return true;
            }
        }

        /// <summary>
        /// Determines whether [has in cache child] [the specified menu].
        /// </summary>
        /// <param name="menu">The menu.</param>
        /// <returns><c>true</c> if [has in cache child] [the specified menu]; otherwise, <c>false</c>.</returns>
        private bool HasInCacheChild(NavigationMenuItem menu)
        {
            foreach (var item in m_lstAnchors)
            {
                if (item.Key == menu)
                {
                    return true;
                }
                else
                {
                    if (item.Key.Items != null)
                    {
                        if (item.Key.Items.Contains(menu))
                            return true;
                    }
                }
            }
            return false;
        }

        /// <summary>
        /// Closes the list.
        /// </summary>
        /// <param name="menu">The menu.</param>
        private void CloseList(NavigationMenuItem menu)
        {
            if (menu.Items != null)
            {
                foreach (var item in menu.Items)
                {
                    CloseList(item);
                    if (m_lstAnchors.ContainsKey(item))
                    {
                        if (m_lstAnchors[item] != null && !m_lstAnchors[item].IsDisposed)
                        {
                            m_lstAnchors[item].Close();
                            m_lstAnchors[item] = null;
                            m_lstAnchors.Remove(item);
                        }
                    }
                }
            }
        }

        /// <summary>
        /// Shows the more menu.
        /// </summary>
        /// <param name="lbl">The label.</param>
        private void ShowMoreMenu(Label lbl)
        {
            var menu = (NavigationMenuItem)lbl.Tag;
            if (CheckShow(menu))
            {
                if (menu.Items != null && menu.Items.Length > 0)
                {
                    Panel panel = new Panel();
                    panel.BackColor = Color.White;
                    panel.Paint += panel_Paint;
                    panel.Padding = new System.Windows.Forms.Padding(1);
                    Size size = GetItemsSize(menu.Items);
                    var height = size.Height * menu.Items.Length + 2;
                    height += menu.Items.Count(p => p.HasSplitLintAtTop);//分割线
                    if (size.Width < lbl.Width)
                        size.Width = lbl.Width;
                    panel.Size = new Size(size.Width, height);

                    foreach (var item in menu.Items)
                    {
                        if (item.HasSplitLintAtTop)
                        {
                            UCSplitLine_H line = new UCSplitLine_H();
                            line.Dock = DockStyle.Top;
                            panel.Controls.Add(line);
                            line.BringToFront();
                        }
                        Label _lbl = new Label();
                        _lbl.Font = Font;
                        _lbl.ForeColor = this.BackColor;
                        _lbl.AutoSize = false;
                        _lbl.TextAlign = ContentAlignment.MiddleCenter;
                        _lbl.Height = size.Height;
                        _lbl.Text = item.Text;
                        _lbl.Dock = DockStyle.Top;
                        _lbl.BringToFront();
                        _lbl.Paint += lbl_Paint;
                        _lbl.MouseEnter += lbl_MouseEnter;
                        _lbl.Tag = item;
                        _lbl.Click += lbl_Click;
                        _lbl.Size = new System.Drawing.Size(size.Width, size.Height);
                        panel.Controls.Add(_lbl);
                        _lbl.BringToFront();
                    }
                    Point point = Point.Empty;

                    if (menu.ParentItem != null)
                    {
                        Point p = lbl.Parent.PointToScreen(lbl.Location);
                        if (p.X + lbl.Width + panel.Width > Screen.PrimaryScreen.Bounds.Width)
                        {
                            point = new Point(-1 * panel.Width - 2, -1 * lbl.Height);
                        }
                        else
                        {
                            point = new Point(panel.Width + 2, -1 * lbl.Height);
                        }
                    }
                    m_lstAnchors[menu] = new FrmAnchor(lbl, panel, point);
                    m_lstAnchors[menu].FormClosing += UCNavigationMenu_FormClosing;
                    m_lstAnchors[menu].Show();
                    m_lstAnchors[menu].Size = new Size(size.Width, height);
                }
            }

        }

        /// <summary>
        /// Handles the FormClosing event of the UCNavigationMenu control.
        /// </summary>
        /// <param name="sender">The source of the event.</param>
        /// <param name="e">The <see cref="FormClosingEventArgs"/> instance containing the event data.</param>
        void UCNavigationMenu_FormClosing(object sender, FormClosingEventArgs e)
        {
            FrmAnchor frm = sender as FrmAnchor;
            if (m_lstAnchors.ContainsValue(frm))
            {
                foreach (var item in m_lstAnchors)
                {
                    if (item.Value == frm)
                    {
                        m_lstAnchors.Remove(item.Key);
                        return;
                    }
                }
            }
        }

        /// <summary>
        /// Handles the Paint event of the panel control.
        /// </summary>
        /// <param name="sender">The source of the event.</param>
        /// <param name="e">The <see cref="PaintEventArgs"/> instance containing the event data.</param>
        void panel_Paint(object sender, PaintEventArgs e)
        {
            e.Graphics.SetGDIHigh();
            Rectangle rect = new Rectangle(0, 0, e.ClipRectangle.Width - 1, e.ClipRectangle.Height - 1);
            var path = rect.CreateRoundedRectanglePath(2);
            e.Graphics.DrawPath(new Pen(new SolidBrush(LineColors.Light)), path);
        }



        /// <summary>
        /// Gets the size of the items.
        /// </summary>
        /// <param name="items">The items.</param>
        /// <returns>Size.</returns>
        private Size GetItemsSize(NavigationMenuItem[] items)
        {
            Size size = Size.Empty;
            if (items != null && items.Length > 0)
            {
                using (var g = this.CreateGraphics())
                {
                    foreach (NavigationMenuItem item in items)
                    {
                        var s = g.MeasureString(item.Text, Font);
                        if (s.Width + 25 > size.Width)
                        {
                            size.Width = (int)s.Width + 25;
                        }
                        if (s.Height + 10 > size.Height)
                        {
                            size.Height = (int)s.Height + 10;
                        }
                    }
                }
            }
            return size;
        }


        /// <summary>
        /// Handles the Paint event of the lbl control.
        /// </summary>
        /// <param name="sender">The source of the event.</param>
        /// <param name="e">The <see cref="PaintEventArgs"/> instance containing the event data.</param>
        void lbl_Paint(object sender, PaintEventArgs e)
        {
            Label lbl = sender as Label;
            if (lbl.Tag != null)
            {
                var menu = (NavigationMenuItem)lbl.Tag;
                e.Graphics.SetGDIHigh();
                if (menu.ParentItem == null)//顶级节点支持图标和角标
                {
                    if (menu.ShowTip)
                    {
                        if (!string.IsNullOrEmpty(menu.TipText))
                        {
                            var rect = new Rectangle(lbl.Width - 25, lbl.Height / 2 - 10, 20, 20);
                            var path = rect.CreateRoundedRectanglePath(5);
                            e.Graphics.FillPath(new SolidBrush(tipColor), path);
                            e.Graphics.DrawString(menu.TipText, new Font("微软雅黑", 8f), new SolidBrush(Color.White), rect, new StringFormat() { Alignment = StringAlignment.Center, LineAlignment = StringAlignment.Center });
                        }
                        else
                        {
                            e.Graphics.FillEllipse(new SolidBrush(tipColor), new Rectangle(lbl.Width - 20, lbl.Height / 2 - 10, 10, 10));
                        }
                    }
                    if (menu.Icon != null)
                    {
                        e.Graphics.DrawImage(menu.Icon, new Rectangle(1, (lbl.Height - 25) / 2, 25, 25), 0, 0, menu.Icon.Width, menu.Icon.Height, GraphicsUnit.Pixel);
                    }
                }
                if (menu.ParentItem != null && menu.Items != null && menu.Items.Length > 0)
                {
                    ControlHelper.PaintTriangle(e.Graphics, new SolidBrush(this.BackColor), new Point(lbl.Width - 11, (lbl.Height - 5) / 2), 5, GraphDirection.Rightward);
                }
            }
        }
    }
}

最后的话

如果你喜欢的话,请到 HZHControls控件库: HZHControls控件库,c#的winform自定义控件,对触屏具有更好的操作支持,项目是基于framework4.0,完全原生控件开发,没有使用任何第三方控件,你可以放心的用在你的项目中(winfromcontrol/winformcontrol/.net)。还有更丰富的工业控件持续增加中~~~ 点个星星吧

  • 0
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值