//代碼比較簡單,就不多解析了。
#region 移動窗體保存數據 Point mouseOff;//鼠標移動位置變量 bool leftFlag; //標志是否為左鍵 bool largeFlag; //標志是否同時改變寬度及高度 bool widthFlag; //標志是否改變寬度 bool heightFlag;//標志是否改變高度 /// <summary> /// 鼠標移動 /// </summary> /// <param name="sender"></param> /// <param name="e"></param> private void Form1_MouseMove(object sender, MouseEventArgs e) { Point mouseSet = Control.MousePosition; if (leftFlag) { //改變寬高 if (largeFlag) { int dw = mouseSet.X - this.Location.X; int dh = mouseSet.Y - this.Location.Y; this.Width = dw; this.Height = dh; } //改變寬度 else if (widthFlag) { int dw = mouseSet.X - this.Location.X; this.Width = dw; } //改變高度 else if (heightFlag) { int dh = mouseSet.Y - this.Location.Y; this.Height = dh; } //移動位置 else { mouseSet.Offset(mouseOff.X, mouseOff.Y); //設置移動后的位置 this.Location = mouseSet; } } else { //設置改變窗體寬高的標志 if ((this.Location.X + this.Width - mouseSet.X) < 10 && (this.Location.Y + this.Height - mouseSet.Y) < 10) { this.Cursor = Cursors.SizeNWSE; largeFlag = true; } //設置改變窗體寬度的標志 else if ((this.Location.X + this.Width - mouseSet.X) < 10) { this.Cursor = Cursors.SizeWE; widthFlag = true; } //設置改變窗體高度的標志 else if ((this.Location.Y + this.Height - mouseSet.Y) < 10) { this.Cursor = Cursors.SizeNS; heightFlag = true; } //設置移動位置的標志 else { this.Cursor = Cursors.Default; largeFlag = false; widthFlag = false; heightFlag = false; } } } /// <summary> /// 鼠標按下 /// </summary> /// <param name="sender"></param> /// <param name="e"></param> private void Form1_MouseDown(object sender, MouseEventArgs e) { if (e.Button == MouseButtons.Left) { mouseOff = new Point(-e.X, -e.Y); //得到變量的值 leftFlag = true; //點擊左鍵按下時標注為true; } } /// <summary> /// 鼠標彈起 /// </summary> /// <param name="sender"></param> /// <param name="e"></param> private void Form1_MouseUp(object sender, MouseEventArgs e) { if (leftFlag) { leftFlag = false;//釋放鼠標后標注為false; } } #endregion