拆分器控件Splitcontainer

拆分器控件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,窗體的單擊事件,不會被觸發;由於控件遮擋住了!

    1. using System;
    2. using Cshcn;
    3. using System.Drawing;
    4. using System.ComponentModel;
    5. using System.Windows.Forms;
    6. using System.Threading;
    7. namespace Cshcn
    8. {   
    9.    class Program
    10.     {
    11.         
    12.     [STAThread]
    13.         static void Main()
    14.         {   
    15.             Application.EnableVisualStyles();
    16.             Application.SetCompatibleTextRenderingDefault(false);
    17.             Application.Run(new Form1());
    18.         }
    19.     }
    20.     public class Form1:Form
    21.     {  
    22.        
    23.        private SplitContainer splitContainer1;
    24.        private SplitContainer splitContainer2;
    25.        private Button buttonSplitterFixed;
    26.        private Button buttonOrientation;
    27.        private Label labelPanelCollapsed;
    28.        private Button buttonSplitterWidth;
    29.        private bool IsCollapsed=true;
    30.        private bool IsVer=true;
    31.        private bool IsSplitFixed=false;
    32.        private ToolTip toolTip;
    33.        public Form1()
    34.         {
    35.              InitializeComponent();
    36.         }
    37.        public void InitializeComponent()
    38.         {  
    39.            this.splitContainer1=new SplitContainer();
    40.            this.splitContainer2=new SplitContainer();
    41.            this.buttonSplitterFixed=new Button();
    42.            this.buttonOrientation=new Button();
    43.            this.labelPanelCollapsed=new Label();
    44.            this.buttonSplitterWidth=new Button();
    45.            this.toolTip=new ToolTip();
    46.            this.splitContainer2.SuspendLayout();
    47.            this.splitContainer1.SuspendLayout();
    48.            this.SuspendLayout();
    49.            this.splitContainer1.Location=new Point(10,10);
    50.            this.splitContainer1.Size=new Size(480,420);
    51.            this.splitContainer1.BorderStyle=BorderStyle.Fixed3D;
    52.            this.splitContainer1.FixedPanel=FixedPanel.Panel1;
    53.            this.splitContainer1.SplitterDistance=100;
    54.            this.splitContainer1.Panel1MinSize=0;
    55.            this.splitContainer1.SplitterWidth=4;
    56.            this.splitContainer1.SplitterMoving+=new SplitterCancelEventHandler
    57. (this.splitContainer1_Moving);
    58.            this.splitContainer1.SplitterMoved+=new SplitterEventHandler
    59. (this.splitContainer1_Moved);
    60.            this.splitContainer2.BackColor=SystemColors.Control;
    61.            this.splitContainer2.Dock=DockStyle.Fill;
    62.            this.splitContainer2.BorderStyle=BorderStyle.Fixed3D;
    63.            this.splitContainer2.SplitterWidth=4;
    64.            this.splitContainer2.SplitterIncrement=6;
    65.            this.splitContainer2.Orientation=Orientation.Horizontal;
    66.            this.splitContainer2.Panel2MinSize=100;
    67.            this.splitContainer2.SplitterMoving+=new SplitterCancelEventHandler
    68. (this.splitContainer2_Moving);
    69.            this.splitContainer1.Panel2.Controls.Add(this.splitContainer2);
    70.            this.labelPanelCollapsed.Location=new Point(508,20);
    71.            this.labelPanelCollapsed.Size=new Size(35,35);
    72.            this.labelPanelCollapsed.BackColor=Color.YellowGreen;
    73.            this.labelPanelCollapsed.AutoSize=false;
    74.            this.labelPanelCollapsed.MouseEnter+=new EventHandler
    75. (this.labelPanelCollapsed_MouseEnter);
    76.            this.labelPanelCollapsed.MouseLeave+=new EventHandler
    77. (this.labelPanelCollapsed_MouseLeave);
    78.            this.labelPanelCollapsed.Click+=new EventHandler
    79. (this.labelPanelCollapsed_Click);
    80.            this.buttonSplitterFixed.Text="鎖定拆分條";
    81.            this.buttonSplitterFixed.Location=new Point(508,70);
    82.            this.buttonSplitterFixed.Click+=new EventHandler
    83. (this.buttonSplitterFixed_Click);
    84.            
    85.            this.buttonOrientation.Text="垂直/水平";
    86.            this.buttonOrientation.Location=new Point(508,110);
    87.            this.buttonOrientation.Click+=new EventHandler
    88. (this.buttonOrientation_Click);
    89.            this.buttonSplitterWidth.Text="拆分條寬度";
    90.            this.buttonSplitterWidth.Location=new Point(508,150);
    91.            this.buttonSplitterWidth.Click+=new EventHandler
    92. (this.buttonSplitterWidth_Click);
    93.            this.buttonSplitterWidth.MouseMove+=new MouseEventHandler
    94. (this.buttonSplitterWidth_MouseMove);
    95.            this.buttonSplitterWidth.MouseLeave+=new EventHandler
    96. (this.buttonSplitterWidth_MouseLeave);
    97.           
    98.            this.Text="拆分器_SplitContainer";
    99.            this.ClientSize=new Size(600,440);
    100.            this.StartPosition=FormStartPosition.CenterScreen;
    101.            this.MaximizeBox=false;
    102.            this.Controls.AddRange(new Control[]
    103. {this.splitContainer1,this.buttonSplitterFixed,
    104.                                                 
    105. this.labelPanelCollapsed,this.buttonOrientation,
    106.                                                 this.buttonSplitterWidth});
    107.            
    108.            this.splitContainer2.ResumeLayout(false);
    109.            this.splitContainer1.ResumeLayout(false);
    110.            this.ResumeLayout(false);
    111.         }
    112.         
    113.         private void buttonSplitterWidth_Click(object sender,EventArgs e)
    114.         {
    115.               splitContainer1.SplitterWidth+=4;
    116.         }
    117.         private void buttonSplitterWidth_MouseMove(object sender,MouseEventArgs e)
    118.         {
    119.              Button btn=(Button)sender;
    120.              toolTip.SetToolTip(btn,"Width= "+SplitterWidth);
    121.         }
    122.         private void buttonSplitterWidth_MouseLeave(object sender,EventArgs e)
    123.         {
    124.              splitContainer1.SplitterWidth=4;
    125.         }
    126.         private string SplitterWidth
    127.         {   
    128.             get{return splitContainer1.SplitterWidth.ToString();}
    129.         }
    130.         private void buttonSplitterFixed_Click(object sender,EventArgs e)
    131.         {
    132.             if(!IsSplitFixed)
    133.             {
    134.                  splitContainer1.IsSplitterFixed=true;
    135.                  IsSplitFixed=true;
    136.             }
    137.             else
    138.             {
    139.                  splitContainer1.IsSplitterFixed=false;
    140.                  IsSplitFixed=false;
    141.                
    142.             }
    143.         }
    144.         private void labelPanelCollapsed_MouseEnter(object sender,EventArgs e)
    145.         {
    146.              for(int i=0;i<10;i++)
    147.              {
    148.                  Thread.Sleep(8);
    149.                  splitContainer1.SplitterDistance+=20;
    150.              }
    151.              labelPanelCollapsed.BackColor=Color.Brown;
    152.         }
    153.         private void labelPanelCollapsed_MouseLeave(object sender,EventArgs e)
    154.         {
    155.             for(int i=0;i<10;i++)
    156.             {
    157.                 Thread.Sleep(8);
    158.                 splitContainer1.SplitterDistance-=20;
    159.             }
    160.             labelPanelCollapsed.BackColor=Color.YellowGreen;
    161.             Label label=(Label)sender;
    162.             toolTip.SetToolTip(label,"Click:展開/摺疊");
    163.         }
    164.         private void labelPanelCollapsed_Click(object sedner, EventArgs e)
    165.         {
    166.              if(IsCollapsed)
    167.               {
    168.                  splitContainer1.Panel1Collapsed=true;
    169.                  IsCollapsed=false;
    170.               }
    171.              else
    172.               {
    173.                  splitContainer1.Panel1Collapsed=false;
    174.                  IsCollapsed=true;
    175.               }
    176.               labelPanelCollapsed.BackColor=Color.YellowGreen;
    177.         }
    178.         private void buttonOrientation_Click(object sedner, EventArgs e)
    179.         {
    180.              if(IsVer)
    181.               {
    182.                  splitContainer1.Orientation=Orientation.Horizontal;
    183.                  IsVer=false;
    184.               }
    185.              else
    186.               {
    187.                  splitContainer1.Orientation=Orientation.Vertical;
    188.                  IsVer=true;
    189.               }
    190.         }
    191.    
    192.         private void splitContainer1_Moving(object sender,SplitterCancelEventArgs e)
    193.         {  
    194.             Cursor.Current=Cursors.VSplit;
    195.             
    196.             SplitContainer splitter=(SplitContainer)sender;
    197.             toolTip.SetToolTip(splitter,
    198.                                "SplitX= "+e.SplitX+
    199.                                "/nSplitY= "+e.SplitY+
    200.                                "/nMouseCursorX= "+e.MouseCursorX+
    201.                                "/nMouseCursorY= "+e.MouseCursorY);        
    202.     
    203.             
    204.         }
    205.         private void splitContainer1_Moved(object sender,SplitterEventArgs e)
    206.         {
    207.             SplitContainer splitter=(SplitContainer)sender;
    208.             toolTip.SetToolTip(splitter,
    209.                                "SplitX= "+e.SplitX+
    210.                                "/nSplitY= "+e.SplitY+
    211.                                "/n X= "+e.X+
    212.                                "/n Y= "+e.Y);    
    213.         }
    214.         private void splitContainer2_Moving(object sender ,SplitterCancelEventArgs 
    215. e)
    216.         {   
    217.             Cursor.Current=Cursors.HSplit;
    218.         }
    219.        
    220.        private System.ComponentModel.IContainer components = null;
    221.        protected override void Dispose(bool disposing)
    222.         {
    223.             if (disposing && (components != null))
    224.             {
    225.                 components.Dispose();
    226.             }
    227.             base.Dispose(disposing);
    228.         }
    229.     }
    230. }
相關文章
相關標籤/搜索