1. 程式人生 > >佈局控制元件"WeifenLuo.WinFormsUI.Docking"的使用--如何控制自動停靠視窗的大小

佈局控制元件"WeifenLuo.WinFormsUI.Docking"的使用--如何控制自動停靠視窗的大小

在使用這個控制元件的時候,估計大家都會碰到 這樣一個問題,就是當視窗是自動隱藏的時候,好像出來的大小一般比實際的大,感覺不太美觀,有沒有什麼方法可以控制它的呢,答案是當然有了,其實實現起來也很簡單。

首先我們來看看其內部機制是如何實現的,因為該控制元件有一些屬性,專門用來控制視窗的比例的。我們在該控制元件的原始碼上看到DocingPanel類中有這麼一個屬性,是用來控制自動隱藏視窗的縮放比例的。

        [LocalizedCategory("Category_Docking")]
        [LocalizedDescription(
"DockContent_AutoHidePortion_Description
")]
        [DefaultValue(
0.25)]
        
publicdouble AutoHidePortion
        {
            
get    {    return DockHandler.AutoHidePortion;    }
            
set    {    DockHandler.AutoHidePortion = value;    }

        } 

 預設的大小是0.25,這個引數是可以修改的。因為控制元件提供了一個儲存佈局狀態的方法,它預設是沒有儲存的,如果需要記住調整的視窗大小(當然這是一種最好的方法),那麼只需要加上幾段程式碼即可。

 首先我們看儲存佈局狀態的程式碼。

privatevoid MainForm_Closing(object sender, System.ComponentModel.CancelEventArgs e)
        {
            
string configFile = Path.Combine(Path.GetDirectoryName(Application.ExecutablePath), "DockPanel.config");
            
if (m_bSaveLayout)
                dockPanel.SaveAsXml(configFile);
            
elseif (File.Exists(configFile))
                File.Delete(configFile);
        }

 這樣的方法,因為是直接呼叫控制元件本身的儲存,所以應該比較易懂。我們再看看程式啟動的時候,載入還原原有佈局資訊的時候,是如何的。

privatevoid MainForm_Load(object sender, EventArgs e)
        {
            
//載入佈局            m_deserializeDockContent =new DeserializeDockContent(GetContentFromPersistString);
            
string configFile = Path.Combine(Path.GetDirectoryName(Application.ExecutablePath), "DockPanel.config");
            
if (File.Exists(configFile))
            {
                dockPanel.LoadFromXml(configFile, m_deserializeDockContent);
            }
        }
private IDockContent GetContentFromPersistString(string persistString)
        {
            
if (persistString ==typeof(MainToolWindow).ToString())
                
return mainToolWin;
            
elseif (persistString ==typeof(FrmStatus).ToString())
                
return mainStatus;
            
elseif (persistString ==typeof(FrmRoomView).ToString())
                
return frmRoomView;
            
elsereturnnull;
        }

 這樣,我們就可以實現佈局由使用者調整,而不需要怕每次都有那麼一點大,不雅觀了。

我們看程式的根目錄下面生成了一個檔案,叫做DockPanel.config, 我們看看就知道里面的佈局狀態引數了,其中的AutoHidePortion,我們通過自動調整介面,它也就會跟著變化的了,如下面的那個AutoHidePortion="0.179554494828958"就是我調整後的大小。

<Contents Count="7"><Content ID="0" PersistString="DockSample.DummyToolbox" AutoHidePortion="0.25" IsHidden="True" IsFloat="True"/><Content ID="1" PersistString="DockSample.DummySolutionExplorer" AutoHidePortion="0.25" IsHidden="False" IsFloat="False"/><Content ID="2" PersistString="DockSample.DummyPropertyWindow" AutoHidePortion="0.25" IsHidden="False" IsFloat="False"/><Content ID="3" PersistString="DockSample.DummyOutputWindow" AutoHidePortion="0.179554494828958" IsHidden="False" IsFloat="False"/><Content ID="4" PersistString="DockSample.DummyTaskList" AutoHidePortion="0.25" IsHidden="True" IsFloat="True"/><Content ID="5" PersistString="DockSample.DummyDoc,,Document1" AutoHidePortion="0.25" IsHidden="False" IsFloat="False"/><Content ID="6" PersistString="DockSample.DummyDoc,,Document2" AutoHidePortion="0.25" IsHidden="False" IsFloat="False"/></Contents>

 當然如果我們需要 完美的佈局,只需要在釋出前,調整好這些引數給使用者就可以了,是不是很方便呢。