您的位置:首页 > 其它

当去掉WinForm的边框时候,鼠标移动窗口的方法

2013-12-20 11:42 567 查看
【方法一】窗体本身进行

#region 鼠标移动窗口

        // 位置 - 用于记录鼠标位置

        Point downPoint;

        // 当鼠标按下时,记录鼠标位置

        private void MainForm_MouseDown(object sender, MouseEventArgs e)

        {

            //Point FrmP = new Point(button1.Left, button1.Top);  

            ////ScreenP返回相对屏幕的坐标 Point   ScreenP=this.PointToScreen(FrmP);

            downPoint = new Point(e.X, e.Y);

        }

        // 当鼠标移动是,改变窗体位置

        private void MainForm_MouseMove(object sender, MouseEventArgs e)

        {

            if (e.Button == MouseButtons.Left)

                Location = new Point(Location.X + e.X - downPoint.X, Location.Y + e.Y - downPoint.Y);


        }
        #endregion

【方法二】在窗体控件上进行

        #region   //鼠标坐标

        private int xpos;

        private int ypos;

        #region panel1在表面,鼠标移动窗体

        private void panel1_MouseDown(object sender, MouseEventArgs e)

        {

            xpos = e.X;

            ypos = e.Y;

        }

        private void panel1_MouseMove(object sender, MouseEventArgs e)

        {

            if (e.Button == MouseButtons.Left)

            {

                this.Left += e.X - this.xpos;

                this.Top += e.Y - this.ypos;

            }

        }

        #endregion

【方法三】转载于博客园<小锋刚>

窗体的移动:

      bool formMove = false;//窗体是否移动

        Point formPoint;//记录窗体的位置

private void Form4_MouseDown(object sender, MouseEventArgs e)//鼠标按下

        {

            formPoint = new Point();

            int xOffset;

              int yOffset;


            if (e.Button == MouseButtons.Left)

            {

                xOffset = -e.X - SystemInformation.FrameBorderSize.Width;

                yOffset = -e.Y - SystemInformation.CaptionHeight - SystemInformation.FrameBorderSize.Height;

                formPoint = new Point(xOffset, yOffset);


                formMove = true;//开始移动

            }

        }

private void Form4_MouseMove(object sender, MouseEventArgs e)//鼠标移动

        {

            if (formMove == true)

            {

                Point mousePos = Control.MousePosition;

                mousePos.Offset(formPoint.X, formPoint.Y);

                Location = mousePos;

            }

        }

private void Form4_MouseUp(object sender, MouseEventArgs e)//鼠标松开

        {

            if (e.Button == MouseButtons.Left)//按下的是鼠标左键

            {

                formMove = false;//停止移动

            }

        }
        private void panel1_MouseMove(object sender, MouseEventArgs e)

        {

            if (e.Button == MouseButtons.Left)

            {

                this.Left += e.X - this.xpos;

                this.Top += e.Y - this.ypos;

            }

        }

        #endregion

【案例】

拖动无边框窗体Form至桌面任何位置

首先建一个Windows应用程序

将Form1的 FormBorderStyle属性设置为None

        Point mouseOff;//鼠标移动位置变量
        bool leftFlag;//标签是否为左键
        private void Form1_MouseDown(object sender, MouseEventArgs e)

        {

            if (e.Button == MouseButtons.Left)

            {

                mouseOff = new Point(-e.X, -e.Y); //得到变量的值
                leftFlag = true;                  //点击左键按下时标注为true;
            }

        }

        private void Form1_MouseMove(object sender, MouseEventArgs e)

        {

            if (leftFlag)

            {

                Point mouseSet = Control.MousePosition;

                mouseSet.Offset(mouseOff.X, mouseOff.Y);  //设置移动后的位置
                Location = mouseSet;

            }

        }

        private void Form1_MouseUp(object sender, MouseEventArgs e)

        {

            if (leftFlag)

            {

                leftFlag = false;//释放鼠标后标注为false;
            }

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