温馨提示×

温馨提示×

您好,登录后才能下订单哦!

密码登录×
登录注册×
其他方式登录
点击 登录注册 即表示同意《亿速云用户服务条款》

Winform 窗口拖动

发布时间:2020-07-13 10:44:17 来源:网络 阅读:10192 作者:AESCR 栏目:编程语言
 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;
            }
        }

    }
向AI问一下细节

免责声明:本站发布的内容(图片、视频和文字)以原创、转载和分享为主,文章观点不代表本网站立场,如果涉及侵权请联系站长邮箱:is@yisu.com进行举报,并提供相关证据,一经查实,将立刻删除涉嫌侵权内容。

AI