代码改变世界

C#中实现鼠标拖动窗体的方法

2010-04-03 21:51  jinze  阅读(612)  评论(0编辑  收藏  举报

用C#实现鼠标拖动窗体,方法可能有很多,不过我想最好多还应该是直接记录鼠标的位置,进而完成操作:

private void AddList_MouseDown(object sender, MouseEventArgs e)
        {
            //   Gets   the   x-coordinate   and   y-coordinate   of   a   mouse   click   based   on   the   the   client   area   of   the   form   
            mouse_offset = new Point(-e.X, -e.Y); 
        }

        private void AddList_MouseMove(object sender, MouseEventArgs e)
        {
            if (e.Button == MouseButtons.Left)
            {
                //   Gets   the   position   of   the   mouse   cursor   in   screen   coordinates   
                Point mousePos = Control.MousePosition;
                //   Translates   this   Point   by   the   specified   amount.   
                mousePos.Offset(mouse_offset.X, mouse_offset.Y);
                Location = mousePos;
            }   

        }
用电子邮件联系Andy