【发布时间】:2011-12-09 16:08:12
【问题描述】:
我正在尝试通过用鼠标抓住一条画线来移动它。
这条线已经用Graphics.DrawLine(Pen P, Point A, Point B)画好了。
创建线条并将其绘制在表单上绝对没有问题。
我试过了:
将线添加到
GraphicsPath- 这甚至不会绘制线OnPaint。检查
MouseEventArgs e.Location是否符合一些基本代数(我现在已经丢弃的计算)
所以总结一下:我想抓住这条线并将它拖到某个地方,但我什至无法检查 e.Location 是否在线,我该怎么做?
编辑:这是我使用 GraphicsPath 时代码的外观。
当我不使用我拥有的 GraphicsPath 时:
if (s.thisShape == ShapeType.Line) {
g.DrawLine(pen, s.p1, s.p2);
} else { ... }`
在 drawingShapes 方法中。
来自drawStuff:用户控件类:
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);
}
}
来自 Shape : 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() 中说 if 语句它画得很完美,但我不能把它拖到其他地方。
【问题讨论】:
-
@HansPassant 我编辑了这个问题,我希望这是足够的。现在这与矩形和椭圆等其他形状完美配合,它不会移动线条,更不用说绘制它们了。
标签: c# winforms drag-and-drop line