拆分器控件Splitcontainer ,是一個含有Splitter拆分條的容器,它包含兩個面板容器Panel1,Panel2,能夠移動拆分條,對面板大小進行控制!ide
控件學習示例程序!學習
屬性介紹;測試
//拆分條的是否啓用禁用
bool IsSplitterFixed{get;set;}
bool類型,true:不能調節拆分條; false :可以使用拆分條.
決定是否能夠經過鍵盤或鼠標來移動拆分器。this
//拆分器的方向;垂直和水平方向上拆分控件,上下和左右兩塊面板
Orientation Orientation{get;set;}
Orientation是枚舉;包含兩個值;水平與垂直; Vertical ; Horizontal ;spa
//獲取拆分條(器)的大小
Rectangle SplitterRectangle{get;}
取得拆分條的矩形表示形式;new Rectangle(X,Y width,height)拆分器就是指拆分條,座標點,寬高
拆分條,是一個矩形表示的!
該屬性的變化,與Orientation屬性相關!垂直,水平顯示狀態,拆分條的位置大小固然不一樣!指針
//指定拆分器面板之一保持不變
調節拆分器,使用panel1或panel2面板不會改變大小;
FixedPanel FixedPanel{get;set;}
FixedPanel枚舉: 有三個值;None(默認值), Panel1 ,Panel2;
肯定調整SplitterContainer控件後,哪一個面板將保持不變!調整拆器容器大小時,能夠保持哪一個面板不變時,
才設置此屬性,若是容器不會改變,也不用設置此屬性!component
//設置拆分器離容器的初始大小,拆分條左或上邊緣離容器的初始寬度。
int SplitterDistance{get;set;}
像素爲單位,默認值爲40 ,在移動拆分器時,這個值是變化的,計算左邊緣或上邊緣的距離!
拆分器的初始距離.orm
//設置拖動拆分條(器)移動的增量大小,
也就是拖移的速度!一樣的拖動,移動的寬度可不相同。
int SplitterIncrement{get;set;}
獲取設置拆分條移動的增量,默認值爲1;
測試SplitterIncrement屬性值的大小,能夠通接受鍵盤上下鍵的,調節拆分器,IsSplitterFixed必須爲trueblog
//設置拆分條的寬度
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,窗體的單擊事件,不會被觸發;由於控件遮擋住了!
- using System;
- using Cshcn;
- using System.Drawing;
- using System.ComponentModel;
- using System.Windows.Forms;
- using System.Threading;
- namespace Cshcn
- {
- class Program
- {
-
- [STAThread]
- static void Main()
- {
- Application.EnableVisualStyles();
- Application.SetCompatibleTextRenderingDefault(false);
- Application.Run(new Form1());
- }
- }
- public class Form1:Form
- {
-
- private SplitContainer splitContainer1;
- private SplitContainer splitContainer2;
- private Button buttonSplitterFixed;
- private Button buttonOrientation;
- private Label labelPanelCollapsed;
- private Button buttonSplitterWidth;
- private bool IsCollapsed=true;
- private bool IsVer=true;
- private bool IsSplitFixed=false;
- private ToolTip toolTip;
- public Form1()
- {
- InitializeComponent();
- }
- public void InitializeComponent()
- {
- this.splitContainer1=new SplitContainer();
- this.splitContainer2=new SplitContainer();
- this.buttonSplitterFixed=new Button();
- this.buttonOrientation=new Button();
- this.labelPanelCollapsed=new Label();
- this.buttonSplitterWidth=new Button();
- this.toolTip=new ToolTip();
- this.splitContainer2.SuspendLayout();
- this.splitContainer1.SuspendLayout();
- this.SuspendLayout();
- this.splitContainer1.Location=new Point(10,10);
- this.splitContainer1.Size=new Size(480,420);
- this.splitContainer1.BorderStyle=BorderStyle.Fixed3D;
- this.splitContainer1.FixedPanel=FixedPanel.Panel1;
- this.splitContainer1.SplitterDistance=100;
- this.splitContainer1.Panel1MinSize=0;
- this.splitContainer1.SplitterWidth=4;
- this.splitContainer1.SplitterMoving+=new SplitterCancelEventHandler
- (this.splitContainer1_Moving);
- this.splitContainer1.SplitterMoved+=new SplitterEventHandler
- (this.splitContainer1_Moved);
- this.splitContainer2.BackColor=SystemColors.Control;
- this.splitContainer2.Dock=DockStyle.Fill;
- this.splitContainer2.BorderStyle=BorderStyle.Fixed3D;
- this.splitContainer2.SplitterWidth=4;
- this.splitContainer2.SplitterIncrement=6;
- this.splitContainer2.Orientation=Orientation.Horizontal;
- this.splitContainer2.Panel2MinSize=100;
- this.splitContainer2.SplitterMoving+=new SplitterCancelEventHandler
- (this.splitContainer2_Moving);
- this.splitContainer1.Panel2.Controls.Add(this.splitContainer2);
- this.labelPanelCollapsed.Location=new Point(508,20);
- this.labelPanelCollapsed.Size=new Size(35,35);
- this.labelPanelCollapsed.BackColor=Color.YellowGreen;
- this.labelPanelCollapsed.AutoSize=false;
- this.labelPanelCollapsed.MouseEnter+=new EventHandler
- (this.labelPanelCollapsed_MouseEnter);
- this.labelPanelCollapsed.MouseLeave+=new EventHandler
- (this.labelPanelCollapsed_MouseLeave);
- this.labelPanelCollapsed.Click+=new EventHandler
- (this.labelPanelCollapsed_Click);
- this.buttonSplitterFixed.Text="鎖定拆分條";
- this.buttonSplitterFixed.Location=new Point(508,70);
- this.buttonSplitterFixed.Click+=new EventHandler
- (this.buttonSplitterFixed_Click);
-
- this.buttonOrientation.Text="垂直/水平";
- this.buttonOrientation.Location=new Point(508,110);
- this.buttonOrientation.Click+=new EventHandler
- (this.buttonOrientation_Click);
- this.buttonSplitterWidth.Text="拆分條寬度";
- this.buttonSplitterWidth.Location=new Point(508,150);
- this.buttonSplitterWidth.Click+=new EventHandler
- (this.buttonSplitterWidth_Click);
- this.buttonSplitterWidth.MouseMove+=new MouseEventHandler
- (this.buttonSplitterWidth_MouseMove);
- this.buttonSplitterWidth.MouseLeave+=new EventHandler
- (this.buttonSplitterWidth_MouseLeave);
-
- this.Text="拆分器_SplitContainer";
- this.ClientSize=new Size(600,440);
- this.StartPosition=FormStartPosition.CenterScreen;
- this.MaximizeBox=false;
- this.Controls.AddRange(new Control[]
- {this.splitContainer1,this.buttonSplitterFixed,
-
- this.labelPanelCollapsed,this.buttonOrientation,
- this.buttonSplitterWidth});
-
- this.splitContainer2.ResumeLayout(false);
- this.splitContainer1.ResumeLayout(false);
- this.ResumeLayout(false);
- }
-
- private void buttonSplitterWidth_Click(object sender,EventArgs e)
- {
- splitContainer1.SplitterWidth+=4;
- }
- private void buttonSplitterWidth_MouseMove(object sender,MouseEventArgs e)
- {
- Button btn=(Button)sender;
- toolTip.SetToolTip(btn,"Width= "+SplitterWidth);
- }
- private void buttonSplitterWidth_MouseLeave(object sender,EventArgs e)
- {
- splitContainer1.SplitterWidth=4;
- }
- private string SplitterWidth
- {
- get{return splitContainer1.SplitterWidth.ToString();}
- }
- private void buttonSplitterFixed_Click(object sender,EventArgs e)
- {
- if(!IsSplitFixed)
- {
- splitContainer1.IsSplitterFixed=true;
- IsSplitFixed=true;
- }
- else
- {
- splitContainer1.IsSplitterFixed=false;
- IsSplitFixed=false;
-
- }
- }
- private void labelPanelCollapsed_MouseEnter(object sender,EventArgs e)
- {
- for(int i=0;i<10;i++)
- {
- Thread.Sleep(8);
- splitContainer1.SplitterDistance+=20;
- }
- labelPanelCollapsed.BackColor=Color.Brown;
- }
- private void labelPanelCollapsed_MouseLeave(object sender,EventArgs e)
- {
- for(int i=0;i<10;i++)
- {
- Thread.Sleep(8);
- splitContainer1.SplitterDistance-=20;
- }
- labelPanelCollapsed.BackColor=Color.YellowGreen;
- Label label=(Label)sender;
- toolTip.SetToolTip(label,"Click:展開/摺疊");
- }
- private void labelPanelCollapsed_Click(object sedner, EventArgs e)
- {
- if(IsCollapsed)
- {
- splitContainer1.Panel1Collapsed=true;
- IsCollapsed=false;
- }
- else
- {
- splitContainer1.Panel1Collapsed=false;
- IsCollapsed=true;
- }
- labelPanelCollapsed.BackColor=Color.YellowGreen;
- }
- private void buttonOrientation_Click(object sedner, EventArgs e)
- {
- if(IsVer)
- {
- splitContainer1.Orientation=Orientation.Horizontal;
- IsVer=false;
- }
- else
- {
- splitContainer1.Orientation=Orientation.Vertical;
- IsVer=true;
- }
- }
-
- private void splitContainer1_Moving(object sender,SplitterCancelEventArgs e)
- {
- Cursor.Current=Cursors.VSplit;
-
- SplitContainer splitter=(SplitContainer)sender;
- toolTip.SetToolTip(splitter,
- "SplitX= "+e.SplitX+
- "/nSplitY= "+e.SplitY+
- "/nMouseCursorX= "+e.MouseCursorX+
- "/nMouseCursorY= "+e.MouseCursorY);
-
-
- }
- private void splitContainer1_Moved(object sender,SplitterEventArgs e)
- {
- SplitContainer splitter=(SplitContainer)sender;
- toolTip.SetToolTip(splitter,
- "SplitX= "+e.SplitX+
- "/nSplitY= "+e.SplitY+
- "/n X= "+e.X+
- "/n Y= "+e.Y);
- }
- private void splitContainer2_Moving(object sender ,SplitterCancelEventArgs
- e)
- {
- Cursor.Current=Cursors.HSplit;
- }
-
- private System.ComponentModel.IContainer components = null;
- protected override void Dispose(bool disposing)
- {
- if (disposing && (components != null))
- {
- components.Dispose();
- }
- base.Dispose(disposing);
- }
- }
- }