欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页  >  IT编程

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

程序员文章站 2024-02-19 20:16:10
所谓的浮动工具栏,效果图如下: 也就是说,可以将工具栏拖出其原先的停靠位置,而且可以将拖出来的工具栏再拖放回去。 实现的基本思路如下 1、拖动出来以后,需要创建一个大...

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

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;
            }
        }
    }
}