/// <summary> ///enables a specified control in a window to support run-time movement///TODO: Run-time scaling/// </summary> Public classControlmoveresize {#regionPrivate membersBOOLIsmoving =false; Point Pctrllastcoordinate=NewPoint (0,0); Point Pcursoroffset=NewPoint (0,0); Point Pcursorlastcoordinate=NewPoint (0,0); PrivateControl CTRL =NULL; PrivateScrollableControl Containe =NULL; #endregion #regionPrivate methods/// <summary> ///record the current position of the mouse and the current position of the moved component in the left mouse button pressed state/// </summary> /// <param name= "Sender" ></param> /// <param name= "E" ></param> Private voidMouseDown (Objectsender, MouseEventArgs e) { if(Containe = =NULL) { return; } if(E.button = =mousebuttons.left) {ismoving=true; Pctrllastcoordinate.x=Ctrl. Left; Pctrllastcoordinate.y=Ctrl. Top; Pcursorlastcoordinate.x=cursor.position.x; Pcursorlastcoordinate.y=CURSOR.POSITION.Y; } } Private voidMouseMove (Objectsender, MouseEventArgs e) { if(Containe = =NULL) { return; } if(E.button = =mousebuttons.left) {if( This. ismoving) {Point pcursor=NewPoint (cursor.position.x, CURSOR.POSITION.Y); Pcursoroffset.x= Pcursor.x-pcursorlastcoordinate.x; Pcursoroffset.y= Pcursor.y-pcursorlastcoordinate.y; Ctrl. Left= pctrllastcoordinate.x +Pcursoroffset.x; Ctrl. Top= Pctrllastcoordinate.y +Pcursoroffset.y; } } } Private voidMouseUp (Objectsender, MouseEventArgs e) { if(Containe = =NULL) { return; } if( This. ismoving) {if(Pcursoroffset.x = =0&& Pcursoroffset.y = =0) { return; } if((pctrllastcoordinate.x + pcursoroffset.x + CTRL.) Width) >0) {Ctrl. Left= pctrllastcoordinate.x +Pcursoroffset.x; } Else{Ctrl. Left=0; } if((pctrllastcoordinate.y + Pcursoroffset.y + CTRL.) Height) >0) {Ctrl. Top= Pctrllastcoordinate.y +Pcursoroffset.y; } Else{Ctrl. Top=0; } pcursoroffset.x=0; Pcursoroffset.y=0; } } #endregion #regionconstructor function/// <summary> ///gets the moved control object and container object/// </summary> /// <param name= "C" >controls that are set to be moved when run</param> /// <param name= "Parentcontain" >containers for removable controls</param> Publiccontrolmoveresize (Control C, ScrollableControl parentcontain) {Ctrl=C; This. Containe =Parentcontain; Ctrl. MouseDown+=NewMouseEventHandler (MouseDown); Ctrl. MouseMove+=NewMouseEventHandler (MouseMove); Ctrl. MouseUp+=NewMouseEventHandler (MouseUp); } #endregion }
The above is the class, and the following method is called
The parameters passed in are: control, control container (i.e. form)
New Controlmoveresize (this. Button1,this);
C # enables a form to move with the mouse