// Add an event
This. mouseup + = new system. Windows. Forms. mouseeventhandler (this. dragend );
This. mousemove + = new system. Windows. Forms. mouseeventhandler (this. dragmove );
This. mousedown + = new system. Windows. Forms. mouseeventhandler (this. dragbegin );
Private Point P1; // The Screen coordinate of the cursor before dragging
Private point P2; // The Screen coordinate of the mouse after dragging
Private bool _ isdrag; // whether it is being dragged
Private bool _ allowdragmove;
/// <Summary>
/// Whether to allow dragging.
/// </Summary>
Public bool allowdragmove
{
Get
{
Return this. _ allowdragmove;
}
Set
{
This. _ allowdragmove = value;
}
}
Private void dragbegin (Object sender, system. Windows. Forms. mouseeventargs E)
{
If (E. Button = mousebuttons. Left & this. allowdragmove)
{
This. _ isdrag = true;
// Record the coordinates before dragging
This. p1 = This. pointtoscreen (new point (E. X, E. y ));
}
}
Private void dragend (Object sender, system. Windows. Forms. mouseeventargs E)
{
If (E. Button = mousebuttons. Left & this. _ isdrag)
{
This. _ isdrag = false;
// Record the coordinates After dragging
This. P2 = This. pointtoscreen (new point (E. X, E. y ));
// Calculates the displacement.
Int x = p2.x-p1.x;
Int y = p2.y-p1.y;
// Move the widget location
This. Left + = X;
This. Top + = y;
}
}
Private void dragmove (Object sender, system. Windows. Forms. mouseeventargs E)
{
If (this. _ isdrag)
{
// Record the coordinates After dragging
This. P2 = This. pointtoscreen (new point (E. X, E. y ));
// Calculates the displacement.
Int x = p2.x-p1.x;
Int y = p2.y-p1.y;
// Move the widget location
This. Left + = X;
This. Top + = y;
// Use the dragged vertex as the new vertex before dragging
P1 = P2;
}
}