c#实现可折叠导航栏.docx
《c#实现可折叠导航栏.docx》由会员分享,可在线阅读,更多相关《c#实现可折叠导航栏.docx(27页珍藏版)》请在冰豆网上搜索。
c#实现可折叠导航栏
先上张效果图,依次为全展开图,部分折叠图,全部折叠图
时间仓促,功能相对简单,也未经过详细测试,不支持设计期操作,这里提供思路给大家,有时间完善吧,上代码:
代码文件介绍
NavBar.cs 导航栏主体,继承自Panel
NavGroup.cs NavBar中的分组,即(控制面板,我的电脑等),继承自Control
NavBarItem.cs 分组中的小项(即区域选项,字体等),继承自Control
NavBarButton.cs 导航栏主体右边的圆形按钮
NavGroupCollection.cs 分组的集合
NavBarItemCollection.cs 分组中小项的集合
NavGroupState.cs 组的状态,收缩还是展开
NavBar.cs
[csharp] viewplaincopy
1.using System;
2.using System.Collections.Generic;
3.using System.Drawing.Design;
4.using System.ComponentModel.Design;
5.using System.ComponentModel;
6.using System.Data;
7.using System.Drawing;
8.using System.Linq;
9.using System.Text;
10.using System.Windows.Forms;
11.
12.namespace WindowsApplication1
13.{
14. public partial class NavBar :
Panel
15. {
16. private NavGroupCollection _groups;
17. public NavGroupCollection Groups
18. {
19. get { return this._groups; }
20. }
21. public NavGroup this[int index]
22. {
23. get
24. {
25. if (index > -1)
26. return this._groups[index];
27. else
28. return null;
29. }
30. }
31. private int _selectedIndex = -1;
32. ///
33. /// 选择组的索引
34. ///
35. [DefaultValue(-1)]
36. public int SelectedIndex
37. {
38. get { return this._selectedIndex; }
39. set
40. {
41. this._selectedIndex = value;
42. this.SelectGroup(value);
43. }
44. }
45. private int _groupSpace = 20;
46. ///
47. /// Group间距
48. ///
49. public int GroupSpace
50. {
51. get { return this._groupSpace; }
52. set { this._groupSpace = value; }
53. }
54. private int _groupMargin = 5;
55. ///
56. /// Group边距
57. ///
58. public int GroupMargin
59. {
60. get { return this._groupMargin; }
61. set { this._groupMargin = value; }
62. }
63. private ImageList _smallImageList;
64. ///
65. /// 设置图像列表
66. ///
67. public ImageList SmallImageList
68. {
69. get { return this._smallImageList; }
70. set { this._smallImageList = value; }
71. }
72. public NavBar()
73. {
74. InitializeComponent();
75.
76. this.BackColor = Color.FromArgb(112, 140, 225);
77. this._groups = new NavGroupCollection(this);
78. this.AutoScroll = true;
79. }
80. ///
81. /// 根据添加的项,布局
82. ///
83. ///
84. public void SetLayOut(NavGroup item)
85. {
86. this.SuspendLayout();
87. this.Controls.Add(item);
88. if (this._groups.Count == 0)
89. {
90. item.Top = 10;
91. }
92. else
93. {
94. item.Top = this[this._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. public void SetLayOut()
106. {
107. for (int i = 0; i < this._groups.Count; i++)
108. {
109. if (i == 0)
110. this._groups[i].Top = 10;
111. else
112. {
113. this._groups[i].Top = this._groups[i - 1].Bottom + this._groupSpace;
114. this._groups[i].GroupIndex = i;
115. }
116. }
117. }
118. ///
119. /// 添加分组
120. ///
121. ///
122. public NavGroup AddGroup()
123. {
124. this._groups.Add();
125. return this._groups[this._groups.Count - 1];
126. }
127. ///
128. /// 删除分组
129. ///
130. ///
131. public void RemoveGroup(NavGroup item)
132. {
133. int i = item.GroupIndex;
134. this._groups.Remove(item);
135. this.Controls.Remove(item);
136. this.SetLayOut();
137. }
138. ///
139. /// 删除指定索引的分组
140. ///
141. ///
142. public void RemoveGroupAt(int index)
143. {
144. this.Controls.Remove(this._groups[index]);
145. this._groups.RemoveAt(index);
146. this.SetLayOut();
147. }
148. ///
149. /// 选中指定索引的分组
150. ///
151. ///
152. private void SelectGroup(int index)
153. {
154. foreach (NavGroup g in this._groups)
155. {
156. if (g.GroupIndex == this._selectedIndex) continue;
157. g.IsSelected = false;
158. }
159. }
160.
161. protected override void OnPaint(PaintEventArgs e)
162. {
163. base.OnPaint(e);
164. }
165. }
166.}
NavGroup.cs
[csharp] viewplaincopy
1.using System;
2.using System.Collections.Generic;
3.using System.ComponentModel;
4.using System.Data;
5.using System.Drawing;
6.using System.Drawing.Drawing2D;
7.using System.Linq;
8.using System.Text;
9.using System.Windows.Forms;
10.
11.namespace WindowsApplication1
12.{
13. public partial class NavGroup :
Control
14. {
15. private Rectangle _titleRectangle;
16. private NavBar _ownerBar;
17. private NavBarButton _button;
18. private NavBarItemCollection _items;
19. public NavBarItemCollection Items
20. {
21. get { return this._items; }
22. set { this._items = value; }
23. }
24. public NavBarItem this[int index]
25. {
26. get
27. {
28. if (index > -1)
29. return this._items[index];
30. else
31. return null;
32. }
33. }
34. private NavGroupState _groupState = NavGroupState.expand;
35. ///
36. /// 组的状态
37. ///
38. public NavGroupState GroupState
39. {
40. get { return this._groupState; }
41. set
42. {
43. this._groupState = value;
44. this.SetGroupState(value);
45. }
46. }
47. private int _groupIndex;
48. ///
49. /// 组索引
50. ///
51. public int GroupIndex
52. {
53. get { return this._groupIndex; }
54. set { this._groupIndex = value; }
55. }
56. private int _titleHeight = 20;
57. ///
58. /// 标题高度
59. ///
60. public int TitleHeight
61. {
62. get { return this._titleHeight; }
63. set
64. {
65. this._titleHeight = value;
66. this.SetTitleRectangle();
67. }
68. }
69. private string _title;
70. ///
71. /// 标题
72. ///
73. public string Title
74. {
75. get { return this._title; }
76. set { this._title = value; }
77. }
78. private Color _titleStartColor = Color.White;
79. ///
80. /// 标题渐变开始色
81. ///
82. public Color TitleStartColor
83. {
84. get { return this._titleStartColor; }
85. set
86. {
87. this._titleStartColor = value;
88. }
89. }
90. private Color _titleEndColor = Color.FromArgb(199, 211, 247);
91. ///
92. /// 标题渐变结束色
93. ///
94. public Color TitleEndColor
95. {
96. get { return this._titleEndColor; }
97. set
98. {
99. this._titleEndColor = value;
100. }
101. }
102. private int _itemSpace = 5;
103. ///
104. /// Item间距
105. ///
106. public int ItempSpace
107. {
108. get { return this._itemSpace; }
109. set { this._itemSpace = value; }
110. }
111. p