C#無邊框窗體移動

 

 private bool isMouseDown = false;
        private Point FormLocation;     //form的location
        private Point mouseOffset;      //鼠標的按下位置
        private void frmCity_MouseDown(object sender, MouseEventArgs e)
        {
            try
            {
                if (e.Button == MouseButtons.Left)
                {
                    isMouseDown = true;
                    FormLocation = this.Location;
                    mouseOffset = Control.MousePosition;
                }
            }
            catch (Exception)
            {

            }
        }
        private void frmCity_MouseMove(object sender, MouseEventArgs e)
        {
            try
            {
                int _x = 0;
                int _y = 0;
                if (isMouseDown)
                {
                    Point pt = Control.MousePosition;
                    _x = mouseOffset.X - pt.X;
                    _y = mouseOffset.Y - pt.Y;
                    this.Location = new Point(FormLocation.X - _x, FormLocation.Y - _y);
                }
            }

            catch (Exception)
            {
            }
        }

        private void frmCity_MouseUp(object sender, MouseEventArgs e)
        {
            try
            {
                isMouseDown = false;
            }
            catch (Exception)
            {
            }
        }

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章