首页 > 代码库 > 学习winform第三方界面weiFenLuo.winFormsUI.Docking.dll

学习winform第三方界面weiFenLuo.winFormsUI.Docking.dll

  1. 控件dockpanel中提供了几个可用的类, 重要的有两个, 一是DockPanel, 一是DockContent, 
       DockPanel是从panel继承出来的, 用于提供可浮动的dock的子窗口进行浮动和dock的场所, 
     DockContent是从form类中继承出来的, 用于提供可浮动的窗口基类. 就是说: DockContent对象可以在DockPanel对象中任意贴边, 浮动, TAB化等.

     

  2. 引用—>添加引用—>浏览—>weiFenLuo.winFormsUI.Docking.dll。
  3. 工具箱—>右键—>选择项—>.net组件—>浏览—>weiFenLuo.winFormsUI.Docking.dll—>在工具箱出现dockPanel。
  4. 将dockPanel拖到窗体Form1上,设置Dock属性,我设置的是:Fill。窗体属性IsMdiContainer:True。
  5.  在代码中修改窗体继承于DockContent。           public partial class Form2 : DockContent
  6. 在主窗体中:
    public partial class Form1 : DockContent
        {
            //子窗体
            private FrmCustomer frmCustomer = new FrmCustomer();
            private b_employee employee = new b_employee ();
    
            public Form1()
            {
                InitializeComponent();
                frmCustomer.Show(this.dockPanel1);
                employee.Show(this.dockPanel1);
                System.Threading.Thread.Sleep(50);
            }
    
            private void Form1_Load(object sender, EventArgs e)
            {
                Form2 frm = new Form2();
                frm.Show(this.dockPanel1);
                //窗体Form2添加到主窗体左边缘
                frm.DockTo(this.dockPanel1,DockStyle.Left);
            }
    }

     

  7. 在主窗体中添加右键事件,设置dockPanel的contentMenuScrip为拖拽的menuscrip,窗体添加
     /// <summary>
            /// 关闭所有窗体
            /// </summary>
            /// <param name="sender"></param>
            /// <param name="e"></param>
            private void menu_Window_CloseAll_Click(object sender, EventArgs e)
            {
                CloseAllDocuments();
            }
    
            public void CloseAllDocuments()
            {
                if (dockPanel1.DocumentStyle==DocumentStyle.SystemMdi)
                {
                    foreach (Form form in MdiChildren)
                    {
                         form.Close();
                    }
                }
                    else
                    {
                        IDockContent[] documents=dockPanel1.DocumentsToArray();
                        foreach (IDockContent content in documents)
                        {
                            content.DockHandler.Close();
                        }
                    }
                }
            /// <summary>
            /// 关闭其他窗体
            /// </summary>
            /// <param name="sender"></param>
            /// <param name="e"></param>
            private void menu_Window_CloseOther_Click(object sender, EventArgs e)
            {
                if (dockPanel1.DocumentStyle == DocumentStyle.SystemMdi)
                {
                    Form activeMdi = ActiveMdiChild;
                    foreach (Form form in MdiChildren)
                    {
                        if (form != activeMdi)
                        {
                            form.Close();
                        }
                    }
                }
                else 
                {
                    foreach (IDockContent document in dockPanel1.DocumentsToArray())
                    {
                        if (!document.DockHandler.IsActivated)
                        {
                            document.DockHandler.Close();
                        }
                    }
                }
            }
    
            /// <summary>
            /// 找到窗体
            /// </summary>
            /// <param name="text"></param>
            /// <returns></returns>
            private DockContent FindDocument(string text) 
            {
                if (dockPanel1.DocumentStyle == DocumentStyle.SystemMdi)
                {
                    foreach (Form form in MdiChildren)
                    {
                        if (form.Text == text)
                        {
                            return form as DockContent;
                        }
                    }
                    return null;
                }
                else {
                    foreach (DockContent  content in dockPanel1.Documents)
                    {
                        if (content.DockHandler.TabText==text)
                        {
                            return content;
                        }
                    }
    
                    return null;
                }
            }
            public DockContent ShowContent(string caption, Type formType)
            {
                DockContent frm = FindDocument(caption);
                if (frm==null)
                {
                    // frm = ChildWinManagement.LoadMdiForm(Portal.gc.MainDialog, formType) as DockContent;
                }
                frm.Show(this.dockPanel1);
                frm.BringToFront();//将控件带到前面
                return frm;
            }

     

学习winform第三方界面weiFenLuo.winFormsUI.Docking.dll