The Methods subtract () and add () in the struct point can be used to calculate the relative positions between two points in the same coordinate system:
Public static point subtract (
Point PT,
Size SZ
)
Public static point add (
Point PT,
Size SZ
)
The size can be converted by point, as shown in the following constructor:
Public size (
Point PT
)
Label1 and label2 are two components in the same container (form1). When label1 is dragged, label2 is also moved.
1. Define global fields:
Private point label1startpos, offsetpoint;
Private bool canmove = false;
2. Related event methods:
Private void labeldomainmousedown (Object sender, mouseeventargs E)
{
Canmove = true;
Label1startpos = E. location;
Offsetpoint = point. Subtract (label2.location, new size (label1.location ));
}
Private void labeldomainmouseup (Object sender, mouseeventargs E)
{
Canmove = false;
}
Private void label1_mousemove (Object sender, mouseeventargs E)
{
If (canmove)
{
Label1.location = This. pointtoclient (point. Subtract (label1.pointtoscreen (E. Location), new size (label1startpos )));
Label2.location = point. Add (label1.location, new size (offsetpoint ));
}
}
You can.