나는 그려진 선을 마우스로 움켜 잡으려고하고있다.마우스로 그려진 선을 옮기기
라인은 이미 Graphics.DrawLine(Pen P, Point A, Point B)
으로 그려져 있습니다.
선 만들기 및 양식 그리기에 전혀 문제가 없습니다.
이 나는 시도했다 :
는
그래서GraphicsPath
에 선을 추가 -이도 선OnPaint
을 그릴하지 않습니다. 내가 선을 잡고 어딘가를 드래그합니다 :MouseEventArgs e.Location
가 (지금의 멀리 던져 계산) 몇 가지 기본적인 대수와 라인
그것을 요약하는에있는 경우
확인 e.Location이 라인에 있는지도 확인할 수 없습니다. 어떻게해야합니까?
편집 : 이것은 GraphicsPath를 사용할 때 코드가 어떻게 표시되는지입니다. 나는 GraphicsPath를 사용하지 않는 경우
나는이 다음 drawingShapes 방법
if (s.thisShape == ShapeType.Line) {
g.DrawLine(pen, s.p1, s.p2);
} else { ... }`
. drawStuff에서
: UserControl을 클래스 : 도형에서
private void drawStuff_MouseDown(object sender, MouseEventArgs e)
{
pointRegion = e.Location;
for (int i = 0; i < Shapes.Count; i++)
{
if (Shapes[i].Region.IsVisible(pointRegion))
{
isDragging = true;
count = i;
break;
}
}
}
private void drawStuff_MouseMove(object sender, MouseEventArgs e)
{
if (isDragging)
{
Shapes[count].moveWithDiff(pointRegion, e.Location);
pointRegion = e.Location;
Refresh();
}
}
private void drawStuff_MouseUp(object sender, MouseEventArgs e)
{
isDragging = false;
Refresh();
}
protected override void OnPaint(PaintEventArgs e)
{
base.OnPaint(e);
drawShapes(e.Graphics);
}
private void drawShapes(Graphics g)
{
temporaryPen = pennaLeft;
foreach (Shape s in Shapes)
{
g.FillRegion(temporaryPen, s.Region);
}
}
: UserControl을 클래스 :
public void moveWithDiff(Point pr, Point mp)
{
Point p = new Point();
if (this.thisShape == ShapeType.Line)
{
p.X = mp.X - pr.X;
p.Y = mp.Y - pr.Y;
this.p1.X += p.X;
this.p1.Y += p.Y;
this.p2.X += p.X;
this.p2.Y += p.Y;
}
RefreshPath();
}
private void RefreshPath()
{
gPath = new GraphicsPath();
switch (thisShape)
{
case ShapeType.Line:
gPath.AddLine(this.p1, this.p2);
break;
}
this.Region = new Region(gPath);
}
지금이조차 말했다 그러나 함께 선을 그어야하지 않는 경우 drawingShapes의 문 () 그것은 완벽하게 그으나 다른 곳으로 끌 수는 없습니다.
@HansPassant 나는 내가 희망하는 것을 충분히 편집하여 질문을 편집했다. 이제 직사각형 및 타원과 같은 다른 도형과 완벽하게 작동합니다. 단선 및 타원과 같이 선을 이동하지 않습니다. – NJS