高手请进,请问怎么实现通过鼠标同时画三条平行线?
我现实现用鼠标选定起点后,移动鼠标自动在平面上画三条平行线,中间一条的起点为鼠标选定点,另外两条可以设定间距后随鼠标移动自动划线,也就是用鼠标在平面任意地方点一下,拖动鼠标就可以画三条平行线,请问怎么实现?
------解决方案--------------------from
http://blog.sina.com.cn/u/589d32f501000aa3
private Point downPoint = Point.Empty; // 鼠标按下的坐标
private Point movePoint = Point.Empty; // 鼠标移动的坐标
private double lineSpace = 12.0f; // 线条之间的距离
private Point offsetPointA = Point.Empty; // 偏移坐标A
private Point offsetPointB = Point.Empty; // 偏移坐标B
private double PointToAngle(Point AOrigin, Point APoint)
{
if (APoint.X == AOrigin.X)
if (APoint.Y > AOrigin.Y)
return Math.PI * 0.5f;
else return Math.PI * 1.5f;
else if (APoint.Y == AOrigin.Y)
if (APoint.X > AOrigin.X)
return 0;
else return Math.PI;
else
{
double Result = Math.Atan((double)(AOrigin.Y - APoint.Y) /
(AOrigin.X - APoint.X));
if ((APoint.X < AOrigin.X) && (APoint.Y > AOrigin.Y))
return Result + Math.PI;
else if ((APoint.X < AOrigin.X) && (APoint.Y < AOrigin.Y))
return Result + Math.PI;
else if ((APoint.X > AOrigin.X) && (APoint.Y < AOrigin.Y))
return Result + 2.0f * Math.PI;
else return Result;
}
} /* PointToAngle */
private void Form1_MouseDown(object sender, MouseEventArgs e)
{
downPoint = e.Location;
}
private void Form1_MouseMove(object sender, MouseEventArgs e)
{
if (downPoint != Point.Empty)
{
Graphics vGraphics = CreateGraphics();
if (movePoint != Point.Empty)
{
vGraphics.DrawLine(new Pen(BackColor), offsetPointA,
new Point(movePoint.X + offsetPointA.X - downPoint.X,
movePoint.Y + offsetPointA.Y - downPoint.Y));
vGraphics.DrawLine(new Pen(BackColor), offsetPointB,
new Point(movePoint.X + offsetPointB.X - downPoint.X,
movePoint.Y + offsetPointB.Y - downPoint.Y));
vGraphics.DrawLine(new Pen(BackColor), downPoint, movePoint);
}
movePoint = e.Location;
double angle = PointToAngle(downPoint, movePoint);
offsetPointA.X = (int)(Math.Cos(angle + 0.5f * Math.PI) * lineSpace +
downPoint.X);
offsetPointA.Y = (int)(Math.Sin(angle + 0.5f * Math.PI) * lineSpace +
downPoint.Y);
offsetPointB.X = (int)(Math.Cos(angle - 0.5f * Math.PI) * lineSpace +
downPoint.X);
offsetPointB.Y = (int)(Math.Sin(angle - 0.5f * Math.PI) * lineSpace +
downPoint.Y);
vGraphics.DrawLine(Pens.Red, offsetPointA,
new Point(movePoint.X + offsetPointA.X - downPoint.X,
movePoint.Y + offsetPointA.Y - downPoint.Y));
vGraphics.DrawLine(Pens.Red, offsetPointB,
new Point(movePoint.X + offsetPointB.X - downPoint.X,
movePoint.Y + offsetPointB.Y - downPoint.Y));
vGraphics.DrawLine(Pens.Blue, downPoint, movePoint);
vGraphics.Dispose();
}
}
private void Form1_MouseUp(object sender, MouseEventArgs e)
{
downPoint = Point.Empty;
}