拆分器控件Splitcontainer ,是一个含有Splitter拆分条的容器,它包含两个面板容器Panel1,Panel2,可以移动拆分条,对面板大小进行控制!
控件学习示例程序!
属性介绍;
//拆分条的是否启用禁用
bool IsSplitterFixed{get;set;}
bool类型,true:不能调节拆分条; false :能够使用拆分条.
决定是否可以通过键盘或鼠标来移动拆分器。
//拆分器的方向;垂直和水平方向上拆分控件,上下和左右两块面板
Orientation Orientation{get;set;}
Orientation是枚举;包含两个值;水平与垂直; Vertical ; Horizontal ;
//获取拆分条(器)的大小
Rectangle SplitterRectangle{get;}
取得拆分条的矩形表示形式;new Rectangle(X,Y width,height)拆分器就是指拆分条,坐标点,宽高
拆分条,是一个矩形表示的!
该属性的变化,与Orientation属性相关!垂直,水平显示状态,拆分条的位置大小当然不同!
//指定拆分器面板之一保持不变
调节拆分器,使用panel1或panel2面板不会改变大小;
FixedPanel FixedPanel{get;set;}
FixedPanel枚举: 有三个值;None(默认值), Panel1 ,Panel2;
确定调整SplitterContainer控件后,哪个面板将保持不变!调整拆器容器大小时,可以保持哪个面板不变时,
才设置此属性,如果容器不会改变,也不用设置此属性!
//设置拆分器离容器的初始大小,拆分条左或上边缘离容器的初始宽度。
int SplitterDistance{get;set;}
像素为单位,默认值为40 ,在移动拆分器时,这个值是变化的,计算左边缘或上边缘的距离!
拆分器的初始距离.
//设置拖动拆分条(器)移动的增量大小,
也就是拖移的速度!同样的拖动,移动的宽度可不相同。
int SplitterIncrement{get;set;}
获取设置拆分条移动的增量,默认值为1;
测试SplitterIncrement属性值的大小,可以通接受键盘上下键的,调节拆分器,IsSplitterFixed必须为true
//设置拆分条的宽度
int SplitterWidth{get;set;}
默认值为4 ,通过设置可以更改此值,使拆分条的宽度变粗!
// 获取设置拆分器与panel1的左边缘,或上边缘的距离固定大小 ;与边缘的默认大小为25
int Panel1MinSize{get;set;}
int Panel2MinSize{get;set;}
此属性作用比较大,可控制拆分器的活动范围大小,也就是左右面板中固定的大小面积,拆分器不能
进行拖放进入的区域。
把该属性设置为0;表示拆分器可以到过缘。
//设置拆分器的Panel1 Panel2折叠或展开;
bool Panel1Collapsed{get;set;}
bool Panel2Collapsed{get;set;}
注意:Panel1Collapsed属性,true为拆叠,false展开为默认值,
在拆叠状态下,不能设置SplitterDistance,SplitterWidth属性,这时的拆分器处了隐藏关闭状态,
必须在展开时才能设置这些属性!
//Pane1 与Pane2属性 拆分器的两个面板容器
SplitterPanel Panel1{get}
SplitterPanel Panel2{get;}
SplitterPanel:Panel类型
此属性设置如果与SplitterDistance属性冲突,拆分条的初始位置会以此属性为准进行定位!
事件介绍:
1:SplitterMoving事件
拆分器移动时发生;拆分条移动过程中发生!
委托类型;SplitterCancelEventHandler
数据类型为:SplitterCancelEventArgs :CancelEventArgs
数据成员为:SplitX,SplitY,MouseCursorX,MouseCursorY,表示在工作区,鼠标指针,拆分条的坐
标值。
SplitX SplitY X坐标点指的是拆分器与容器距离,垂直Y为0
MouseCursorX MouseCursorY 表示鼠标指针在工作的位置点坐标
2: SplitterMoved事件
拆分器移动后发生;
委托类型;SplitterEventHandler
数据类型;SplitterEventArgs :EventArgs
数据成员;SplitX,SplitY,X,Y 表示在工作区,鼠标指针,拆分条的坐标值,这都是移动后的位置值!
Moving事件是指,鼠标按住拆分器,不放,进行移动时,发生,只要拆分器移动就会发生,不移则事件不会发生!
Moved事件是指,移动拆分器之后,释放鼠标时,发生的!它是Moving事件之后发生!
小结:
控件的Dock属性如果设置成Fill,窗体的单击事件,不会被触发;因为控件遮挡住了!
01.using System; 02.using Cshcn; 03.using System.Drawing; 04.using System.ComponentModel; 05.using System.Windows.Forms; 06.using System.Threading; 07. 08.namespace Cshcn 09.{ 10. class Program 11. { 12. 13. [STAThread] 14. static void Main() 15. { 16. Application.EnableVisualStyles(); 17. Application.SetCompatibleTextRenderingDefault(false); 18. Application.Run(new Form1()); 19. 20. } 21. } 22. 23. public class Form1:Form 24. { 25. 26. private SplitContainer splitContainer1; 27. private SplitContainer splitContainer2; 28. private Button buttonSplitterFixed; 29. private Button buttonOrientation; 30. private Label labelPanelCollapsed; 31. private Button buttonSplitterWidth; 32. 33. private bool IsCollapsed=true; 34. private bool IsVer=true; 35. private bool IsSplitFixed=false; 36. private ToolTip toolTip; 37. 38. public Form1() 39. { 40. InitializeComponent(); 41. } 42. 43. public void InitializeComponent() 44. { 45. this.splitContainer1=new SplitContainer(); 46. this.splitContainer2=new SplitContainer(); 47. this.buttonSplitterFixed=new Button(); 48. this.buttonOrientation=new Button(); 49. this.labelPanelCollapsed=new Label(); 50. this.buttonSplitterWidth=new Button(); 51. 52. this.toolTip=new ToolTip(); 53. 54. this.splitContainer2.SuspendLayout(); 55. this.splitContainer1.SuspendLayout(); 56. this.SuspendLayout(); 57. 58. this.splitContainer1.Location=new Point(10,10); 59. this.splitContainer1.Size=new Size(480,420); 60. this.splitContainer1.BorderStyle=BorderStyle.Fixed3D; 61. this.splitContainer1.FixedPanel=FixedPanel.Panel1; 62. this.splitContainer1.SplitterDistance=100; 63. this.splitContainer1.Panel1MinSize=0; 64. this.splitContainer1.SplitterWidth=4; 65. this.splitContainer1.SplitterMoving+=new SplitterCancelEventHandler 66. 67.(this.splitContainer1_Moving); 68. this.splitContainer1.SplitterMoved+=new SplitterEventHandler 69. 70.(this.splitContainer1_Moved); 71. 72. this.splitContainer2.BackColor=SystemColors.Control; 73. this.splitContainer2.Dock=DockStyle.Fill; 74. this.splitContainer2.BorderStyle=BorderStyle.Fixed3D; 75. this.splitContainer2.SplitterWidth=4; 76. this.splitContainer2.SplitterIncrement=6; 77. this.splitContainer2.Orientation=Orientation.Horizontal; 78. this.splitContainer2.Panel2MinSize=100; 79. this.splitContainer2.SplitterMoving+=new SplitterCancelEventHandler 80. 81.(this.splitContainer2_Moving); 82. this.splitContainer1.Panel2.Controls.Add(this.splitContainer2); 83. 84. this.labelPanelCollapsed.Location=new Point(508,20); 85. this.labelPanelCollapsed.Size=new Size(35,35); 86. this.labelPanelCollapsed.BackColor=Color.YellowGreen; 87. this.labelPanelCollapsed.AutoSize=false; 88. this.labelPanelCollapsed.MouseEnter+=new EventHandler 89. 90.(this.labelPanelCollapsed_MouseEnter); 91. this.labelPanelCollapsed.MouseLeave+=new EventHandler 92. 93.(this.labelPanelCollapsed_MouseLeave); 94. this.labelPanelCollapsed.Click+=new EventHandler 95. 96.(this.labelPanelCollapsed_Click); 97. 98. this.buttonSplitterFixed.Text="锁定拆分条"; 99. this.buttonSplitterFixed.Location=new Point(508,70); 100. this.buttonSplitterFixed.Click+=new EventHandler 101. 102.(this.buttonSplitterFixed_Click); 103. 104. this.buttonOrientation.Text="垂直/水平"; 105. this.buttonOrientation.Location=new Point(508,110); 106. this.buttonOrientation.Click+=new EventHandler 107. 108.(this.buttonOrientation_Click); 109. 110. this.buttonSplitterWidth.Text="拆分条宽度"; 111. this.buttonSplitterWidth.Location=new Point(508,150); 112. this.buttonSplitterWidth.Click+=new EventHandler 113. 114.(this.buttonSplitterWidth_Click); 115. this.buttonSplitterWidth.MouseMove+=new MouseEventHandler 116. 117.(this.buttonSplitterWidth_MouseMove); 118. this.buttonSplitterWidth.MouseLeave+=new EventHandler 119. 120.(this.buttonSplitterWidth_MouseLeave); 121. 122. 123. this.Text="拆分器_SplitContainer"; 124. this.ClientSize=new Size(600,440); 125. this.StartPosition=FormStartPosition.CenterScreen; 126. this.MaximizeBox=false; 127. this.Controls.AddRange(new Control[] 128. 129.{this.splitContainer1,this.buttonSplitterFixed, 130. 131. 132.this.labelPanelCollapsed,this.buttonOrientation, 133. this.buttonSplitterWidth}); 134. 135. this.splitContainer2.ResumeLayout(false); 136. this.splitContainer1.ResumeLayout(false); 137. this.ResumeLayout(false); 138. } 139. 140. private void buttonSplitterWidth_Click(object sender,EventArgs e) 141. { 142. splitContainer1.SplitterWidth+=4; 143. } 144. private void buttonSplitterWidth_MouseMove(object sender,MouseEventArgs e) 145. { 146. Button btn=(Button)sender; 147. toolTip.SetToolTip(btn,"Width= "+SplitterWidth); 148. 149. } 150. private void buttonSplitterWidth_MouseLeave(object sender,EventArgs e) 151. { 152. splitContainer1.SplitterWidth=4; 153. } 154. private string SplitterWidth 155. { 156. get{return splitContainer1.SplitterWidth.ToString();} 157. } 158. private void buttonSplitterFixed_Click(object sender,EventArgs e) 159. { 160. if(!IsSplitFixed) 161. { 162. splitContainer1.IsSplitterFixed=true; 163. IsSplitFixed=true; 164. } 165. else 166. { 167. splitContainer1.IsSplitterFixed=false; 168. IsSplitFixed=false; 169. 170. } 171. } 172. private void labelPanelCollapsed_MouseEnter(object sender,EventArgs e) 173. { 174. for(int i=0;i<10;i++) 175. { 176. Thread.Sleep(8); 177. splitContainer1.SplitterDistance+=20; 178. } 179. 180. labelPanelCollapsed.BackColor=Color.Brown; 181. } 182. private void labelPanelCollapsed_MouseLeave(object sender,EventArgs e) 183. { 184. for(int i=0;i<10;i++) 185. { 186. Thread.Sleep(8); 187. splitContainer1.SplitterDistance-=20; 188. } 189. 190. labelPanelCollapsed.BackColor=Color.YellowGreen; 191. Label label=(Label)sender; 192. toolTip.SetToolTip(label,"Click:展开/折叠"); 193. } 194. private void labelPanelCollapsed_Click(object sedner, EventArgs e) 195. { 196. if(IsCollapsed) 197. { 198. splitContainer1.Panel1Collapsed=true; 199. IsCollapsed=false; 200. } 201. else 202. { 203. splitContainer1.Panel1Collapsed=false; 204. IsCollapsed=true; 205. } 206. labelPanelCollapsed.BackColor=Color.YellowGreen; 207. 208. } 209. private void buttonOrientation_Click(object sedner, EventArgs e) 210. { 211. if(IsVer) 212. { 213. splitContainer1.Orientation=Orientation.Horizontal; 214. IsVer=false; 215. } 216. else 217. { 218. splitContainer1.Orientation=Orientation.Vertical; 219. IsVer=true; 220. } 221. 222. } 223. 224. private void splitContainer1_Moving(object sender,SplitterCancelEventArgs e) 225. { 226. Cursor.Current=Cursors.VSplit; 227. 228. SplitContainer splitter=(SplitContainer)sender; 229. toolTip.SetToolTip(splitter, 230. "SplitX= "+e.SplitX+ 231. "/nSplitY= "+e.SplitY+ 232. "/nMouseCursorX= "+e.MouseCursorX+ 233. "/nMouseCursorY= "+e.MouseCursorY); 234. 235. 236. 237. } 238. private void splitContainer1_Moved(object sender,SplitterEventArgs e) 239. { 240. SplitContainer splitter=(SplitContainer)sender; 241. toolTip.SetToolTip(splitter, 242. "SplitX= "+e.SplitX+ 243. "/nSplitY= "+e.SplitY+ 244. "/n X= "+e.X+ 245. "/n Y= "+e.Y); 246. } 247. private void splitContainer2_Moving(object sender ,SplitterCancelEventArgs 248. 249.e) 250. { 251. Cursor.Current=Cursors.HSplit; 252. } 253. 254. private System.ComponentModel.IContainer components = null; 255. protected override void Dispose(bool disposing) 256. { 257. if (disposing && (components != null)) 258. { 259. components.Dispose(); 260. } 261. base.Dispose(disposing); 262. } 263. 264. } 265.}