1. 程式人生 > >C#實現浮動和多標籤窗體解決方案---使用Dockpanel

C#實現浮動和多標籤窗體解決方案---使用Dockpanel

首先宣告:在此感謝Dockpanel 實現浮動和多標籤窗體解決方案給我提供思路和靈感。本文的形成也多有借鑑,再次表示感謝!在此記錄一下Dockpanel框架的搭建過程(主要步驟),以及需要注意的幾點。

1. 下載Dockpanel suite。

2. 構建主窗體(父窗體):FrmMain。

     ① 新建工程:VehicleMonitorSystem;

     ② 將WeifenLuo.WinFormsUI.Docking.dll放置當前工程資料夾下,建議:\bin\Debug\WeifenLuo.WinFormsUI.Docking.dll;

     ③ 在當前工程中,通過解決方案資源管理器新增引用WeifenLuo.WinFormsUI.Docking.dll到當前工程;

     ④  新增主窗體:FrmMain,並設定主窗體 IsMdiContainer = true;

     ⑤ 在主窗體中新增dockpanel控制元件:DockPanelOfFrmMain,並設定dockpanel 的documentstyle :dockPanel.DocumentStyle = DocumentStyle.DockingMdi;

      後臺程式碼如下:

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.IO;
using WeifenLuo.WinFormsUI.Docking;

namespace VehicleMonitorSystem
{
    public partial class FrmMain : Form
    {        

        #region 欄位
        private string m_DockPath = string.Empty;
        #endregion

        #region 建構函式
        public FrmMain()
        {
            InitializeComponent();
        }
        #endregion

        #region 主窗體載入
        private void FrmMain_Load(object sender, EventArgs e)
        {
            this.DockPanelOfFrmMain.DocumentStyle = DocumentStyle.DockingMdi;
            this.m_DockPath = Path.Combine(Path.GetDirectoryName(Application.ExecutablePath), "DockPanel.config");
            this.InitDockPanel();
            this.StatusBarOfFrmMain.Items.Add("就緒");            
        }
        #endregion

        #region DockPanel初始化與配置儲存

        #region 按照配置檔案初始化Dockpanel
        private void InitDockPanel()
        {
            try
            {
                //根據配置檔案動態載入浮動窗體
                this.DockPanelOfFrmMain.LoadFromXml(this.m_DockPath, delegate(string persistString)
                {
                    //功能窗體
                    if (persistString == typeof(FrmFunction).ToString())
                    {
                        return FrmFunction.GetInstance();
                    }
                                 
                    //主框架之外的窗體不顯示
                    return null;
                });
            }
            catch (Exception)
            {
                //配置檔案不存在或配置檔案有問題時 按系統預設規則載入子窗體
                FrmFunction.GetInstance().Show(this.DockPanelOfFrmMain, AppConfig.ms_FrmFunction);
                
            }
        }
        #endregion

        #region 關閉窗體時儲存介面。為了下次開啟程式時,浮動窗體的顯示位置和關閉時一致,可以在主窗體的frmMain_FormClosing事件中呼叫:dockPanel.SaveAsXml(this.m_DockPath)

        private void FrmMain_FormClosing(object sender, FormClosingEventArgs e)
        {
            try
            {
                DockPanelOfFrmMain.SaveAsXml(this.m_DockPath);
            }
            catch (Exception ex)
            {
                MessageBox.Show("儲存Dockpanel配置檔案失敗," + ex.Message);
                return;
            }
        }

        #endregion   

        #region 退出
        private void 退出EToolStripMenuItem_Click(object sender, EventArgs e)
        {
            this.Close();
        }
        #endregion

        #region 關於
        private void 關於AToolStripMenuItem_Click(object sender, EventArgs e)
        {
            MessageBox.Show(" DockPanel Suite V2.1.3364.29178 \n\n VehicleMonitorSystem V1.0.0 \n\n Created by 張月華 \n\n E-mail:
[email protected]
\n\n Date:2011-10-10", "車輛監控系統"); } #endregion #endregion } }

3. 構建需要浮動顯示的窗體:FrmFunction。

    ① 在當前工程中新增窗體:FrmFunction;(注意:浮動窗體和標籤窗體需要繼承自DockContent);

    ② 為了保證在關閉某一浮動窗體之後,再開啟時能夠在原位置顯示,要對浮動窗體處理,處理窗體的DockstateChanged事件,標籤窗體dock位置改變,記錄到公共類;

    後臺程式碼如下:

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using WeifenLuo.WinFormsUI.Docking;

namespace VehicleMonitorSystem
{
    public partial class FrmFunction : DockContent   // 浮動窗體和標籤窗體需要繼承自DockContent
    {
       
        #region 欄位
        private static FrmFunction Instance;
        #endregion

        #region 建構函式
        public FrmFunction()
        {
            InitializeComponent();
        }
        #endregion        

        #region 靜態例項初始化函式
        public static FrmFunction GetInstance()
        {
            if (Instance == null)
            {
                Instance = new FrmFunction();
            }
            return Instance;
        }
        #endregion

        #region 為了保證在關閉某一浮動窗體之後,再開啟時能夠在原位置顯示,要對浮動窗體處理,處理窗體的DockstateChanged事件,標籤窗體dock位置改變,記錄到公共類

        private void FrmFunction_DockStateChanged(object sender, EventArgs e)
        {
            //關閉時(dockstate為unknown) 不把dockstate儲存
            if (Instance != null)
            {
                if (this.DockState == DockState.Unknown || this.DockState == DockState.Hidden)
                {
                    return;
                }
                AppConfig.ms_FrmFunction = this.DockState;
            }
        }
        #endregion

        #region 關閉事件
        private void FrmFunction_FormClosed(object sender, FormClosedEventArgs e)
        {
            Instance = null;  // 否則下次開啟時報錯,提示“無法訪問已釋放物件”
        }
        #endregion

    }
}

4. 讓窗體FrmFunction浮動起來。

     ①  在主窗體FrmMain中新增選單欄(或者按鈕):檢視->功能窗;

     ② 雙擊選單對應選項,新增程式碼如下:

#region 顯示功能窗

        private void 功能窗ToolStripMenuItem_Click(object sender, EventArgs e)
        {
            this.ShowFrmFunction();
        }   

        private void ShowFrmFunction()
        {
            
            FrmFunction frmFun = FrmFunction.GetInstance();          
            frmFun.Show(this.DockPanelOfFrmMain, AppConfig.ms_FrmFunction);
            this.StatusBarOfFrmMain.Items[0].Text = frmFun.Text;
        }
#endregion


5. 在當前工程中新增類AppConfig。

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using WeifenLuo.WinFormsUI.Docking;

namespace VehicleMonitorSystem
{
    class AppConfig
    {
        #region 欄位

        public static DockState ms_FrmFunction = DockState.DockLeft;   // 功能窗體,左端停靠 
        
        #endregion
    }
}


6. 執行,子窗體FrmFunction將在父窗體FrmMain的左端停靠。Enjoy it ~~吐舌頭

值得注意的幾點:

A. 

    Dockpanel suite 是基於配置檔案的,其配置檔案Dockpanel.config可以放置到指定的位置。在主窗體的load 事件中要做載入配置檔案的工作。需要執行配置檔案的路徑,並使用dockpanel 的LoadFromXml方法載入需要顯示的浮動窗體,在initDockpanel 中自定義  裝載的哪些浮動窗體。

主窗體的load事件:

private void FrmMain_Load(object sender, EventArgs e)
        
{   
   ...
         
   this.m_DockPath = Path.Combine(Path.GetDirectoryName(Application.ExecutablePath), "DockPanel.config");
   this.InitDockPanel(); 

   ...           
        
}

InitDockPanel方法:

#region 按照配置檔案初始化Dockpanel
        private void InitDockPanel()
        {
            try
            {
                //根據配置檔案動態載入浮動窗體
                this.DockPanelOfFrmMain.LoadFromXml(this.m_DockPath, delegate(string persistString)
                {
                    //功能窗體
                    if (persistString == typeof(FrmFunction).ToString())
                    {
                        return FrmFunction.GetInstance();
                    }                    
               
                    //主框架之外的窗體不顯示
                    return null;
                });
            }
            catch (Exception)
            {
                // 配置檔案不存在或配置檔案有問題時 按系統預設規則載入子窗體
                  // 注:在此加入的特殊處理,來改進Dockpanel 的載入,如果dockpanel.config不存在(不小心刪除了),可以按照Appconfig中預定義的dockstate顯示。

                FrmFunction.GetInstance().Show(this.DockPanelOfFrmMain, AppConfig.ms_FrmFunction);                
            }
        }
        #endregion

B.

     浮動窗體使用:frmServer.Show(this.dockPanel, dockstate);

     標籤窗體使用:dummyDoc.Show(dockPanel);

C.

     為了下次開啟程式時,浮動窗體的顯示位置和關閉時一致,可以在主窗體的frmMain_FormClosing事件中呼叫:dockPanel.SaveAsXml(this.m_DockPath):    

#region 關閉窗體時儲存介面。為了下次開啟程式時,浮動窗體的顯示位置和關閉時一致,可以在主窗體的frmMain_FormClosing事件中呼叫:dockPanel.SaveAsXml(this.m_DockPath)

        private void FrmMain_FormClosing(object sender, FormClosingEventArgs e)
        {
            try
            {
                DockPanelOfFrmMain.SaveAsXml(this.m_DockPath);
            }
            catch (Exception ex)
            {
                MessageBox.Show("儲存Dockpanel配置檔案失敗," + ex.Message);
                return;
            }
        }

#endregion

以上幾點是搭建基於dockpanel 浮動和多標籤窗體系統的主要步驟和注意事項,如有紕漏,歡迎指教。謝謝!

注:本文原創,歡迎轉載!請註明出處,並附本文連結!
作    者:zyh

相關推薦

C#實現浮動標籤窗體解決方案---使用Dockpanel

首先宣告:在此感謝Dockpanel 實現浮動和多標籤窗體解決方案給我提供思路和靈感。本文的形成也多有借鑑,再次表示感謝!在此記錄一下Dockpanel框架的搭建過程(主要步驟),以及需要注意的幾點。 1. 下載Dockpanel suite。 2. 構建主窗體(父窗

FlexBison的C++可重進入—執行緒解決方案

這樣user可以在解析程式中使用driver or parm這兩個變數.這兩個變數都是從外面通過引數的形式傳給解析程式,那此時bison如何跟flex通訊呢?這時候就不會透過全域性變數yylval等進行通訊了,加入%pure_parser新生成的程式碼中已經沒有yylval這個全域性變量了,在解析器的內部實際

C語言如何實現繼承

使用函式指標來實現繼承和多型 #include <stdio.h> #include <stdlib.h> struct Base_Vptr //虛擬函式表 { void(*fun1)( void*);

C語言實現繼承

開發十年,就只剩下這套架構體系了! >>>   

css實現單行行省略號

1.單行省略 { width:300px; overflow: hidden; text-overflow:ellipsis; whitewhite-space: nowrap; } 注:單行省略必須設定寬度 2.多行省略 { display:-webkit-b

C++程式設計-繼承

                &nb

第二十二講 執行緒——執行緒間的通訊——個生產者消費者的升級解決方案

這裡我也是採用循序漸進的方式來講解JDK1.5版本中提供的多執行緒升級解決方案,希望能更加容易地讓大家接受。 為了解決多生產多消費的效率低下這一核心問題,在這兒我就告訴大家勢必要用到JDK1.5中jav

C++實現陣列連結串列的排序演算法

OK,我們經常會用到排序演算法。那麼,常用的排序演算法,除了使用陣列之外,用連結串列又該如何處理呢?下面我們來做個對比: //使用插入排序對陣列進行排序 int *get_order(int *num, int length) { for(int eiter = 1; eiter <

C++實現utf8gbk編碼字串互相轉換

不同系統或者伺服器之間訊息傳遞經常遇到編碼轉換問題,這裡用C++實現了一個輕量的gbk和utf8互相轉換,可跨平臺使用。(重量級的可以用libiconv庫) 在windows下用<windows.h>標頭檔案裡的函式進行多位元組和寬字元轉換,linu

C++實現佇列(Linux環境)

Stack.h #pragma once #include<iostream> using namespace std; #include<assert.h> //靜態棧 te

C 實現字串按個字符采用Split方法分割

using System.Text.RegularExpressions;string str="aaajsbbbjsccc";string[] sArray=Regex.Split(str,"js",RegexOptions.IgnoreCase);foreach (string i in sArray)

C#實現伺服器客戶端之間通訊

TCP  套接字程式設計 伺服器端實現步驟: 1、使用Socket類建立套接字。 2、利用Bind方法將建立的套接字繫結到指定的地址結構。 3、利用Listen方法設定套接字為監聽模式,使得伺服器進入被動開啟狀態。 4、接受客戶端的連線請求。 5、接收、應答客戶端的資料請求

Java基於UDP實現伺服器客戶端之間的通訊

UDPServer.java package com.zh.socket; import java.io.IOException; import java.net.DatagramPacket; import java.net.DatagramSocket; import

[Ubuntu]終端terminal標籤標籤切換快捷鍵

Ubuntu下的終端產生多標籤和多標籤切換快捷鍵 ctrl+alt+t是開啟一個terminal 開啟terminal之後使用ctrl+shift+t是在terminal中開啟多個標籤 在多個標籤中

css實現單行行文字溢位顯示省略號點點點...

一、單行文字溢位顯示省略號點點點... overflow: hidden; text-overflow:ellipsis; white-space: nowrap; 二、多行文字溢位顯示省略號點點點... display: -webkit-box; -webkit

C# 實現像QQ一樣隱藏窗體

在製作QQ窗體時,其關鍵是判斷滑鼠是否在窗體上,主要是用API函式WindowFromPoint和GetParent來實現的 (1)API函式WindowFromPoint 該函式獲取當前滑鼠下視覺化控制元件的控制代碼。忽略遮蔽、隱藏以及透明視窗。其宣告如下: [DllIm

C++實現訊號槽機制

主要通過,C++實現型別QT訊號和槽的問題 設計思路: 1、利於模板函式和模板類的通用性 2、bind的時候,將槽函式指標儲存,觸發時呼叫 程式碼如下: #include <algorithm> #include <iostream> #

C#禁止關閉拖動窗體【有漏洞】

*我也是抄別人的,禁止關閉沒什麼問題,當然只要別在程序管理器裡關就行。呵呵 禁止拖動有問題,就是用標題欄左邊的圖示選單,是可以拖動窗體的。 【解決辦法應該是,用this.position來解決吧】 private const int SC_CLOSE =

佇列(用C++實現佇列)

棧是一種後進先出的線型結構,C++實現棧的程式碼如下: #include <iostream> using namespace std; #define MAXLEN 50 typede

C++實現LinuxWindows下遍歷指定目錄下的檔案

一、Linux下遍歷方法 方法非常簡單,這裡不多說了,可以直接看程式碼 #include <dirent.h>//遍歷系統指定目錄下檔案要包含的標頭檔案 #include <iost