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

 更新时间:2013年12月09日 11:33:12   作者:  
这篇文章主要介绍了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;
            }
        }
    }
}

相关文章

  • 使用GetInvalidFileNameCharts生成文件名

    使用GetInvalidFileNameCharts生成文件名

    这篇文章主要介绍了一个很实用的函数Path.GetInvalidFileNameCharts(),他可以很方便的生成一个有效的文件名称
    2014-01-01
  • C# WinForm中禁止改变窗口大小的方法

    C# WinForm中禁止改变窗口大小的方法

    这篇文章主要介绍了C# WinForm中禁止改变窗口大小的方法,需要把FormBorderStyle和MaximizeBox的值固定即可,需要的朋友可以参考下
    2014-08-08
  • C#使用channel实现Plc异步任务之间的通信

    C#使用channel实现Plc异步任务之间的通信

    在C#的并发编程中,Channel是一种非常强大的数据结构,用于在生产者和消费者之间进行通信,本文将给大家介绍C#使用channel实现Plc异步任务之间的通信,文中有相关的代码示例供大家参考,感兴趣的朋友跟着小编一起来看看吧
    2024-05-05
  • C# 创建MDB数据库、并存放表格数据的案例

    C# 创建MDB数据库、并存放表格数据的案例

    这篇文章主要介绍了C# 创建MDB数据库、并存放表格数据的案例,具有很好的参考价值,希望对大家有所帮助。一起跟随小编过来看看吧
    2021-01-01
  • WPF实现雷达图(仿英雄联盟)的示例代码

    WPF实现雷达图(仿英雄联盟)的示例代码

    这篇文章主要介绍了如何利用WPF实现雷达图(仿英雄联盟)的绘制,文中的示例代码讲解详细,对我们学习或工作有一定帮助,需要的可以参考一下
    2022-07-07
  • C#在MEF框架中手动导入依赖模块

    C#在MEF框架中手动导入依赖模块

    这篇文章介绍了C#在MEF框架中手动导入依赖模块的方法,文中通过示例代码介绍的非常详细。对大家的学习或工作具有一定的参考借鉴价值,需要的朋友可以参考下
    2022-06-06
  • .Net多进程通信共享内存映射文件Memory Mapped

    .Net多进程通信共享内存映射文件Memory Mapped

    这篇文章主要介绍了.Net多进程通信共享内存映射文件Memory Mapped的使用示例,有需要的朋友可以借鉴参考下,希望能够有所帮助,祝大家多多进步,早日升职加薪
    2022-04-04
  • C#实现软件开机自动启动的两种常用方法总结

    C#实现软件开机自动启动的两种常用方法总结

    这篇文章主要为大家详细介绍了C#实现软件开机自动启动的两种常用方法,文中的示例代码讲解详细,具有一定的学习价值,感兴趣的小伙伴可以了解一下
    2023-07-07
  • C#使用Object类实现栈的方法详解

    C#使用Object类实现栈的方法详解

    这篇文章主要介绍了C#使用Object类实现栈的方法,详细分析了栈的原理及使用Object类实现栈的相关技巧与注意事项,需要的朋友可以参考下
    2016-06-06
  • C#中基于流的XML文件操作笔记

    C#中基于流的XML文件操作笔记

    这篇文章主要介绍了C#中基于流的XML文件操作笔记,本文主要是讲解针对XmlReader和XmlWriters两个XML读写类的使用,需要的朋友可以参考下
    2015-06-06

最新评论