c# - 在c#中移动不规则形状
问题描述
我目前正在使用 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>();
}
我在鼠标中选择了形状,开始,结束和该形状的点列表。
解决方案
很高兴终于找到了这个。在鼠标向下写下这段代码:
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();
}
推荐阅读
- reactjs - 将 DateRangePicker 选择的日期更改为字符串
- salesforce - Salesforce:带有包含的 SOSL
- python - python matplotlib,第二个 y 轴与第一个
- java - 对具有不同属性的对象数组进行排序
- python - Sqlite和python之间不返回值
- android - RecyclerView - 从 ViewHolder 获取位置
- haskell - Prolog implementation of Quine's algorithm for classical propositional logic (in Quine's "Methods of Logic")
- linux - Can't write data to applesmc error, after upgrade to Arch linux kernel 5.8.1
- sql-server - SQL Server:输出选择表?
- python - 通过 Google 广告 API 获取出价模拟器结果