码迷,mamicode.com
首页 > Windows程序 > 详细

Winform 窗口拖动

时间:2019-04-12 17:44:25      阅读:152      评论:0      收藏:0      [点我收藏+]

标签:sem   鼠标移动   info   desktop   off   ons   button   lse   ==   

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 窗口拖动

标签:sem   鼠标移动   info   desktop   off   ons   button   lse   ==   

原文地址:https://blog.51cto.com/12268222/2377943

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!