C# 令某個表單可跟著滑鼠移動

來源:互聯網
上載者:User

標籤:

/// <summary>    /// 使視窗的中的指定控制項支援運行時移動    /// TODO:運行時縮放    /// </summary>    public class ControlMoveResize    {        #region 私人成員        bool IsMoving = false;        Point pCtrlLastCoordinate = new Point(0, 0);        Point pCursorOffset = new Point(0, 0);        Point pCursorLastCoordinate = new Point(0, 0);        private Control ctrl = null;        private ScrollableControl Containe = null;        #endregion        #region 私人方法        /// <summary>        /// 在滑鼠左鍵按下的狀態記錄滑鼠當前的位置,以及被移動組件的當前位置        /// </summary>        /// <param name="sender"></param>        /// <param name="e"></param>        private void MouseDown(object sender, MouseEventArgs e)        {            if (Containe == null)            {                return;            }            if (e.Button == MouseButtons.Left)            {                IsMoving = true;                pCtrlLastCoordinate.X = ctrl.Left;                pCtrlLastCoordinate.Y = ctrl.Top;                pCursorLastCoordinate.X = Cursor.Position.X;                pCursorLastCoordinate.Y = Cursor.Position.Y;            }        }        private void MouseMove(object sender, MouseEventArgs e)        {            if (Containe == null)            {                return;            }            if (e.Button == MouseButtons.Left)            {                if (this.IsMoving)                {                    Point pCursor = new Point(Cursor.Position.X, Cursor.Position.Y);                    pCursorOffset.X = pCursor.X - pCursorLastCoordinate.X;                    pCursorOffset.Y = pCursor.Y - pCursorLastCoordinate.Y;                    ctrl.Left = pCtrlLastCoordinate.X + pCursorOffset.X;                    ctrl.Top = pCtrlLastCoordinate.Y + pCursorOffset.Y;                }            }        }        private void MouseUp(object sender, MouseEventArgs e)        {            if (Containe == null)            {                return;            }            if (this.IsMoving)            {                if (pCursorOffset.X == 0 && pCursorOffset.Y == 0)                {                    return;                }                if ((pCtrlLastCoordinate.X + pCursorOffset.X + ctrl.Width) > 0)                {                    ctrl.Left = pCtrlLastCoordinate.X + pCursorOffset.X;                }                else                {                    ctrl.Left = 0;                }                if ((pCtrlLastCoordinate.Y + pCursorOffset.Y + ctrl.Height) > 0)                {                    ctrl.Top = pCtrlLastCoordinate.Y + pCursorOffset.Y;                }                else                {                    ctrl.Top = 0;                }                pCursorOffset.X = 0;                pCursorOffset.Y = 0;            }        }        #endregion        #region 建構函式        /// <summary>        /// 擷取被行動控制項對象和容器物件        /// </summary>        /// <param name="c">被設定為可運行時移動的控制項</param>        /// <param name="parentContain">可行動控制項的容器</param>        public ControlMoveResize(Control c, ScrollableControl parentContain)        {            ctrl = c;            this.Containe = parentContain;            ctrl.MouseDown += new MouseEventHandler(MouseDown);            ctrl.MouseMove += new MouseEventHandler(MouseMove);            ctrl.MouseUp += new MouseEventHandler(MouseUp);        }        #endregion    }

上面是類,以下是調用 的方法

 

傳入的參數為:控制項,控制項容器(即表單)

 ControlMoveResize control = new ControlMoveResize(this.button1,this);

 

C# 令某個表單可跟著滑鼠移動

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在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.