Press "Enter" to skip to content

C#Winform窗口移动–code

大虾 0

在我们将Winform自带的边框隐藏之后,我们需要自己编写窗口的移动。
思路就是1.获得点击左键时当前鼠标的坐标 2.获得移动后鼠标的坐标 3.窗体的坐标=移动后的鼠标坐标-移动前的鼠标坐标

private Point mouseOff;//鼠标移动位置变量         private bool leftFlag;//鼠标是否为左键         private void Form1_MouseDown(object sender, MouseEventArgs e)         {             if(e.Button == MouseButtons.Left)             {                 mouseOff = new Point(-e.X, -e.Y);//获得当前鼠标的坐标                 leftFlag = 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;             }         }

————————————————————————————————————–
文章来源:https://blog.csdn.net/nav/lang
Article from the network, copyright is owned by the original author, if there is infringement, please contact delete.
文章来源于网络,版权属原作者所有,如有侵权请联系删除

发表评论

电子邮件地址不会被公开。 必填项已用*标注