少有人走的路

勇哥的工业自动化技术网站

WeifenLuo.WinFormsUI.Docking"的使用如何控制自动停靠窗口的大小

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

       [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>

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

发表评论:

◎欢迎参与讨论,请在这里发表您的看法、交流您的观点。

«    2025年12月    »
1234567
891011121314
15161718192021
22232425262728
293031
控制面板
您好,欢迎到访网站!
  查看权限
网站分类
搜索
最新留言
文章归档
网站收藏
友情链接

Powered By Z-BlogPHP 1.7.3

Copyright www.skcircle.com Rights Reserved.

鄂ICP备18008319号


站长QQ:496103864 微信:abc496103864