winform拖动窗体

来源:互联网 发布:java爬虫爬取视频 编辑:程序博客网 时间:2024/06/07 09:54

建立一个windows窗体,名字为login,把下面方法加进代码  

     #region 拖动方法

        int LeftTolerance = 0;
        int TopTolerance = 0;
        Point formLocation = new Point(0, 0);
        private void Login_MouseDown(object sender, MouseEventArgs e)
        {
            LeftTolerance = Cursor.Position.X - this.Location.X;
            TopTolerance = Cursor.Position.Y - this.Location.Y;
            this.Cursor = Cursors.SizeAll;
        }


        private void Login_MouseMove(object sender, MouseEventArgs e)
        {
            //拖动中
            if (e.Button == MouseButtons.Left)
            {
                formLocation.X = Cursor.Position.X - LeftTolerance;
                formLocation.Y = Cursor.Position.Y - TopTolerance;
                this.Location = formLocation;
            }
        }

        private void Login_MouseUp(object sender, MouseEventArgs e)
        {
            this.Cursor = Cursors.Default;
        }
        #endregion
原创粉丝点击