The basic principle of implementing control drag is to capture the mouse position, and determine the amplitude and timing of the control movement based on the pressing and release of the mouse button.
Simple example:
There is a Button in the Grid that adapts the Button's Margin property through the mouse event, thereby changing the relative position of the Button in the Grid.
<Grid Name="gd"> <Button Width=90 Height=30 Name="btn">button</Button> </Grid>
Three events are bound to Button control: mouse press, mouse move, mouse release
public SystemMap() { InitializeComponent(); += btn_MouseLeftButtonDown; += btn_MouseMove; += btn_MouseLeftButtonUp; }
Define variable + mouse press event
Point pos = new Point(); void btn_MouseLeftButtonDown(object sender, MouseButtonEventArgs e) { Button tmp = (Button)sender; pos = (null); (); = ; }
Mouse movement event
void btn_MouseMove(object sender, MouseEventArgs e) { if (==) { Button tmp = (Button)sender; double dx = (null).X - + ; double dy = (null).Y - + ; = new Thickness(dx, dy, 0, 0); pos = (null); } }
Mouse release event
void btn_MouseLeftButtonUp(object sender, MouseButtonEventArgs e) { Button tmp = (Button)sender; (); }
The above is all the content of this article. I hope it will be helpful to everyone's study and I hope everyone will support me more.