Skip to content

Commit

Permalink
Merge pull request #28 from lifuhua123/master
Browse files Browse the repository at this point in the history
update in ElementAdorner
  • Loading branch information
yanjinhuagood authored Mar 8, 2023
2 parents cc34388 + 33adcf1 commit 4401d1e
Showing 1 changed file with 45 additions and 2 deletions.
47 changes: 45 additions & 2 deletions src/WPFDevelopers.Shared/Controls/ElementAdorner/ElementAdorner.cs
Original file line number Diff line number Diff line change
Expand Up @@ -67,8 +67,24 @@ private Thumb CreateMoveThumb()
var element = AdornedElement as FrameworkElement;
if (element == null)
return;
Canvas.SetLeft(element, Canvas.GetLeft(element) + e.HorizontalChange);
Canvas.SetTop(element, Canvas.GetTop(element) + e.VerticalChange);

var transform = element.RenderTransform;
if (transform != null)
{
if (transform as RotateTransform != null)
{
var rotateTransform = transform as RotateTransform;

Point ptChange = rotateTransform.Transform(new Point(e.HorizontalChange, e.VerticalChange));

SetPositon(element, Canvas.GetLeft(element) + ptChange.X, Canvas.GetTop(element) + ptChange.Y);
}
else
{
SetPositon(element, Canvas.GetLeft(element) + e.HorizontalChange, Canvas.GetTop(element) + e.VerticalChange);
}
}

};
return thumb;
}
Expand Down Expand Up @@ -160,5 +176,32 @@ protected override Visual GetVisualChild(int index)
{
return visualCollection[index];
}

private void SetPositon(FrameworkElement element,double left, double top)
{
var parent = VisualTreeHelper.GetParent(element) as Canvas;

if (left <= 0)
{
left = 0;
}

if (top <= 0)
{
top = 0;
}

if (left + element.Width > parent.ActualWidth)
{
left = parent.ActualWidth - element.Width;
}
if (top + element.Height > parent.ActualHeight)
{
top = parent.ActualHeight - element.Height;
}

Canvas.SetLeft(element, left);
Canvas.SetTop(element, top);
}
}
}

0 comments on commit 4401d1e

Please sign in to comment.