我目前正在使用c#PictureBox,并且已经在不规则形状和水平线的图片框中进行了免提绘制。如何通过鼠标移动来移动选定的不规则形状。
public class IrregularShape
{
public Color ShapeColor { get; set; }
public Point Start { get; set; }
public Point End { get; set; }
public List<Point> points = new List<Point>();
}
在鼠标中,我具有选定的形状,包括开始,结束以及该形状的点列表。
答案 0 :(得分:0)
很高兴终于找到了这个。 在鼠标下方写下此代码:
isdraggingshape = true;
if (selectedshape != null)
{
OffsetX = selectedshape.Start.X - e.Location.X;
OffsetY = selectedshape.Start.Y - e.Location.Y;
}
在鼠标移动中:
if (isdraggingshape == true)
{
if (OffsetX != null && OffsetY != null)
{
if (e.Button == MouseButtons.Left)
{ int new_x1 = e.X + OffsetX;
int new_y1 = e.Y + OffsetY;
int dx = new_x1 - selectedshape.Start.X;
int dy = new_y1 - selectedshape.Start.Y;
if (dx == 0 && dy == 0) return;
// Move the shape
for (int i = 0; i < selectedshape.points.Count; i++)
{
selectedshape.points[i] = new Point(selectedshape.points[i].X + dx,selectedshape.points[i].Y + dy);
}
// Redraw.
ImagepictureBox.Invalidate();
}