c#实现可折叠导航栏.docx_第1页
c#实现可折叠导航栏.docx_第2页
c#实现可折叠导航栏.docx_第3页
c#实现可折叠导航栏.docx_第4页
c#实现可折叠导航栏.docx_第5页
已阅读5页,还剩15页未读 继续免费阅读

下载本文档

版权说明:本文档由用户提供并上传,收益归属内容提供方,若内容存在侵权,请进行举报或认领

文档简介

先上张效果图,依次为 全展开图,部分折叠图,全部折叠图时间仓促,功能相对简单,也未经过详细测试,不支持设计期操作,这里提供思路给大家,有时间完善吧,上代码:代码文件介绍NavBar.cs 导航栏主体,继承自 PanelNavGroup.cs NavBar中的分组,即(控制面板,我的电脑等),继承自ControlNavBarItem.cs 分组中的小项(即区域选项,字体等),继承自ControlNavBarButton.cs 导航栏主体右边的圆形按钮NavGroupCollection.cs 分组的集合NavBarItemCollection.cs 分组中小项的集合NavGroupState.cs 组的状态,收缩还是展开NavBar.cscsharpview plaincopy1. usingSystem;2. usingSystem.Collections.Generic;3. usingSystem.Drawing.Design;4. usingSystem.ComponentModel.Design;5. usingSystem.ComponentModel;6. usingSystem.Data;7. usingSystem.Drawing;8. usingSystem.Linq;9. usingSystem.Text;10. usingSystem.Windows.Forms;11. 12. namespaceWindowsApplication113. 14. publicpartialclassNavBar:Panel15. 16. privateNavGroupCollection_groups;17. publicNavGroupCollectionGroups18. 19. getreturnthis._groups;20. 21. publicNavGroupthisintindex22. 23. get24. 25. if(index-1)26. returnthis._groupsindex;27. else28. returnnull;29. 30. 31. privateint_selectedIndex=-1;32. /33. /选择组的索引34. /35. DefaultValue(-1)36. publicintSelectedIndex37. 38. getreturnthis._selectedIndex;39. set40. 41. this._selectedIndex=value;42. this.SelectGroup(value);43. 44. 45. privateint_groupSpace=20;46. /47. /Group间距48. /49. publicintGroupSpace50. 51. getreturnthis._groupSpace;52. setthis._groupSpace=value;53. 54. privateint_groupMargin=5;55. /56. /Group边距57. /58. publicintGroupMargin59. 60. getreturnthis._groupMargin;61. setthis._groupMargin=value;62. 63. privateImageList_smallImageList;64. /65. /设置图像列表66. /67. publicImageListSmallImageList68. 69. getreturnthis._smallImageList;70. setthis._smallImageList=value;71. 72. publicNavBar()73. 74. InitializeComponent();75. 76. this.BackColor=Color.FromArgb(112,140,225);77. this._groups=newNavGroupCollection(this);78. this.AutoScroll=true;79. 80. /81. /根据添加的项,布局82. /83. /84. publicvoidSetLayOut(NavGroupitem)85. 86. this.SuspendLayout();87. this.Controls.Add(item);88. if(this._groups.Count=0)89. 90. item.Top=10;91. 92. else93. 94. item.Top=thisthis._groups.Count-1.Bottom+this.GroupSpace;95. 96. item.Width=this.Width-2*this.GroupMargin;97. item.Left=(this.Width-item.Width)/2;98. item.Height=item.TitleHeight;99. this.ResumeLayout();100. /item.Anchor=AnchorStyles.Top|AnchorStyles.Left|AnchorStyles.Right;101. 102. /103. /重新布局组件104. /105. publicvoidSetLayOut()106. 107. for(inti=0;ithis._groups.Count;i+)108. 109. if(i=0)110. this._groupsi.Top=10;111. else112. 113. this._groupsi.Top=this._groupsi-1.Bottom+this._groupSpace;114. this._groupsi.GroupIndex=i;115. 116. 117. 118. /119. /添加分组120. /121. /122. publicNavGroupAddGroup()123. 124. this._groups.Add();125. returnthis._groupsthis._groups.Count-1;126. 127. /128. /删除分组129. /130. /131. publicvoidRemoveGroup(NavGroupitem)132. 133. inti=item.GroupIndex;134. this._groups.Remove(item);135. this.Controls.Remove(item);136. this.SetLayOut();137. 138. /139. /删除指定索引的分组140. /141. /142. publicvoidRemoveGroupAt(intindex)143. 144. this.Controls.Remove(this._groupsindex);145. this._groups.RemoveAt(index);146. this.SetLayOut();147. 148. /149. /选中指定索引的分组150. /151. /152. privatevoidSelectGroup(intindex)153. 154. foreach(NavGroupginthis._groups)155. 156. if(g.GroupIndex=this._selectedIndex)continue;157. g.IsSelected=false;158. 159. 160. 161. protectedoverridevoidOnPaint(PaintEventArgse)162. 163. base.OnPaint(e);164. 165. 166. NavGroup.cscsharpview plaincopy1. usingSystem;2. usingSystem.Collections.Generic;3. usingSystem.ComponentModel;4. usingSystem.Data;5. usingSystem.Drawing;6. usingSystem.Drawing.Drawing2D;7. usingSystem.Linq;8. usingSystem.Text;9. usingSystem.Windows.Forms;10. 11. namespaceWindowsApplication112. 13. publicpartialclassNavGroup:Control14. 15. privateRectangle_titleRectangle;16. privateNavBar_ownerBar;17. privateNavBarButton_button;18. privateNavBarItemCollection_items;19. publicNavBarItemCollectionItems20. 21. getreturnthis._items;22. setthis._items=value;23. 24. publicNavBarItemthisintindex25. 26. get27. 28. if(index-1)29. returnthis._itemsindex;30. else31. returnnull;32. 33. 34. privateNavGroupState_groupState=NavGroupState.expand;35. /36. /组的状态37. /38. publicNavGroupStateGroupState39. 40. getreturnthis._groupState;41. set42. 43. this._groupState=value;44. this.SetGroupState(value);45. 46. 47. privateint_groupIndex;48. /49. /组索引50. /51. publicintGroupIndex52. 53. getreturnthis._groupIndex;54. setthis._groupIndex=value;55. 56. privateint_titleHeight=20;57. /58. /标题高度59. /60. publicintTitleHeight61. 62. getreturnthis._titleHeight;63. set64. 65. this._titleHeight=value;66. this.SetTitleRectangle();67. 68. 69. privatestring_title;70. /71. /标题72. /73. publicstringTitle74. 75. getreturnthis._title;76. setthis._title=value;77. 78. privateColor_titleStartColor=Color.White;79. /80. /标题渐变开始色81. /82. publicColorTitleStartColor83. 84. getreturnthis._titleStartColor;85. set86. 87. this._titleStartColor=value;88. 89. 90. privateColor_titleEndColor=Color.FromArgb(199,211,247);91. /92. /标题渐变结束色93. /94. publicColorTitleEndColor95. 96. getreturnthis._titleEndColor;97. set98. 99. this._titleEndColor=value;100. 101. 102. privateint_itemSpace=5;103. /104. /Item间距105. /106. publicintItempSpace107. 108. getreturnthis._itemSpace;109. setthis._itemSpace=value;110. 111. privateint_itemMargin=5;112. /113. /Item边距114. /115. publicintItemMargin116. 117. getreturnthis._itemMargin;118. setthis._itemMargin=value;119. 120. privatebool_isSelected=false;121. /122. /是否选中123. /124. publicboolIsSelected125. 126. getreturnthis._isSelected;127. set128. 129. this._isSelected=value;130. this.Invalidate();131. 132. 133. privateImageList_smallImageList;134. publicImageListSmallImageList135. 136. getreturnthis._smallImageList;137. setthis._smallImageList=value;138. 139. 140. publicNavGroup()141. 142. InitializeComponent();143. this._title=新建组;144. this.BackColor=Color.FromArgb(214,223,247);145. SetTitleRectangle();146. this._button=newNavBarButton(this);147. this._button.Click+=newEventHandler148. (149. delegate(objectsender,EventArgse)150. 151. if(this._groupState=NavGroupState.collapse)152. this.GroupState=NavGroupState.expand;153. else154. this.GroupState=NavGroupState.collapse;155. );156. this._items=newNavBarItemCollection(this);157. 158. publicNavGroup(NavBarownerbar):this()159. 160. this._ownerBar=ownerbar;161. 162. /163. /设置标题区域164. /165. privatevoidSetTitleRectangle()166. 167. this._titleRectangle=newRectangle(0,0,this.Width,_titleHeight);168. 169. /170. /添加项目171. /172. /173. publicNavBarItemAddItem()174. 175. this._items.Add();176. returnthis._itemsthis._items.Count-1;177. 178. /179. /设置组状态180. /181. /182. privatevoidSetGroupState(NavGroupStatevalue)183. 184. if(value=NavGroupState.collapse)185. 186. this.Height=this._titleHeight;187. 188. else189. 190. if(this._items.Count0)191. 192. this.Height=this._itemsthis._items.Count-1.Bottom+this._itemSpace;193. 194. 195. this._ownerBar.SetLayOut();196. 197. /198. /根据新增项布局199. /200. /201. publicvoidSetLayOut(NavBarItemitem)202. 203. this.SuspendLayout();204. this.Controls.Add(item);205. if(this._items.Count=0)206. 207. item.Top=this._titleHeight+10;208. 209. else210. 211. item.Top=thisthis._items.Count-1.Bottom+this.ItempSpace;212. 213. item.Width=this.Width-2*this.ItemMargin;214. item.Left=(this.Width-item.Width)/2;215. this.Height=item.Bottom+this.ItempSpace;216. this._ownerBar.SetLayOut();217. this.ResumeLayout();218. 219. /220. /重新布局,这个需要完善221. /222. /223. publicvoidSetLayOut(intindex)224. 225. for(inti=index+1;ithis._items.Count;i+)226. 227. this._itemsi.Top=this._itemsi-1.Bottom+this._itemSpace;228. 229. 230. 231. protectedoverridevoidOnClick(EventArgse)232. 233. base.OnClick(e);234. this.IsSelected=true;235. this._ownerBar.SelectedIndex=this._groupIndex;236. 237. protectedoverridevoidOnMouseUp(MouseEventArgse)238. 239. base.OnMouseUp(e);240. if(this._button.ClientRectangle.Contains(e.Location)241. 242. this._button.DoClick();243. this.Invalidate();244. 245. 246. protectedoverridevoidOnPaint(PaintEventArgse)247. 248. base.OnPaint(e);249. SizeFsize=e.Graphics.MeasureString(this._title,this.Font);250. Fonttitlefont=newFont(this.Font.FontFamily,this.Font.Size,this.Font.Style|FontStyle.Bold);251. /未选中252. if(!this._isSelected)253. 254. LinearGradientBrushbrush=newLinearGradientBrush(this._titleRectangle,this._titleStartColor,this._titleEndColor,0f);255. e.Graphics.FillRectangle(brush,this._titleRectangle);256. e.Graphics.DrawString(this._title,titlefont,Brushes.Black,this._titleRectangle.X,this._titleRectangle.Top+(this._titleRectangle.Height-size.Height)/2);257. 258. else259. 260. e.Graphics.FillRectangle(newSolidBrush(Color.FromArgb(35,90,200),this._titleRectangle);261. e.Graphics.DrawString(this._title,titlefont,Brushes.White,this._titleRectangle.X,this._titleRectangle.Top+(this._titleRectangle.Height-size.Height)/2);262. 263. e.Graphics.DrawRectangle(Pens.White,newRectangle(0,0,this.Width-1,this.Height-1);264. /绘制右侧原型按钮265. this._button.Draw(e.Graphics);266. 267. 268. protectedoverridevoidOnResize(EventArgse)269. 270. base.OnResize(e);271. SetTitleRectangle();272. this._button.SetClientRectangle(this._titleRectangle);273. 274. 275. NavBarItem.cs,这里偷了个懒,该组件包含一个PictureBox和Label组件,PictureBox用来显示图标,Lable用来显示文字csharpview plaincopy1. usingSystem;2. usingSystem.Collections.Generic;3. usingSystem.ComponentModel;4. usingSystem.Diagnostics;5. usingSystem.Linq;6. usingSystem.Text;7. usingSystem.Windows.Forms;8. usingSystem.Drawing;9. 10. namespaceWindowsApplication111. 12. publicpartialclassNavBarItem:Control13. 14. /15. /供外部调用项的点击事件16. /17. Browsable(false)18. publiceventEventHandlerItemClick;19. 20. privateRectangle_imageRectangle;21. privatePictureBox_picbox;22. privateLabel_titlebox;23. 24. privateNavGroup_ownerGroup;25. /26. /所属组27. /28. publicNavGroupOwnerGroup29. 30. getreturnthis._ownerGroup;31. setthis._ownerGroup=value;32. 33. privateint_itemIndex;34

温馨提示

  • 1. 本站所有资源如无特殊说明,都需要本地电脑安装OFFICE2007和PDF阅读器。图纸软件为CAD,CAXA,PROE,UG,SolidWorks等.压缩文件请下载最新的WinRAR软件解压。
  • 2. 本站的文档不包含任何第三方提供的附件图纸等,如果需要附件,请联系上传者。文件的所有权益归上传用户所有。
  • 3. 本站RAR压缩包中若带图纸,网页内容里面会有图纸预览,若没有图纸预览就没有图纸。
  • 4. 未经权益所有人同意不得将文件中的内容挪作商业或盈利用途。
  • 5. 人人文库网仅提供信息存储空间,仅对用户上传内容的表现方式做保护处理,对用户上传分享的文档内容本身不做任何修改或编辑,并不能对任何下载内容负责。
  • 6. 下载文件中如有侵权或不适当内容,请与我们联系,我们立即纠正。
  • 7. 本站不保证下载资源的准确性、安全性和完整性, 同时也不承担用户因使用这些下载资源对自己和他人造成任何形式的伤害或损失。

评论

0/150

提交评论