在运行时通过鼠标拖动移动控件位置(c#)

来源:互联网 发布:宣传展板设计软件 编辑:程序博客网 时间:2024/04/30 06:01

前些日子因为工作需要想了解有关于在C#下实现运行时鼠标移动控件的方法,Google了一下还真找到了一个帖子,粗略看去代码还真不少,感觉有点复杂的样子,因为当时手头上还有点别的事情,没来得及细看,就把帖子转到了自己的blog里面收藏。周末晚上没事,抽时间看了一下程序,发现只能以Form为容器进行操作(因为使用了Form的一些属性来确定控件位置),不完全符合自己的需要,而且作者的实现代码似乎多了一点,就自己琢磨着怎么给精简一下,后来竟然把几乎把大部分代码给删掉了,实现方式也有不同,也可以在Form之外的其它容器内进行组件运行时拖动。为了程序可读性,暂未把改变控件大小的代码加进来,但基本原理和移动控件位置也相差不远,稍晚一点再添加进来。

/// <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

原创粉丝点击