再谈布局控件"WeifenLuo.WinFormsUI.Docking"的使用--如何控制自动停靠窗口的大小

在上篇文章《WinForm界面开发之布局控件"WeifenLuo.WinFormsUI.Docking"的使用》,我介绍了如何在程序界面中使用WeifenLuo.WinFormsUI.Docking这个优秀的布局控件,这款软件目前我还没有找出比他更好的免费控件了,不知WeifenLuo其人如何,不过东西确实不错,赞一个。

在使用这个控件的时候,估计大家都会碰到 这样一个问题,就是当窗口是自动隐藏的时候,好像出来的大小一般比实际的大,感觉不太美观,有没有什么方法可以控制它的呢,答案是当然有了,其实实现起来也很简单。

首先我们来看看其内部机制是如何实现的,因为该控件有一些属性,专门用来控制窗口的比例的。我们在该控件的源码上看到DocingPanel类中有这么一个属性,是用来控制自动隐藏窗口的缩放比例的。

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

        } 

 

 默认的大小是0.25,这个参数是可以修改的。因为控件提供了一个保存布局状态的方法,它默认是没有保存的,如果需要记住调整的窗口大小(当然这是一种最好的方法),那么只需要加上几段代码即可。

 首先我们看保存布局状态的代码。

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

 

 这样的方法,因为是直接调用控件本身的保存,所以应该比较易懂。我们再看看程序启动的时候,加载还原原有布局信息的时候,是如何的。

        private void 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;
            
else if (persistString == typeof(FrmStatus).ToString())
                
return mainStatus;
            
else if (persistString == typeof(FrmRoomView).ToString())
                
return frmRoomView;
            
else
                
return null;
        }

 

 这样,我们就可以实现布局由用户调整,而不需要怕每次都有那么一点大,不雅观了。

我们看程序的根目录下面生成了一个文件,叫做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>

 

 当然如果我们需要 完美的布局,只需要在发布前,调整好这些参数给用户就可以了,是不是很方便呢。

 

posted on 2009-10-20 20:40  伍华聪  阅读(12951)  评论(18编辑  收藏  举报

导航