C# ToolStrip制作四边停靠浮动工具栏

 更新时间:2013年12月09日 11:57:50   作者:  
这篇文章主要介绍了C# ToolStrip浮动工具栏的制作,可以上/下/左/右停靠,代码在下面

关于浮动工具条的制作,阿捷写了一篇很不错的文章,见:https://www.jb51.net/article/44272.htm
阿捷这个工具条浮动后只能在顶部停靠,基于此,我在这边增加在左/右/底部停靠,停靠条件是浮动窗体紧贴或越过主窗体边缘。

其实阿捷给出的代码已经相当详细了:) 我这里主要给出重写的ToolStrip代码段,增加了三个ToolStripPanel

复制代码 代码如下:

    public partial class MyToolStrip : ToolStrip
    {
        public MyToolStrip()
        {
            InitializeComponent();
            this.EndDrag += new EventHandler(MyToolStrip_EndDrag);
            this.SizeChanged += new EventHandler(MyToolStrip_SizeChanged);
        }

        #region 漂浮状态

        public ToolStripFloatWindow FloatWindow { get; set; }

        private bool isFloating
        {
            get
            {
                return (FloatWindow != null);
            }
        }

        public ToolStripPanel TopToolStripPanel { get; set; }
        public ToolStripPanel BottomToolStripPanel { get; set; }
        public ToolStripPanel LeftToolStripPanel { get; set; }
        public ToolStripPanel RightToolStripPanel { get; set; }

        #endregion

        #region 漂浮实现

        private void FloatWindow_LocationChanged(object sender, EventArgs e)
        {
            //当floatwindws的位置移动到 toolstrippanel中时,将this放置到 toolstripPanel上
            if (this.FloatWindow == null)
            {
                return;
            }
            if (FloatWindow.HasCreated)
            {
                //主窗体位置
                Point frmLoc = this.TopToolStripPanel.Parent.Location;
                //浮动工具条位置
                Point toolBarLoc = FloatWindow.Location;

                if (toolBarLoc.Y - frmLoc.Y <= 0) //置于顶部StripPanel
                {
                    this.FloatWindow.Controls.Remove(this);
                    this.TopToolStripPanel.SuspendLayout();
                    this.TopToolStripPanel.Controls.Add(this);
                    this.Location = this.TopToolStripPanel.PointToClient(toolBarLoc);
                    this.TopToolStripPanel.ResumeLayout();
                    this.FloatWindow.Dispose();
                    this.FloatWindow = null;
                    return;
                }
                if (toolBarLoc.X - frmLoc.X <= 0) //置于左边StripPanel
                {
                    this.FloatWindow.Controls.Remove(this);
                    this.LeftToolStripPanel.SuspendLayout();
                    this.LeftToolStripPanel.Controls.Add(this);
                    this.Location = this.LeftToolStripPanel.PointToClient(toolBarLoc);
                    this.LeftToolStripPanel.ResumeLayout();
                    this.FloatWindow.Dispose();
                    this.FloatWindow = null;
                    return;
                }
                if (toolBarLoc.X + FloatWindow.Width >= this.TopToolStripPanel.Parent.Width) //置于右边StripPanel
                {
                    this.FloatWindow.Controls.Remove(this);
                    this.RightToolStripPanel.SuspendLayout();
                    this.RightToolStripPanel.Controls.Add(this);
                    this.Location = this.RightToolStripPanel.PointToClient(toolBarLoc);
                    this.RightToolStripPanel.ResumeLayout();
                    this.FloatWindow.Dispose();
                    this.FloatWindow = null;
                    return;
                }
                if (toolBarLoc.Y + FloatWindow.Height >= this.TopToolStripPanel.Parent.Height) //置于底部StripPanel
                {
                    this.FloatWindow.Controls.Remove(this);
                    this.BottomToolStripPanel.SuspendLayout();
                    this.BottomToolStripPanel.Controls.Add(this);
                    this.Location = this.BottomToolStripPanel.PointToClient(toolBarLoc);
                    this.BottomToolStripPanel.ResumeLayout();
                    this.FloatWindow.Dispose();
                    this.FloatWindow = null;
                    return;
                }
            }
        }

        private void MyToolStrip_EndDrag(object sender, EventArgs e)
        {
            Point screenPt = Cursor.Position;
            Point clientPt = this.TopToolStripPanel.Parent.PointToClient(screenPt);

            //浮动区域
            Rectangle floatArea = new Rectangle(32, 32,    //我这里图标大小调整为32*32
                this.TopToolStripPanel.Parent.Width - 2 * 32,
                this.TopToolStripPanel.Parent.Height - 2 * 32);

            if (floatArea.Contains(clientPt)) //判断移出时
            {
                ToolStripFloatWindow fw = new ToolStripFloatWindow();
                fw.Controls.Add(this);
                this.Left = 0;
                this.Top = 0;
                this.FloatWindow = fw;
                FloatWindow.LocationChanged += new EventHandler(FloatWindow_LocationChanged);
                fw.SetBounds(screenPt.X, screenPt.Y, this.ClientSize.Width, this.ClientSize.Height + 22); //22为窗体标题栏高度
                  fw.Show();
             }
        }

        private void MyToolStrip_SizeChanged(object sender, EventArgs e)
        {
            if (this.isFloating)
            {
                this.FloatWindow.Width = this.ClientSize.Width;
            }
        }

        #endregion

    }

相关文章

  • C#之WinForm跨线程访问控件实例

    C#之WinForm跨线程访问控件实例

    这篇文章主要介绍了C#之WinForm跨线程访问控件,实例讲述了跨线程访问控件的简单实现方法与用法,需要的朋友可以参考下
    2014-10-10
  • C#生成漂亮验证码完整代码类

    C#生成漂亮验证码完整代码类

    本文主要介绍了C#生成漂亮验证码的完整代码类。具有很好的参考价值。下面跟着小编一起来看下吧
    2017-03-03
  • C#中的协变与逆变方式

    C#中的协变与逆变方式

    协变和逆变是C#中处理泛型类型参数可变性的两个重要概念,协变允许将派生类型的泛型参数转换为基类型的泛型参数,而逆变允许将基类型的泛型参数转换为派生类型的泛型参数,通过协变和逆变,可以提高代码的灵活性和可重用性,但也需要注意类型参数的限制和安全性
    2024-12-12
  • c#中(&&,||)与(&,|)的区别详解

    c#中(&&,||)与(&,|)的区别详解

    这篇文章主要介绍了c#中(&&,||)与(&,|)的区别详解,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友们下面随着小编来一起学习学习吧
    2019-12-12
  • C#多线程系列之线程池

    C#多线程系列之线程池

    本文详细讲解了C#多线程中的线程池,文中通过示例代码介绍的非常详细。对大家的学习或工作具有一定的参考借鉴价值,需要的朋友可以参考下
    2022-02-02
  • C#实现在匿名方法中捕获外部变量的方法

    C#实现在匿名方法中捕获外部变量的方法

    这篇文章主要介绍了C#实现在匿名方法中捕获外部变量的方法,本文直接给出代码实例,然后分析了代码中的一些知识点,需要的朋友可以参考下
    2015-03-03
  • C# Winform实现捕获窗体最小化、最大化、关闭按钮事件的方法

    C# Winform实现捕获窗体最小化、最大化、关闭按钮事件的方法

    这篇文章主要介绍了C# Winform实现捕获窗体最小化、最大化、关闭按钮事件的方法,可通过重写WndProc来实现,需要的朋友可以参考下
    2014-08-08
  • C#中的委托、事件学习笔记

    C#中的委托、事件学习笔记

    这篇文章主要介绍了C#中的委托、事件学习笔记,本文讲解了委托delegate、事件的相关知识并给出代码实例,需要的朋友可以参考下
    2015-01-01
  • C#实现Winform监控文件夹变化以及监控文件操作教程

    C#实现Winform监控文件夹变化以及监控文件操作教程

    在开发应用程序时,我们可能会因为场景的需要,要对文件系统中的文件或文件夹进行实时监测,以便在文件内容改变、文件被创建、删除或重命名时能够及时做出反应,今天,我将为大家介绍完整的操作流程,让你轻松实现监控文件/文件夹变化的功能,需要的朋友可以参考下
    2024-12-12
  • 一个C#开发者重温C++的心路历程

    一个C#开发者重温C++的心路历程

    作为一个C#开发为什么要重新学习C++呢?因为在C#在很多业务场景需要调用一些C++编写的COM组件,如果不了解C++,那么,很容易。。。注定是要被C++同事忽悠的
    2019-05-05

最新评论