實現窗體的拖放

 public Point CPoint;  //添加命名空間using System.Drawing;

 

#region  利用窗體上的控件移動窗體
        /// <summary>
        /// 利用控件移動窗體
        /// </summary>
        /// <param Frm="Form">窗體</param>
        /// <param e="MouseEventArgs">控件的移動事件</param>
        public void FrmMove(Form Frm, MouseEventArgs e)  //Form或MouseEventArgs添加命名空間using System.Windows.Forms;
        {
            if (e.Button == MouseButtons.Left)
            {
                Point myPosittion = Control.MousePosition;//獲取當前鼠標的屏幕座標
                myPosittion.Offset(CPoint.X, CPoint.Y);//重載當前鼠標的位置
                Frm.DesktopLocation = myPosittion;//設置當前窗體在屏幕上的位置
            }
        }
        #endregion


        private void pictureBox1_MouseDown(object sender, MouseEventArgs e)
        {
            this.CPoint = new Point(-e.X, -e.Y);
        }

        private void pictureBox1_MouseMove(object sender, MouseEventArgs e)
        {
            this.FrmMove(this, e);
        }

 

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