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

本文介绍如何创建一个C# Winform自定义控件,模仿Office菜单的样式。通过提供GitHub和码云的源码链接,作者分享了实现这一控件的过程,包括设置属性、处理事件和调整布局。感兴趣的读者可以加入作者的企鹅群进行交流讨论。
摘要由CSDN通过智能技术生成

前提

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

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

码云:https://gitee.com/kwwwvagaa/net_winform_custom_control.git

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

欢迎前来交流探讨: 企鹅群568015492 企鹅群568015492_www.wityx.com

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

NuGet

Install-Package HZH_Controls

目录

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

用处及效果

准备工作

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

开始

添加一个用户控件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=b77a5c561934
  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值