C〓Tool Stripは四辺寄港フローティングツールバーを作ります。


フローティングツールバーの作成について、アリさんはいい文章を書いています。見てください。https://www.jb51.net/article/44272.htmアッ捷というツールストリップが浮動した後、上の方にしか停まらないです。これに基づいて、左/右/底の方に停まることを追加します。停泊条件はフロートフォームがメインフォームのエッジにぴったりくっついたり、越えたりすることです。実は阿捷が与えたコードはかなり詳しくなりました。ここでは主に書き換えたToolStripコードセグメントを提供します。3つのTool StripPanel

    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

    }

を追加しました。