我们专注攀枝花网站设计 攀枝花网站制作 攀枝花网站建设
成都网站建设公司服务热线:400-028-6601

网站建设知识

十年网站开发经验 + 多家企业客户 + 靠谱的建站团队

量身定制 + 运营维护+专业推广+无忧售后,网站问题一站解决

Winform窗口拖动

 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窗口拖动
网站URL:http://shouzuofang.com/article/gghedo.html

其他资讯