//Click and hold the mouse button and the control follows the move. The cast does not move//the controls here can be replaced with other controls. usingSystem;usingSystem.Collections.Generic;usingSystem.ComponentModel;usingSystem.Data;usingSystem.Drawing;usingSystem.Linq;usingSystem.Text;usingSystem.Windows.Forms;namespacetest{ Public Partial classForm1:form {Private intAx, Ay;//define two variables to store the horizontal ordinate of the mouse PublicForm1 () {InitializeComponent (); } /// <summary> ///Mouse First click Control, record mouse coordinates/// </summary> /// <param ax= "int" >Mouse Horizontal</param> /// <param ay= "int" >Mouse Ordinate</param> Private voidButton1_mousedown (Objectsender, MouseEventArgs e) {Ax=e.x; Ay=e.y; } /// <summary> ///when the mouse moves, the control follows the mouse movement. Calculate with relative coordinates/// </summary> /// <param name= "Sender" ></param> /// <param name= "E" ></param> Private voidButton1_mousemove (Objectsender, MouseEventArgs e) { if(E.button = =mousebuttons.left) {button1. Left= button1. Left + (e.x-Ax); Button1. Top= button1. Top + (E.Y-ay); } } }}
Example of C # Mouse control control movement