from:http://www.codeproject.com/Articles/31840/Move-controls-on-a-form-at-runtime
thank the author a lot 。
本文是根據codeproject中的一個程式修改的。
可以實現,視窗中的控制項滑鼠拖動。really? yes
code:
一個form 。一個類:
form:
效果:
代碼:
public partial class Frm_MoveControl : Form{private Cls_MoveControl MoveControl; public Frm_MoveControl(){ InitializeComponent(); MoveControl = new Cls_MoveControl(this);//this代表視窗,或者panel等容器 MoveControl.ApplyMove(button2); MoveControl.ApplyMove(button3, Cls_MoveControl.MoveDirection.Vertical); MoveControl.ApplyMove(button1,panel1,Cls_MoveControl.MoveDirection.Any);}}
接下來是用於行動控制項的類:
using System;using System.Collections.Generic;using System.Drawing;using System.Text;using System.Windows.Forms; namespace cacheDemo1{class Cls_MoveControl{public enum MoveDirection{ Any, Horizontal, Vertical}private int int_FrmWidth=200;private int int_FrmHeight = 200;public int Int_FrmWidth{ get { return int_FrmWidth; } set { int_FrmWidth = value; }}public int Int_FrmHeight{ get { return int_FrmHeight; } set { int_FrmHeight = value; }}public Cls_MoveControl(int width,int height){ this.int_FrmWidth = width; this.int_FrmHeight = height;}//這裡如果傳進來的只是視窗可以用form。當然也可以用Control類,因為Form也是派上於Control類public Cls_MoveControl(Control frm){ this.int_FrmWidth = frm.ClientRectangle.Width; this.int_FrmHeight = frm.ClientRectangle.Height; frm.Resize += delegate(object sender, EventArgs e) { this.int_FrmWidth = frm.ClientRectangle.Width; this.int_FrmHeight = frm.ClientRectangle.Height; };}/* * frm.ClientRectangle.Width;這裡用ClientRectangle的原因 * 因為ClientRectangle可以得到視窗可用性區域域的大小, * 可以去掉視窗的標題列的高度 * 這裡寫Control的好處是,可以傳入Form。也可以穿容器控制項,如panel */ public void ApplyMove(Control control){ ApplyMove(control, MoveDirection.Any);}public void ApplyMove(Control control, MoveDirection moveDirection){ ApplyMove(control, control, moveDirection);}public void ApplyMove(Control control, Control container, MoveDirection moveDirection){ bool isDrag = false; Point dragStartPoint = Point.Empty; control.MouseDown += delegate(object sender, MouseEventArgs e) { isDrag = true; dragStartPoint = new Point(e.X, e.Y); control.Cursor = Cursors.SizeAll; control.Capture = true; }; control.MouseUp += delegate(object sender, MouseEventArgs e) { isDrag = false; control.Cursor = Cursors.Default; control.Capture = false; }; control.MouseMove += delegate(object sender, MouseEventArgs e) { if (isDrag) //當滑鼠有按下才會為true。一旦滑鼠放開就為false { //橫向移動的只能橫向移動。縱向移動的只能縱向移動。 //任意方向的則都可以移動 if (moveDirection!=MoveDirection.Vertical) //horizontal和any可以進入 { int left=container.Left + e.X - dragStartPoint.X; // container.Left = Math.Max(0, left); //不要超過左邊界 container.Left = GetMiddleValue(left,container.Width,this.int_FrmWidth); } if (moveDirection != MoveDirection.Horizontal) { int top = container.Top + e.Y - dragStartPoint.Y; // container.Top = Math.Max(0, top); container.Top = GetMiddleValue(top, container.Height, this.int_FrmHeight); } } };}/// <summary>/// 控制拖動的控制項不要超出視窗/// </summary>/// <param name="val"></param>/// <param name="containerWOH"></param>/// <param name="FrmWidth"></param>/// <returns></returns>private int GetMiddleValue(int val,int containerWOH,int FrmWOH){ int min = 0; int max = FrmWOH - containerWOH; if (val<min) { val = min; } if (val>max) { val = max; } Console.WriteLine(val); return val;} }}