public partial class Desktop : Form
{
private Point mPoint = new Point();//记录鼠标指针的坐标
private bool isMouseDown = false; //记录鼠标按键是否按下
public Desktop()
{
InitializeComponent();
Init();
}
public void Init()
{
//鼠标按下事件
this.MouseDown += Desktop_MouseDown;
//鼠标移动事件
this.MouseMove += Desktop_MouseMove;
this.MouseUp += Desktop_MouseUp;
}
private void Desktop_MouseUp(object sender, MouseEventArgs e)
{
// 修改鼠标状态isMouseDown的值
// 确保只有鼠标左键按下并移动时,才移动窗体
if (e.Button == MouseButtons.Left)
{
isMouseDown = false;
}
}
private void Desktop_MouseMove(object sender, MouseEventArgs e)
{
if (isMouseDown)
{
Point myPosittion = MousePosition;
myPosittion.Offset(-mPoint.X, -mPoint.Y);
Location = myPosittion;
}
}
private void Desktop_MouseDown(object sender, MouseEventArgs e)
{
if (e.Button == MouseButtons.Left)
{
mPoint.X = e.X;
mPoint.Y = e.Y;
isMouseDown = true;
}
}
}
新闻标题:Winform窗口拖动
网页地址:https://www.cdcxhl.com/article10/gghedo.html
成都网站建设公司_创新互联,为您提供网页设计公司、小程序开发、动态网站、微信小程序、App设计、定制开发
声明:本网站发布的内容(图片、视频和文字)以用户投稿、用户转载内容为主,如果涉及侵权请尽快告知,我们将会在第一时间删除。文章观点不代表本网站立场,如需处理请联系客服。电话:028-86922220;邮箱:631063699@qq.com。内容未经允许不得转载,或转载时需注明来源: 创新互联