这是在图像上拖动一些裁剪器的逻辑,而且它是有效的。但是我在不同的窗口上有多个图像(因为不同的文件),我想给它们分配相同的逻辑,但我不想在任何地方复制相同的代码。有办法吗?
private bool isDragging;
private Point clickPosition;
private void OnMouseMove(object sender, MouseEventArgs e)
{
if (isDragging)
{
Point currentPosition = e.GetPosition(this.Parent as UIElement);
double xdiff = currentPosition.X - clickPosition.X;
double ydiff = currentPosition.Y - clickPosition.Y;
croppingAdorner.HandleThumb(1, 1, 0, 0, xdiff, ydiff);
clickPosition = e.GetPosition(this);
}
}
private void OnMouseDown(object sender, MouseButtonEventArgs e)
{
if (CropHelper.IsPointInsideRect(e.GetPosition(this.originalImage), rc))
{
isDragging = true;
clickPosition = e.GetPosition(this);
}
}
private void OnMouseUp(object sender, MouseButtonEventArgs e)
{
isDragging = false;
}
private void OnMouseLeave(object sender, MouseEventArgs e)
{
isDragging = false;
}