码迷,mamicode.com
首页 > 其他好文 > 详细

拆分器控件Splitcontainer

时间:2015-03-11 07:05:57      阅读:154      评论:0      收藏:0      [点我收藏+]

标签:

拆分器控件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,窗体的单击事件,不会被触发;因为控件遮挡住了!

技术分享

    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. }

拆分器控件Splitcontainer

标签:

原文地址:http://www.cnblogs.com/haifan1984/p/4328900.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!