C# ToolStrip製作四邊停靠浮動工具列

來源:互聯網
上載者:User

C# ToolStrip製作四邊停靠浮動工具列

 這篇文章主要介紹了C# ToolStrip浮動工具列的製作,可以上/下/左/右停靠,代碼在下面

關於浮動工具條的製作

 

其實阿捷給出的代碼已經相當詳細了:) 我這裡主要給出重寫的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

 

    }

 

相關文章

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.