我找到了例子,大家共乡一下, private void initProperty() { foreach(Control aa in this.Controls) { //aa.MouseEnter += new System.EventHandler(lmMouseEnter); aa.MouseUp += new System.Windows.Forms.MouseEventHandler(lmMouseUp); aa.MouseDown += new System.Windows.Forms.MouseEventHandler(lmMouseDown); } } private void lmMouseDown(object sender, System.Windows.Forms.MouseEventArgs e) { oldX=e.X; oldY=e.Y;
// this.textBox3.Text=sender.GetType().Name.ToString(); }
private void lmMouseUp(object sender, System.Windows.Forms.MouseEventArgs e) { int nX,nY; nX=e.X; nY=e.Y; string oType=sender.GetType().Name.ToString(); switch (oType) { //case "PictureBox": //(sender as PictureBox).Left+=nX-oldX; //(sender as PictureBox).Top+=nY-oldY; //break;
//case "Button": //(sender as Button).Left+=nX-oldX; //(sender as Button).Top+=nY-oldY; //break; case "TextBox": (sender as TextBox).Left+=nX-oldX; (sender as TextBox).Top+=nY-oldY; break; //case "Label": //(sender as Label).Left+=nX-oldX; //(sender as Label).Top+=nY-oldY; //break; default: break; }
oldX=-1; oldY=-1; }