c#创建浮动工具栏功能示例

时间:2022-08-31 01:08:31 

所谓的浮动工具栏,效果图如下:

c#创建浮动工具栏功能示例

也就是说,可以将工具栏拖出其原先的停靠位置,而且可以将拖出来的工具栏再拖放回去。

实现的基本思路如下

1、拖动出来以后,需要创建一个大小合适的窗口,作为工具栏新的停靠容器,这个窗口可以这样设置:

FormBorderStyle = System.Windows.Forms.FormBorderStyle.FixedToolWindow;

ShowIcon = false;

ShowInTaskbar = false;

TopMost = true;

2、浮动工具栏可以扩展自.Net Framework提供的ToolStrip,它被拖动都某个位置,松开鼠标左键时,会触发EndDarg事件,在这个事件中,我们将其从原来的停靠容器中移除,同时根据鼠标左键松开时,在鼠标所在位置上创建一个窗口,作为工具栏的新容器。

这个就是基本的思路了,下面是浮动工具栏FloatToolstrip 具体的实现代码:


代码

Code highlighting produced by Actipro CodeHighlighter (freeware)http://www.CodeHighlighter.com/-->using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Drawing;
using System.Data;
using System.Text;
using System.Windows.Forms;

namespace FloatToolStripDemo
{
    public partial class FloatToolstrip : ToolStrip
    {
        private ToolStripPanel tsPanel;

        public FloatToolstrip()
        {
            InitializeComponent();
            this.EndDrag += new EventHandler(MyToolStrip_EndDrag);
            this.SizeChanged += new EventHandler(MyToolStrip_SizeChanged);
        }

        private ToolStripFloatWindow floatForm;
        public ToolStripFloatWindow FloatForm
        {
            get { return floatForm; }
            set
            {
                floatForm = value;
                if (floatForm != null)
                {
                    floatForm.LocationChanged += new EventHandler(floatForm_LocationChanged);
                    floatForm.FormClosing += new FormClosingEventHandler(floatForm_FormClosing);
                }
            }
        }

        void floatForm_FormClosing(object sender, FormClosingEventArgs e)
        {
            e.Cancel = true;
        }

        private void floatForm_LocationChanged(object sender, EventArgs e)
        {
            //当floatwindws的位置移动到toolstrippanel中时,将this放置到 toolstripPanel上 
            if (this.floatForm == null)
            {
                return;
            }
            else
            {
                if (floatForm.HasCreated)
                {
                    Point currentPt = new Point(floatForm.Location.X, floatForm.Location.Y);
                    Point minPt = this.tsPanel.PointToScreen(tsPanel.Location);
                    Point maxPt;
                    if (this.tsPanel.Height <= 20)
                    {
                        maxPt = new Point(minPt.X + this.tsPanel.Width, minPt.Y + 20);

                    }
                    else
                    {
                        maxPt = new Point(minPt.X + this.tsPanel.Width, minPt.Y + this.tsPanel.Height);
                    }

                    if ((currentPt.X > minPt.X) && (currentPt.X < maxPt.X) && (currentPt.Y > minPt.Y - 25) && (currentPt.Y < maxPt.Y - 25))
                    {
                        this.floatForm.Controls.Remove(this);
                        this.tsPanel.SuspendLayout();
                        this.tsPanel.Controls.Add(this);
                        this.Location = this.tsPanel.PointToClient(currentPt);
                        this.tsPanel.ResumeLayout();
                        this.floatForm.Dispose();
                        this.floatForm = null;
                    }
                }
            }
        }

        public bool isFloating
        {
            get
            {
                return (floatForm != null);
            }
        }

        public ToolStripPanel ToolStripPanel
        {
            get
            {
                return this.tsPanel;
            }
            set
            {
                this.tsPanel = value;
            }
        }

        private void MyToolStrip_EndDrag(object sender, EventArgs e)
        {
            //判断移除时
            if (this.tsPanel == null)
            {
                MessageBox.Show("请先设置ToolStripPanel属性");
                return;
            }
            Point dockPoint = Cursor.Position;
            int openX, openY;
            openX = dockPoint.X;
            openY = dockPoint.Y;

            Point clientPt = this.tsPanel.Parent.PointToClient(dockPoint);
            if (clientPt.Y > tsPanel.Height)
            {
                ToolStripFloatWindow tsfw = new ToolStripFloatWindow();
                this.tsPanel.Controls.Remove(this);
                tsfw.Controls.Add(this);
                this.Left = 0;
                this.Top = 0;
                this.FloatForm = tsfw;
                Point newLoc = new Point(openX, openY);
                tsfw.Show();
                tsfw.Location = newLoc;
                tsfw.SetBounds(newLoc.X, newLoc.Y, this.ClientSize.Width, this.ClientSize.Height+25);
            }
        }

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

标签:浮动工具栏,c#
0
投稿

猜你喜欢

  • C#下使用XmlDocument操作XML详解

    2022-08-27 16:38:53
  • Android 仿余额宝数字跳动动画效果完整代码

    2021-06-02 16:53:41
  • Java实现按比抽奖功能

    2023-11-11 13:12:30
  • Android检测手机多点触摸点数的方法

    2023-03-10 11:08:35
  • Spring Boot整合MyBatis操作过程

    2022-08-02 13:50:22
  • Java IO流之字符流的使用详解

    2023-10-18 11:28:08
  • C#实现简单俄罗斯方块

    2023-06-18 07:18:36
  • android手机获取gps和基站的经纬度地址实现代码

    2022-04-05 03:03:00
  • IntelliJ IDEA 2021.1 推出语音、视频功能,边写代码边聊天(功能超级强大)

    2023-09-13 23:30:37
  • C#字符串和Acsii码相互转换

    2022-09-24 00:12:07
  • javaweb前端向后端传值的几种方式总结(附代码)

    2022-04-28 09:21:29
  • Spring Security账户与密码验证实现过程

    2023-03-04 21:54:37
  • 你是不是这样写异常处理代码的呢?

    2022-08-08 02:10:10
  • Java超详细分析泛型与通配符

    2023-07-28 08:34:26
  • 基于Java语言实现Socket通信的实例

    2021-08-06 17:17:50
  • Android实现单页面浮层可拖动view的示例代码

    2023-05-25 16:41:03
  • Windows7下的Java运行环境搭建过程图解

    2022-03-14 19:40:59
  • Java中tomcat memecached session 共享同步问题的解决办法

    2021-12-26 14:22:54
  • Java8深入学习之熟透Optional

    2023-08-24 21:27:54
  • C# Dockpanel入门基础必看篇

    2023-02-24 18:50:42
  • asp之家 软件编程 m.aspxhome.com