您的位置:首页 > 编程语言 > C#

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

2010-03-17 14:40 537 查看
前些日子因为工作需要想了解有关于在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

 
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: