Copy codeThe Code is as follows: using System. Windows. Forms;
Namespace WinFormsApp_DragControls
{
Public class DragControl
{
// Controls to be dragged
Private Control m_Control;
// X and y coordinates when the mouse is pressed
Private int m_X;
Private int m_Y;
Public DragControl (Control control)
{
M_Control = control;
M_Control.MouseDown + = new MouseEventHandler (control_MouseDown );
M_Control.MouseMove + = new MouseEventHandler (contro_MouseMove );
}
Private void control_MouseDown (object sender, MouseEventArgs e)
{
M_X = e. X;
M_Y = e. Y;
}
Private void contro_MouseMove (object sender, MouseEventArgs e)
{
If (e. Button = MouseButtons. Left)
{
Int x = e. X-m_X;
Int y = e. Y-m_Y;
This. m_Control.Left + = x;
This. m_Control.Top + = y;
}
}
}
}
Call:
DragControl obj1 = new DragControl (button1 );
It indicates that you can drag button1 at Will on the running interface.
In addition, you can further adjust the widget size, use GDI + to add a boundary foot point, save the widget location to xml, and then read (layout) and automatically layout N Control algorithms, for more information, please contact me ..