前提
入行已经7,8年了,一直想做一套漂亮点的自定义控件,于是就有了本系列文章。
GitHub:https://github.com/kwwwvagaa/NetWinformControl
码云:https://gitee.com/kwwwvagaa/net_winform_custom_control.git
如果觉得写的还行,请点个 star 支持一下吧
来都来了,点个【推荐】再走吧,谢谢
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, Public