欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页  >  IT编程

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

程序员文章站 2022-06-19 22:02:01
前提 入行已经7,8年了,一直想做一套漂亮点的自定义控件,于是就有了本系列文章。 GitHub:https://github.com/kwwwvagaa/NetWinformControl 码云:https://gitee.com/kwwwvagaa/net_winform_custom_contr ......

前提

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

github:https://github.com/kwwwvagaa/netwinformcontrol

码云:

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

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

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

nuget

install-package hzh_controls

目录

用处及效果

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

准备工作

主要用的就是停靠窗体了,(十九)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         }

完整代码

  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 
 26 namespace hzh_controls.controls
 27 {
 28     /// <summary>
 29     /// class ucnavigationmenu.
 30     /// implements the <see cref="system.windows.forms.usercontrol" />
 31     /// </summary>
 32     /// <seealso cref="system.windows.forms.usercontrol" />
 33     [defaultevent("clickitemed")]
 34     public partial class ucnavigationmenu : usercontrol
 35     {
 36         /// <summary>
 37         /// occurs when [click itemed].
 38         /// </summary>
 39         [description("点击节点事件"), category("自定义")]
 40 
 41         public event eventhandler clickitemed;
 42         /// <summary>
 43         /// the select item
 44         /// </summary>
 45         private navigationmenuitem selectitem = null;
 46 
 47         /// <summary>
 48         /// gets the select item.
 49         /// </summary>
 50         /// <value>the select item.</value>
 51         [description("选中的节点"), category("自定义")]
 52         public navigationmenuitem selectitem
 53         {
 54             get { return selectitem; }
 55             private set { selectitem = value; }
 56         }
 57 
 58 
 59         /// <summary>
 60         /// the items
 61         /// </summary>
 62         navigationmenuitem[] items;
 63 
 64         /// <summary>
 65         /// gets or sets the items.
 66         /// </summary>
 67         /// <value>the items.</value>
 68         [description("节点列表"), category("自定义")]
 69         public navigationmenuitem[] items
 70         {
 71             get { return items; }
 72             set
 73             {
 74                 items = value;
 75                 reloadmenu();
 76             }
 77         }
 78 
 79         /// <summary>
 80         /// the tip color
 81         /// </summary>
 82         private color tipcolor = color.fromargb(255, 87, 34);
 83 
 84         /// <summary>
 85         /// gets or sets the color of the tip.
 86         /// </summary>
 87         /// <value>the color of the tip.</value>
 88         [description("角标颜色"), category("自定义")]
 89         public color tipcolor
 90         {
 91             get { return tipcolor; }
 92             set { tipcolor = value; }
 93         }
 94 
 95         /// <summary>
 96         /// 获取或设置控件的前景色。
 97         /// </summary>
 98         /// <value>the color of the fore.</value>
 99         /// <permissionset>
100         ///   <ipermission class="system.security.permissions.fileiopermission, mscorlib, version=2.0.3600.0, culture=neutral, publickeytoken=b77a5c561934e089" version="1" unrestricted="true" />
101         /// </permissionset>
102         public override system.drawing.color forecolor
103         {
104             get
105             {
106                 return base.forecolor;
107             }
108             set
109             {
110                 base.forecolor = value;
111                 foreach (control c in this.controls)
112                 {
113                     c.forecolor = value;
114                 }
115             }
116         }
117         /// <summary>
118         /// 获取或设置控件显示的文字的字体。
119         /// </summary>
120         /// <value>the font.</value>
121         /// <permissionset>
122         ///   <ipermission class="system.security.permissions.environmentpermission, mscorlib, version=2.0.3600.0, culture=neutral, publickeytoken=b77a5c561934e089" version="1" unrestricted="true" />
123         ///   <ipermission class="system.security.permissions.fileiopermission, mscorlib, version=2.0.3600.0, culture=neutral, publickeytoken=b77a5c561934e089" version="1" unrestricted="true" />
124         ///   <ipermission class="system.security.permissions.securitypermission, mscorlib, version=2.0.3600.0, culture=neutral, publickeytoken=b77a5c561934e089" version="1" flags="unmanagedcode, controlevidence" />
125         ///   <ipermission class="system.diagnostics.performancecounterpermission, system, version=2.0.3600.0, culture=neutral, publickeytoken=b77a5c561934e089" version="1" unrestricted="true" />
126         /// </permissionset>
127         public override font font
128         {
129             get
130             {
131                 return base.font;
132             }
133             set
134             {
135                 base.font = value;
136                 foreach (control c in this.controls)
137                 {
138                     c.font = value;
139                 }
140             }
141         }
142 
143         /// <summary>
144         /// the m lst anchors
145         /// </summary>
146         dictionary<navigationmenuitem, frmanchor> m_lstanchors = new dictionary<navigationmenuitem, frmanchor>();
147 
148         /// <summary>
149         /// initializes a new instance of the <see cref="ucnavigationmenu"/> class.
150         /// </summary>
151         public ucnavigationmenu()
152         {
153             initializecomponent();
154             items = new navigationmenuitem[0];
155             if (controlhelper.isdesignmode())
156             {
157                 items = new navigationmenuitem[4];
158                 for (int i = 0; i < 4; i++)
159                 {
160                     items[i] = new navigationmenuitem()
161                     {
162                         text = "菜单" + (i + 1),
163                         anchorright = i >= 2
164                     };
165                 }
166             }
167         }
168 
169         /// <summary>
170         /// reloads the menu.
171         /// </summary>
172         private void reloadmenu()
173         {
174             try
175             {
176                 controlhelper.freezecontrol(this, true);
177                 this.controls.clear();
178                 if (items != null && items.length > 0)
179                 {
180                     foreach (var item in items)
181                     {
182                         var menu = (navigationmenuitem)item;
183                         label lbl = new label();
184                         lbl.autosize = false;
185                         lbl.textalign = contentalignment.middlecenter;
186                         lbl.width = menu.itemwidth;
187                         lbl.text = menu.text;
188 
189                         lbl.font = font;
190                         lbl.forecolor = forecolor;
191 
192                         lbl.paint += lbl_paint;
193                         lbl.mouseenter += lbl_mouseenter;
194                         lbl.tag = menu;
195                         lbl.click += lbl_click;
196                         if (menu.anchorright)
197                         {
198                             lbl.dock = dockstyle.right;
199                         }
200                         else
201                         {
202                             lbl.dock = dockstyle.left;
203                         }
204                         this.controls.add(lbl);
205 
206                         lbl.bringtofront();
207                     }
208 
209 
210                 }
211             }
212             finally
213             {
214                 controlhelper.freezecontrol(this, false);
215             }
216         }
217 
218 
219 
220         /// <summary>
221         /// handles the click event of the lbl control.
222         /// </summary>
223         /// <param name="sender">the source of the event.</param>
224         /// <param name="e">the <see cref="eventargs"/> instance containing the event data.</param>
225         void lbl_click(object sender, eventargs e)
226         {
227             label lbl = sender as label;
228             if (lbl.tag != null)
229             {
230                 var menu = (navigationmenuitem)lbl.tag;
231                 if (menu.items == null || menu.items.length <= 0)
232                 {
233                     selectitem = menu;
234 
235                     while (m_lstanchors.count > 0)
236                     {
237                         try
238                         {
239                             foreach (var item in m_lstanchors)
240                             {
241                                 item.value.close();
242                                 m_lstanchors.remove(item.key);
243                             }
244                         }
245                         catch { }
246                     }
247 
248                     if (clickitemed != null)
249                     {
250                         clickitemed(this, e);
251                     }
252                 }
253                 else
254                 {
255                     closelist(menu);
256                     if (m_lstanchors.containskey(menu))
257                     {
258                         if (m_lstanchors[menu] != null && !m_lstanchors[menu].isdisposed)
259                         {
260                             m_lstanchors[menu].close();
261                         }
262                         m_lstanchors.remove(menu);
263                     }
264                     showmoremenu(lbl);
265                 }
266             }
267         }
268 
269         /// <summary>
270         /// handles the mouseenter event of the lbl control.
271         /// </summary>
272         /// <param name="sender">the source of the event.</param>
273         /// <param name="e">the <see cref="eventargs"/> instance containing the event data.</param>
274         void lbl_mouseenter(object sender, eventargs e)
275         {
276             label lbl = sender as label;
277             showmoremenu(lbl);
278         }
279 
280         /// <summary>
281         /// checks the show.
282         /// </summary>
283         /// <param name="menu">the menu.</param>
284         /// <returns><c>true</c> if xxxx, <c>false</c> otherwise.</returns>
285         private bool checkshow(navigationmenuitem menu)
286         {
287             //检查已经打开的节点
288             if (m_lstanchors.containskey(menu))
289             {
290                 closelist(menu);
291                 return false;
292             }
293             if (hasincachechild(menu))
294             {
295                 if (m_lstanchors.containskey(menu.parentitem))
296                 {
297                     closelist(menu.parentitem);
298                     return true;
299                 }
300                 return false;
301             }
302             else
303             {
304                 for (int i = 0; i < 1; )
305                 {
306                     try
307                     {
308                         foreach (var item in m_lstanchors)
309                         {
310                             if (m_lstanchors[item.key] != null && !m_lstanchors[item.key].isdisposed)
311                             {
312                                 m_lstanchors[item.key].close();
313                             }
314                         }
315                     }
316                     catch
317                     {
318                         continue;
319                     }
320                     i++;
321                 }
322                 m_lstanchors.clear();
323                 return true;
324             }
325         }
326 
327         /// <summary>
328         /// determines whether [has in cache child] [the specified menu].
329         /// </summary>
330         /// <param name="menu">the menu.</param>
331         /// <returns><c>true</c> if [has in cache child] [the specified menu]; otherwise, <c>false</c>.</returns>
332         private bool hasincachechild(navigationmenuitem menu)
333         {
334             foreach (var item in m_lstanchors)
335             {
336                 if (item.key == menu)
337                 {
338                     return true;
339                 }
340                 else
341                 {
342                     if (item.key.items != null)
343                     {
344                         if (item.key.items.contains(menu))
345                             return true;
346                     }
347                 }
348             }
349             return false;
350         }
351 
352         /// <summary>
353         /// closes the list.
354         /// </summary>
355         /// <param name="menu">the menu.</param>
356         private void closelist(navigationmenuitem menu)
357         {
358             if (menu.items != null)
359             {
360                 foreach (var item in menu.items)
361                 {
362                     closelist(item);
363                     if (m_lstanchors.containskey(item))
364                     {
365                         if (m_lstanchors[item] != null && !m_lstanchors[item].isdisposed)
366                         {
367                             m_lstanchors[item].close();
368                             m_lstanchors[item] = null;
369                             m_lstanchors.remove(item);
370                         }
371                     }
372                 }
373             }
374         }
375 
376         /// <summary>
377         /// shows the more menu.
378         /// </summary>
379         /// <param name="lbl">the label.</param>
380         private void showmoremenu(label lbl)
381         {
382             var menu = (navigationmenuitem)lbl.tag;
383             if (checkshow(menu))
384             {
385                 if (menu.items != null && menu.items.length > 0)
386                 {
387                     panel panel = new panel();
388                     panel.backcolor = color.white;
389                     panel.paint += panel_paint;
390                     panel.padding = new system.windows.forms.padding(1);
391                     size size = getitemssize(menu.items);
392                     var height = size.height * menu.items.length + 2;
393                     height += menu.items.count(p => p.hassplitlintattop);//分割线
394                     if (size.width < lbl.width)
395                         size.width = lbl.width;
396                     panel.size = new size(size.width, height);
397 
398                     foreach (var item in menu.items)
399                     {
400                         if (item.hassplitlintattop)
401                         {
402                             ucsplitline_h line = new ucsplitline_h();
403                             line.dock = dockstyle.top;
404                             panel.controls.add(line);
405                             line.bringtofront();
406                         }
407                         label _lbl = new label();
408                         _lbl.font = font;
409                         _lbl.forecolor = this.backcolor;
410                         _lbl.autosize = false;
411                         _lbl.textalign = contentalignment.middlecenter;
412                         _lbl.height = size.height;
413                         _lbl.text = item.text;
414                         _lbl.dock = dockstyle.top;
415                         _lbl.bringtofront();
416                         _lbl.paint += lbl_paint;
417                         _lbl.mouseenter += lbl_mouseenter;
418                         _lbl.tag = item;
419                         _lbl.click += lbl_click;
420                         _lbl.size = new system.drawing.size(size.width, size.height);
421                         panel.controls.add(_lbl);
422                         _lbl.bringtofront();
423                     }
424                     point point = point.empty;
425 
426                     if (menu.parentitem != null)
427                     {
428                         point p = lbl.parent.pointtoscreen(lbl.location);
429                         if (p.x + lbl.width + panel.width > screen.primaryscreen.bounds.width)
430                         {
431                             point = new point(-1 * panel.width - 2, -1 * lbl.height);
432                         }
433                         else
434                         {
435                             point = new point(panel.width + 2, -1 * lbl.height);
436                         }
437                     }
438                     m_lstanchors[menu] = new frmanchor(lbl, panel, point);
439                     m_lstanchors[menu].formclosing += ucnavigationmenu_formclosing;
440                     m_lstanchors[menu].show();
441                     m_lstanchors[menu].size = new size(size.width, height);
442                 }
443             }
444 
445         }
446 
447         /// <summary>
448         /// handles the formclosing event of the ucnavigationmenu control.
449         /// </summary>
450         /// <param name="sender">the source of the event.</param>
451         /// <param name="e">the <see cref="formclosingeventargs"/> instance containing the event data.</param>
452         void ucnavigationmenu_formclosing(object sender, formclosingeventargs e)
453         {
454             frmanchor frm = sender as frmanchor;
455             if (m_lstanchors.containsvalue(frm))
456             {
457                 foreach (var item in m_lstanchors)
458                 {
459                     if (item.value == frm)
460                     {
461                         m_lstanchors.remove(item.key);
462                         return;
463                     }
464                 }
465             }
466         }
467 
468         /// <summary>
469         /// handles the paint event of the panel control.
470         /// </summary>
471         /// <param name="sender">the source of the event.</param>
472         /// <param name="e">the <see cref="painteventargs"/> instance containing the event data.</param>
473         void panel_paint(object sender, painteventargs e)
474         {
475             e.graphics.setgdihigh();
476             rectangle rect = new rectangle(0, 0, e.cliprectangle.width - 1, e.cliprectangle.height - 1);
477             var path = rect.createroundedrectanglepath(2);
478             e.graphics.drawpath(new pen(new solidbrush(linecolors.light)), path);
479         }
480 
481 
482 
483         /// <summary>
484         /// gets the size of the items.
485         /// </summary>
486         /// <param name="items">the items.</param>
487         /// <returns>size.</returns>
488         private size getitemssize(navigationmenuitem[] items)
489         {
490             size size = size.empty;
491             if (items != null && items.length > 0)
492             {
493                 using (var g = this.creategraphics())
494                 {
495                     foreach (navigationmenuitem item in items)
496                     {
497                         var s = g.measurestring(item.text, font);
498                         if (s.width + 25 > size.width)
499                         {
500                             size.width = (int)s.width + 25;
501                         }
502                         if (s.height + 10 > size.height)
503                         {
504                             size.height = (int)s.height + 10;
505                         }
506                     }
507                 }
508             }
509             return size;
510         }
511 
512 
513         /// <summary>
514         /// handles the paint event of the lbl control.
515         /// </summary>
516         /// <param name="sender">the source of the event.</param>
517         /// <param name="e">the <see cref="painteventargs"/> instance containing the event data.</param>
518         void lbl_paint(object sender, painteventargs e)
519         {
520             label lbl = sender as label;
521             if (lbl.tag != null)
522             {
523                 var menu = (navigationmenuitem)lbl.tag;
524                 e.graphics.setgdihigh();
525                 if (menu.parentitem == null)//*节点支持图标和角标
526                 {
527                     if (menu.showtip)
528                     {
529                         if (!string.isnullorempty(menu.tiptext))
530                         {
531                             var rect = new rectangle(lbl.width - 25, lbl.height / 2 - 10, 20, 20);
532                             var path = rect.createroundedrectanglepath(5);
533                             e.graphics.fillpath(new solidbrush(tipcolor), path);
534                             e.graphics.drawstring(menu.tiptext, new font("微软雅黑", 8f), new solidbrush(color.white), rect, new stringformat() { alignment = stringalignment.center, linealignment = stringalignment.center });
535                         }
536                         else
537                         {
538                             e.graphics.fillellipse(new solidbrush(tipcolor), new rectangle(lbl.width - 20, lbl.height / 2 - 10, 10, 10));
539                         }
540                     }
541                     if (menu.icon != null)
542                     {
543                         e.graphics.drawimage(menu.icon, new rectangle(1, (lbl.height - 25) / 2, 25, 25), 0, 0, menu.icon.width, menu.icon.height, graphicsunit.pixel);
544                     }
545                 }
546                 if (menu.parentitem != null && menu.items != null && menu.items.length > 0)
547                 {
548                     controlhelper.painttriangle(e.graphics, new solidbrush(this.backcolor), new point(lbl.width - 11, (lbl.height - 5) / 2), 5, graphdirection.rightward);
549                 }
550             }
551         }
552     }
553 }

 

最后的话

如果你喜欢的话,请到  点个星星吧