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

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

程序员文章站 2024-02-23 10:39:28
关于浮动工具条的制作,阿捷写了一篇很不错的文章,见:阿捷这个工具条浮动后只能在顶部停靠,基于此,我在这边增加在左/右/底部停靠,停靠条件是浮动窗体紧贴或越过主窗体边缘。其实...

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

其实阿捷给出的代码已经相当详细了:) 我这里主要给出重写的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

    }