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

(八十四)c#Winform自定义控件-导航菜单(类Office菜单)

程序员文章站 2022-07-02 12:46:43
前提 入行已经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

目录

https://www.cnblogs.com/bfyx/p/11364884.html

用处及效果

(八十四)c#Winform自定义控件-导航菜单(类Office菜单)

准备工作

没什么准备的,就是组装控件

开始

添加一个用户控件ucnavigationmenuoffice

添加属性

  1 /// <summary>
  2         /// the main menu height
  3         /// </summary>
  4         private int mainmenuheight = 25;
  5 
  6         /// <summary>
  7         /// gets or sets the height of the main menu.
  8         /// </summary>
  9         /// <value>the height of the main menu.</value>
 10         [description("主菜单高度,大于20的值"), category("自定义")]
 11         public int mainmenuheight
 12         {
 13             get { return mainmenuheight; }
 14             set
 15             {
 16                 if (value < 20)
 17                     return;
 18                 mainmenuheight = value;
 19                 this.panmenu.height = value;
 20             }
 21         }
 22         /// <summary>
 23         /// the expand height
 24         /// </summary>
 25         private int expandheight = 125;
 26         /// <summary>
 27         /// gets or sets the height of the expand.
 28         /// </summary>
 29         /// <value>the height of the expand.</value>
 30         [description("展开后高度"), category("自定义")]
 31         public int expandheight
 32         {
 33             get { return expandheight; }
 34             set { expandheight = value; }
 35         }
 36         /// <summary>
 37         /// the is expand
 38         /// </summary>
 39         private bool isexpand = true;
 40         /// <summary>
 41         /// gets or sets a value indicating whether this instance is expand.
 42         /// </summary>
 43         /// <value><c>true</c> if this instance is expand; otherwise, <c>false</c>.</value>
 44         [description("是否展开"), category("自定义")]
 45         public bool isexpand
 46         {
 47             get { return isexpand; }
 48             set
 49             {
 50                 isexpand = value;
 51                 if (value)
 52                 {
 53                     this.height = expandheight;
 54                     resetchildcontrol();
 55                 }
 56                 else
 57                 {
 58                     this.height = this.panmenu.height;
 59                     this.panchilds.controls.clear();
 60                 }
 61             }
 62         }
 63         /// <summary>
 64         /// occurs when [click itemed].
 65         /// </summary>
 66         [description("点击节点事件"), category("自定义")]
 67 
 68         public event eventhandler clickitemed;
 69         /// <summary>
 70         /// the select item
 71         /// </summary>
 72         private navigationmenuitemext selectitem = null;
 73 
 74         /// <summary>
 75         /// gets the select item.
 76         /// </summary>
 77         /// <value>the select item.</value>
 78         [description("选中的节点"), category("自定义")]
 79         public navigationmenuitemext selectitem
 80         {
 81             get { return selectitem; }
 82             private set { selectitem = value; }
 83         }
 84 
 85         /// <summary>
 86         /// the items
 87         /// </summary>
 88         navigationmenuitemext[] items;
 89 
 90         /// <summary>
 91         /// gets or sets the items.
 92         /// </summary>
 93         /// <value>the items.</value>
 94         [description("节点列表"), category("自定义")]
 95         public navigationmenuitemext[] items
 96         {
 97             get { return items; }
 98             set
 99             {
100                 items = value;
101                 reloadmenu();
102                 if (value != null && value.length > 0)
103                 {
104                     selectitem = value[0];
105                     resetchildcontrol();
106                 }
107             }
108         }
109         /// <summary>
110         /// the tip color
111         /// </summary>
112         private color tipcolor = color.fromargb(255, 87, 34);
113 
114         /// <summary>
115         /// gets or sets the color of the tip.
116         /// </summary>
117         /// <value>the color of the tip.</value>
118         [description("角标颜色"), category("自定义")]
119         public color tipcolor
120         {
121             get { return tipcolor; }
122             set { tipcolor = value; }
123         }
124 
125         /// <summary>
126         /// 获取或设置控件的前景色。
127         /// </summary>
128         /// <value>the color of the fore.</value>
129         /// <permissionset>
130         ///   <ipermission class="system.security.permissions.fileiopermission, mscorlib, version=2.0.3600.0, culture=neutral, publickeytoken=b77a5c561934e089" version="1" unrestricted="true" />
131         /// </permissionset>
132         public override system.drawing.color forecolor
133         {
134             get
135             {
136                 return base.forecolor;
137             }
138             set
139             {
140                 base.forecolor = value;
141                 foreach (control c in this.controls)
142                 {
143                     c.forecolor = value;
144                 }
145             }
146         }
147         /// <summary>
148         /// 获取或设置控件显示的文字的字体。
149         /// </summary>
150         /// <value>the font.</value>
151         /// <permissionset>
152         ///   <ipermission class="system.security.permissions.environmentpermission, mscorlib, version=2.0.3600.0, culture=neutral, publickeytoken=b77a5c561934e089" version="1" unrestricted="true" />
153         ///   <ipermission class="system.security.permissions.fileiopermission, mscorlib, version=2.0.3600.0, culture=neutral, publickeytoken=b77a5c561934e089" version="1" unrestricted="true" />
154         ///   <ipermission class="system.security.permissions.securitypermission, mscorlib, version=2.0.3600.0, culture=neutral, publickeytoken=b77a5c561934e089" version="1" flags="unmanagedcode, controlevidence" />
155         ///   <ipermission class="system.diagnostics.performancecounterpermission, system, version=2.0.3600.0, culture=neutral, publickeytoken=b77a5c561934e089" version="1" unrestricted="true" />
156         /// </permissionset>
157         public override font font
158         {
159             get
160             {
161                 return base.font;
162             }
163             set
164             {
165                 base.font = value;
166                 foreach (control c in this.controls)
167                 {
168                     c.font = value;
169                 }
170             }
171         }
172 
173         /// <summary>
174         /// the m lst anchors
175         /// </summary>
176         dictionary<navigationmenuitemext, frmanchor> m_lstanchors = new dictionary<navigationmenuitemext, frmanchor>();

一些事件及辅助函数,处理大小改变时候和重新加载以及鼠标对应处理

  1   /// <summary>
  2         /// handles the sizechanged event of the ucnavigationmenuoffice 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 ucnavigationmenuoffice_sizechanged(object sender, eventargs e)
  7         {
  8             if (isexpand)
  9             {
 10                 expandheight = this.height;
 11             }
 12         }
 13 
 14         /// <summary>
 15         /// resets the child control.
 16         /// </summary>
 17         public void resetchildcontrol()
 18         {
 19             if (isexpand)
 20             {
 21                 if (selectitem != null)
 22                 {
 23                     try
 24                     {
 25                         controlhelper.freezecontrol(this, true);
 26                         this.panchilds.controls.clear();
 27                         if (selectitem.showcontrol != null)
 28                         {
 29                             hzh_controls.controls.ucsplitline_h split = new ucsplitline_h();
 30                             split.backcolor = color.fromargb(50, 197, 197, 197);
 31                             split.dock = dockstyle.top;
 32                             this.panchilds.controls.add(split);
 33                             split.bringtofront();
 34                             this.panchilds.controls.add(selectitem.showcontrol);
 35                             selectitem.showcontrol.dock = dockstyle.fill;
 36                         }
 37                     }
 38                     finally
 39                     {
 40                         controlhelper.freezecontrol(this, false);
 41                     }
 42                 }
 43             }
 44         }
 45 
 46 
 47         /// <summary>
 48         /// reloads the menu.
 49         /// </summary>
 50         private void reloadmenu()
 51         {
 52             try
 53             {
 54                 controlhelper.freezecontrol(this, true);
 55                 this.panmenu.controls.clear();
 56                 if (items != null && items.length > 0)
 57                 {
 58                     foreach (var item in items)
 59                     {
 60                         var menu = (navigationmenuitemext)item;
 61                         label lbl = new label();
 62                         lbl.autosize = false;
 63                         lbl.textalign = contentalignment.middlecenter;
 64                         lbl.width = menu.itemwidth;
 65                         lbl.text = menu.text;
 66 
 67                         lbl.font = font;
 68                         lbl.forecolor = forecolor;
 69 
 70                         lbl.paint += lbl_paint;
 71                         lbl.mouseenter += lbl_mouseenter;
 72                         lbl.tag = menu;
 73                         lbl.click += lbl_click;
 74                         lbl.doubleclick += lbl_doubleclick;
 75                         if (menu.anchorright)
 76                         {
 77                             lbl.dock = dockstyle.right;
 78                         }
 79                         else
 80                         {
 81                             lbl.dock = dockstyle.left;
 82                         }
 83                         this.panmenu.controls.add(lbl);
 84 
 85                         lbl.bringtofront();
 86                     }
 87                 }
 88             }
 89             finally
 90             {
 91                 controlhelper.freezecontrol(this, false);
 92             }
 93         }
 94 
 95         /// <summary>
 96         /// handles the doubleclick event of the lbl control.
 97         /// </summary>
 98         /// <param name="sender">the source of the event.</param>
 99         /// <param name="e">the <see cref="eventargs"/> instance containing the event data.</param>
100         void lbl_doubleclick(object sender, eventargs e)
101         {
102             isexpand = !isexpand;
103         }
104         /// <summary>
105         /// handles the click event of the lbl control.
106         /// </summary>
107         /// <param name="sender">the source of the event.</param>
108         /// <param name="e">the <see cref="eventargs" /> instance containing the event data.</param>
109         void lbl_click(object sender, eventargs e)
110         {
111             label lbl = sender as label;
112             if (lbl.tag != null)
113             {
114                 var menu = (navigationmenuitemext)lbl.tag;
115                 if (menu.showcontrol == null)
116                 {
117                     selectitem = menu;
118 
119                     if (clickitemed != null)
120                     {
121                         clickitemed(this, e);
122                     }
123                 }
124                 else
125                 {
126                     if (isexpand)
127                     {
128                         selectitem = menu;
129                         resetchildcontrol();
130                     }
131                 }
132             }
133         }
134         /// <summary>
135         /// handles the mouseenter event of the lbl control.
136         /// </summary>
137         /// <param name="sender">the source of the event.</param>
138         /// <param name="e">the <see cref="eventargs" /> instance containing the event data.</param>
139         void lbl_mouseenter(object sender, eventargs e)
140         {
141             if (!isexpand)
142             {
143                 label lbl = sender as label;
144                 var menu = lbl.tag as navigationmenuitemext;
145                 foreach (var item in m_lstanchors)
146                 {
147                     m_lstanchors[item.key].hide();
148                 }
149                 if (menu.showcontrol != null)
150                 {
151                     if (!m_lstanchors.containskey(menu))
152                     {
153                         m_lstanchors[menu] = new frmanchor(panmenu, menu.showcontrol, isnotfocus: false);
154 
155                     }
156                     m_lstanchors[menu].backcolor = this.backcolor;
157                     m_lstanchors[menu].show(this);
158                     m_lstanchors[menu].size = new size(this.panchilds.width, expandheight - mainmenuheight);
159                 }
160             }
161         }
162         /// <summary>
163         /// handles the paint event of the lbl control.
164         /// </summary>
165         /// <param name="sender">the source of the event.</param>
166         /// <param name="e">the <see cref="painteventargs" /> instance containing the event data.</param>
167         void lbl_paint(object sender, painteventargs e)
168         {
169             label lbl = sender as label;
170             if (lbl.tag != null)
171             {
172                 var menu = (navigationmenuitemext)lbl.tag;
173                 e.graphics.setgdihigh();
174 
175                 if (menu.showtip)
176                 {
177                     if (!string.isnullorempty(menu.tiptext))
178                     {
179                         var rect = new rectangle(lbl.width - 25, lbl.height / 2 - 10, 20, 20);
180                         var path = rect.createroundedrectanglepath(5);
181                         e.graphics.fillpath(new solidbrush(tipcolor), path);
182                         e.graphics.drawstring(menu.tiptext, new font("微软雅黑", 8f), new solidbrush(color.white), rect, new stringformat() { alignment = stringalignment.center, linealignment = stringalignment.center });
183                     }
184                     else
185                     {
186                         e.graphics.fillellipse(new solidbrush(tipcolor), new rectangle(lbl.width - 20, lbl.height / 2 - 10, 10, 10));
187                     }
188                 }
189                 if (menu.icon != null)
190                 {
191                     e.graphics.drawimage(menu.icon, new rectangle(1, (lbl.height - 25) / 2, 25, 25), 0, 0, menu.icon.width, menu.icon.height, graphicsunit.pixel);
192                 }
193             }
194         }

完整代码

  1 // ***********************************************************************
  2 // assembly         : hzh_controls
  3 // created          : 2019-10-12
  4 //
  5 // ***********************************************************************
  6 // <copyright file="ucnavigationmenuoffice.cs">
  7 //     copyright by huang zhenghui(黄正辉) all, qq group:568015492 qq:623128629 email:623128629@qq.com
  8 // </copyright>
  9 //
 10 // blog: https://www.cnblogs.com/bfyx
 11 // github:https://github.com/kwwwvagaa/netwinformcontrol
 12 // gitee:https://gitee.com/kwwwvagaa/net_winform_custom_control.git
 13 //
 14 // if you use this code, please keep this note.
 15 // ***********************************************************************
 16 using system;
 17 using system.collections.generic;
 18 using system.componentmodel;
 19 using system.drawing;
 20 using system.data;
 21 using system.linq;
 22 using system.text;
 23 using system.windows.forms;
 24 using hzh_controls.forms;
 25 
 26 namespace hzh_controls.controls
 27 {
 28     /// <summary>
 29     /// class ucnavigationmenuoffice.
 30     /// implements the <see cref="system.windows.forms.usercontrol" />
 31     /// </summary>
 32     /// <seealso cref="system.windows.forms.usercontrol" />
 33     public partial class ucnavigationmenuoffice : usercontrol
 34     {
 35         /// <summary>
 36         /// the main menu height
 37         /// </summary>
 38         private int mainmenuheight = 25;
 39 
 40         /// <summary>
 41         /// gets or sets the height of the main menu.
 42         /// </summary>
 43         /// <value>the height of the main menu.</value>
 44         [description("主菜单高度,大于20的值"), category("自定义")]
 45         public int mainmenuheight
 46         {
 47             get { return mainmenuheight; }
 48             set
 49             {
 50                 if (value < 20)
 51                     return;
 52                 mainmenuheight = value;
 53                 this.panmenu.height = value;
 54             }
 55         }
 56         /// <summary>
 57         /// the expand height
 58         /// </summary>
 59         private int expandheight = 125;
 60         /// <summary>
 61         /// gets or sets the height of the expand.
 62         /// </summary>
 63         /// <value>the height of the expand.</value>
 64         [description("展开后高度"), category("自定义")]
 65         public int expandheight
 66         {
 67             get { return expandheight; }
 68             set { expandheight = value; }
 69         }
 70         /// <summary>
 71         /// the is expand
 72         /// </summary>
 73         private bool isexpand = true;
 74         /// <summary>
 75         /// gets or sets a value indicating whether this instance is expand.
 76         /// </summary>
 77         /// <value><c>true</c> if this instance is expand; otherwise, <c>false</c>.</value>
 78         [description("是否展开"), category("自定义")]
 79         public bool isexpand
 80         {
 81             get { return isexpand; }
 82             set
 83             {
 84                 isexpand = value;
 85                 if (value)
 86                 {
 87                     this.height = expandheight;
 88                     resetchildcontrol();
 89                 }
 90                 else
 91                 {
 92                     this.height = this.panmenu.height;
 93                     this.panchilds.controls.clear();
 94                 }
 95             }
 96         }
 97         /// <summary>
 98         /// occurs when [click itemed].
 99         /// </summary>
100         [description("点击节点事件"), category("自定义")]
101 
102         public event eventhandler clickitemed;
103         /// <summary>
104         /// the select item
105         /// </summary>
106         private navigationmenuitemext selectitem = null;
107 
108         /// <summary>
109         /// gets the select item.
110         /// </summary>
111         /// <value>the select item.</value>
112         [description("选中的节点"), category("自定义")]
113         public navigationmenuitemext selectitem
114         {
115             get { return selectitem; }
116             private set { selectitem = value; }
117         }
118 
119         /// <summary>
120         /// the items
121         /// </summary>
122         navigationmenuitemext[] items;
123 
124         /// <summary>
125         /// gets or sets the items.
126         /// </summary>
127         /// <value>the items.</value>
128         [description("节点列表"), category("自定义")]
129         public navigationmenuitemext[] items
130         {
131             get { return items; }
132             set
133             {
134                 items = value;
135                 reloadmenu();
136                 if (value != null && value.length > 0)
137                 {
138                     selectitem = value[0];
139                     resetchildcontrol();
140                 }
141             }
142         }
143         /// <summary>
144         /// the tip color
145         /// </summary>
146         private color tipcolor = color.fromargb(255, 87, 34);
147 
148         /// <summary>
149         /// gets or sets the color of the tip.
150         /// </summary>
151         /// <value>the color of the tip.</value>
152         [description("角标颜色"), category("自定义")]
153         public color tipcolor
154         {
155             get { return tipcolor; }
156             set { tipcolor = value; }
157         }
158 
159         /// <summary>
160         /// 获取或设置控件的前景色。
161         /// </summary>
162         /// <value>the color of the fore.</value>
163         /// <permissionset>
164         ///   <ipermission class="system.security.permissions.fileiopermission, mscorlib, version=2.0.3600.0, culture=neutral, publickeytoken=b77a5c561934e089" version="1" unrestricted="true" />
165         /// </permissionset>
166         public override system.drawing.color forecolor
167         {
168             get
169             {
170                 return base.forecolor;
171             }
172             set
173             {
174                 base.forecolor = value;
175                 foreach (control c in this.controls)
176                 {
177                     c.forecolor = value;
178                 }
179             }
180         }
181         /// <summary>
182         /// 获取或设置控件显示的文字的字体。
183         /// </summary>
184         /// <value>the font.</value>
185         /// <permissionset>
186         ///   <ipermission class="system.security.permissions.environmentpermission, mscorlib, version=2.0.3600.0, culture=neutral, publickeytoken=b77a5c561934e089" version="1" unrestricted="true" />
187         ///   <ipermission class="system.security.permissions.fileiopermission, mscorlib, version=2.0.3600.0, culture=neutral, publickeytoken=b77a5c561934e089" version="1" unrestricted="true" />
188         ///   <ipermission class="system.security.permissions.securitypermission, mscorlib, version=2.0.3600.0, culture=neutral, publickeytoken=b77a5c561934e089" version="1" flags="unmanagedcode, controlevidence" />
189         ///   <ipermission class="system.diagnostics.performancecounterpermission, system, version=2.0.3600.0, culture=neutral, publickeytoken=b77a5c561934e089" version="1" unrestricted="true" />
190         /// </permissionset>
191         public override font font
192         {
193             get
194             {
195                 return base.font;
196             }
197             set
198             {
199                 base.font = value;
200                 foreach (control c in this.controls)
201                 {
202                     c.font = value;
203                 }
204             }
205         }
206 
207         /// <summary>
208         /// the m lst anchors
209         /// </summary>
210         dictionary<navigationmenuitemext, frmanchor> m_lstanchors = new dictionary<navigationmenuitemext, frmanchor>();
211         /// <summary>
212         /// initializes a new instance of the <see cref="ucnavigationmenuoffice"/> class.
213         /// </summary>
214         public ucnavigationmenuoffice()
215         {
216             initializecomponent();
217             this.sizechanged += ucnavigationmenuoffice_sizechanged;
218             items = new navigationmenuitemext[0];
219             if (controlhelper.isdesignmode())
220             {
221                 items = new navigationmenuitemext[4];
222                 for (int i = 0; i < 4; i++)
223                 {
224                     items[i] = new navigationmenuitemext()
225                     {
226                         text = "菜单" + (i + 1),
227                         anchorright = i >= 2
228                     };
229                 }
230             }
231         }
232 
233         /// <summary>
234         /// handles the sizechanged event of the ucnavigationmenuoffice control.
235         /// </summary>
236         /// <param name="sender">the source of the event.</param>
237         /// <param name="e">the <see cref="eventargs"/> instance containing the event data.</param>
238         void ucnavigationmenuoffice_sizechanged(object sender, eventargs e)
239         {
240             if (isexpand)
241             {
242                 expandheight = this.height;
243             }
244         }
245 
246         /// <summary>
247         /// resets the child control.
248         /// </summary>
249         public void resetchildcontrol()
250         {
251             if (isexpand)
252             {
253                 if (selectitem != null)
254                 {
255                     try
256                     {
257                         controlhelper.freezecontrol(this, true);
258                         this.panchilds.controls.clear();
259                         if (selectitem.showcontrol != null)
260                         {
261                             hzh_controls.controls.ucsplitline_h split = new ucsplitline_h();
262                             split.backcolor = color.fromargb(50, 197, 197, 197);
263                             split.dock = dockstyle.top;
264                             this.panchilds.controls.add(split);
265                             split.bringtofront();
266                             this.panchilds.controls.add(selectitem.showcontrol);
267                             selectitem.showcontrol.dock = dockstyle.fill;
268                         }
269                     }
270                     finally
271                     {
272                         controlhelper.freezecontrol(this, false);
273                     }
274                 }
275             }
276         }
277 
278 
279         /// <summary>
280         /// reloads the menu.
281         /// </summary>
282         private void reloadmenu()
283         {
284             try
285             {
286                 controlhelper.freezecontrol(this, true);
287                 this.panmenu.controls.clear();
288                 if (items != null && items.length > 0)
289                 {
290                     foreach (var item in items)
291                     {
292                         var menu = (navigationmenuitemext)item;
293                         label lbl = new label();
294                         lbl.autosize = false;
295                         lbl.textalign = contentalignment.middlecenter;
296                         lbl.width = menu.itemwidth;
297                         lbl.text = menu.text;
298 
299                         lbl.font = font;
300                         lbl.forecolor = forecolor;
301 
302                         lbl.paint += lbl_paint;
303                         lbl.mouseenter += lbl_mouseenter;
304                         lbl.tag = menu;
305                         lbl.click += lbl_click;
306                         lbl.doubleclick += lbl_doubleclick;
307                         if (menu.anchorright)
308                         {
309                             lbl.dock = dockstyle.right;
310                         }
311                         else
312                         {
313                             lbl.dock = dockstyle.left;
314                         }
315                         this.panmenu.controls.add(lbl);
316 
317                         lbl.bringtofront();
318                     }
319                 }
320             }
321             finally
322             {
323                 controlhelper.freezecontrol(this, false);
324             }
325         }
326 
327         /// <summary>
328         /// handles the doubleclick event of the lbl control.
329         /// </summary>
330         /// <param name="sender">the source of the event.</param>
331         /// <param name="e">the <see cref="eventargs"/> instance containing the event data.</param>
332         void lbl_doubleclick(object sender, eventargs e)
333         {
334             isexpand = !isexpand;
335         }
336         /// <summary>
337         /// handles the click event of the lbl control.
338         /// </summary>
339         /// <param name="sender">the source of the event.</param>
340         /// <param name="e">the <see cref="eventargs" /> instance containing the event data.</param>
341         void lbl_click(object sender, eventargs e)
342         {
343             label lbl = sender as label;
344             if (lbl.tag != null)
345             {
346                 var menu = (navigationmenuitemext)lbl.tag;
347                 if (menu.showcontrol == null)
348                 {
349                     selectitem = menu;
350 
351                     if (clickitemed != null)
352                     {
353                         clickitemed(this, e);
354                     }
355                 }
356                 else
357                 {
358                     if (isexpand)
359                     {
360                         selectitem = menu;
361                         resetchildcontrol();
362                     }
363                 }
364             }
365         }
366         /// <summary>
367         /// handles the mouseenter event of the lbl control.
368         /// </summary>
369         /// <param name="sender">the source of the event.</param>
370         /// <param name="e">the <see cref="eventargs" /> instance containing the event data.</param>
371         void lbl_mouseenter(object sender, eventargs e)
372         {
373             if (!isexpand)
374             {
375                 label lbl = sender as label;
376                 var menu = lbl.tag as navigationmenuitemext;
377                 foreach (var item in m_lstanchors)
378                 {
379                     m_lstanchors[item.key].hide();
380                 }
381                 if (menu.showcontrol != null)
382                 {
383                     if (!m_lstanchors.containskey(menu))
384                     {
385                         m_lstanchors[menu] = new frmanchor(panmenu, menu.showcontrol, isnotfocus: false);
386 
387                     }
388                     m_lstanchors[menu].backcolor = this.backcolor;
389                     m_lstanchors[menu].show(this);
390                     m_lstanchors[menu].size = new size(this.panchilds.width, expandheight - mainmenuheight);
391                 }
392             }
393         }
394         /// <summary>
395         /// handles the paint event of the lbl control.
396         /// </summary>
397         /// <param name="sender">the source of the event.</param>
398         /// <param name="e">the <see cref="painteventargs" /> instance containing the event data.</param>
399         void lbl_paint(object sender, painteventargs e)
400         {
401             label lbl = sender as label;
402             if (lbl.tag != null)
403             {
404                 var menu = (navigationmenuitemext)lbl.tag;
405                 e.graphics.setgdihigh();
406 
407                 if (menu.showtip)
408                 {
409                     if (!string.isnullorempty(menu.tiptext))
410                     {
411                         var rect = new rectangle(lbl.width - 25, lbl.height / 2 - 10, 20, 20);
412                         var path = rect.createroundedrectanglepath(5);
413                         e.graphics.fillpath(new solidbrush(tipcolor), path);
414                         e.graphics.drawstring(menu.tiptext, new font("微软雅黑", 8f), new solidbrush(color.white), rect, new stringformat() { alignment = stringalignment.center, linealignment = stringalignment.center });
415                     }
416                     else
417                     {
418                         e.graphics.fillellipse(new solidbrush(tipcolor), new rectangle(lbl.width - 20, lbl.height / 2 - 10, 10, 10));
419                     }
420                 }
421                 if (menu.icon != null)
422                 {
423                     e.graphics.drawimage(menu.icon, new rectangle(1, (lbl.height - 25) / 2, 25, 25), 0, 0, menu.icon.width, menu.icon.height, graphicsunit.pixel);
424                 }
425             }
426         }
427     }
428 }

 

最后的话

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