111,126
社区成员
发帖
与我相关
我的任务
分享
private Point m_OldPoint;
private Point m_OldLocation;
private void label1_MouseDown(object sender, MouseEventArgs e)
{
Control ctr = sender as Control;
this.m_OldPoint = this.PointToScreen(e.Location);
m_OldLocation = ctr.Location;
}
private void label1_MouseMove(object sender, MouseEventArgs e)
{
if (e.Button == MouseButtons.Left)
{
Control ctr = sender as Control;
Point nPoint = this.PointToScreen(e.Location);
Point n = ctr.Location;
n.Offset(nPoint.X - this.m_OldPoint.X, nPoint.Y - this.m_OldPoint.Y);
ctr.Location = n;
}
}
private void label1_MouseUp(object sender, MouseEventArgs e)
{
Control ctr = sender as Control;
Point p = ctr.Location;
Point op = m_OldLocation;
if (p.X != op.X && p.Y!=op.Y)
{
float k = (p.Y - op.Y) / ((p.X - op.X) * 1.0f);
if (p.X < op.X)
{
for (int x = p.X; x < op.X; x++)
{
float y = k * (x - p.X) + p.Y;
ctr.Location = new Point(x, (int)y);
}
}
else
{
for (int x = p.X; x > op.X; x--)
{
float y = k * (x - p.X) + p.Y;
ctr.Location = new Point(x, (int)y);
}
}
}
}