C# 对象旋转后,对象比例不为';t工作正常:UWP
我有个问题,我想不出怎么解决。我有一个用户控件缩放和旋转。 缩放效果很好,但是当我旋转对象时,缩放效果不好。 这是XAMLC# 对象旋转后,对象比例不为';t工作正常:UWP,c#,.net,.net-core,uwp,uwp-xaml,C#,.net,.net Core,Uwp,Uwp Xaml,我有个问题,我想不出怎么解决。我有一个用户控件缩放和旋转。 缩放效果很好,但是当我旋转对象时,缩放效果不好。 这是XAML <Border Name="rectangle1" Background="Blue" Width="200" Height="200" RenderTransformOrigin="0.5,0.5"> <Border.RenderTransform> <RotateTran
<Border Name="rectangle1" Background="Blue"
Width="200" Height="200" RenderTransformOrigin="0.5,0.5">
<Border.RenderTransform>
<RotateTransform x:Name="image_rotate"/>
</Border.RenderTransform>
</Border>
</Canvas>
这是C#
public主页()
{
初始化组件();
}
私有枚举类型
{
无,主体,UL,UR,LR,LL,L,R,T,B
};
//如果正在进行拖动,则为True。
private bool DragInProgress=false;
//阻力的最后一点。
私人点;
//鼠标下的矩形部分。
HitType MouseHitType=HitType.None;
私有bool isRotate=false;
//返回一个HitType值以指示该点的内容。
私有HitType SetHitType(边框矩形,点-点)
{
double left=Canvas.GetLeft(rect);
double-top=Canvas.GetTop(rect);
双右=左+矩形宽度;
双底=顶部+垂直高度;
如果(点X<左)返回HitType.None;
如果(point.X>right)返回HitType.None;
如果(点Y<顶部)返回HitType.None;
如果(点Y>底部)返回HitType.None;
常数双间隙=10;
如果(X点-左侧<间隙)
{
//左边缘。
如果(点Y-顶部<间隙)返回HitType.UL;
if(bottom-point.Ypublic MainPage()
{
InitializeComponent();
}
private enum HitType
{
None, Body, UL, UR, LR, LL, L, R, T, B
};
// True if a drag is in progress.
private bool DragInProgress = false;
// The drag's last point.
private Point LastPoint;
// The part of the rectangle under the mouse.
HitType MouseHitType = HitType.None;
private bool isRotate = false;
// Return a HitType value to indicate what is at the point.
private HitType SetHitType(Border rect, Point point)
{
double left = Canvas.GetLeft(rect);
double top = Canvas.GetTop(rect);
double right = left + rect.Width;
double bottom = top + rect.Height;
if (point.X < left) return HitType.None;
if (point.X > right) return HitType.None;
if (point.Y < top) return HitType.None;
if (point.Y > bottom) return HitType.None;
const double GAP = 10;
if (point.X - left < GAP)
{
// Left edge.
if (point.Y - top < GAP) return HitType.UL;
if (bottom - point.Y < GAP) return HitType.LL;
return HitType.L;
}
if (right - point.X < GAP)
{
// Right edge.
if (point.Y - top < GAP) return HitType.UR;
if (bottom - point.Y < GAP) return HitType.LR;
return HitType.R;
}
if (point.Y - top < GAP) return HitType.T;
if (bottom - point.Y < GAP) return HitType.B;
return HitType.Body;
}
// Set a mouse cursor appropriate for the current hit type.
private void SetMouseCursor()
{
// See what cursor we should display.
Window.Current.CoreWindow.PointerCursor = new Windows.UI.Core.CoreCursor(Windows.UI.Core.CoreCursorType.Arrow, 0);
switch (MouseHitType)
{
case HitType.None:
Window.Current.CoreWindow.PointerCursor = new Windows.UI.Core.CoreCursor(Windows.UI.Core.CoreCursorType.Arrow, 0);
break;
case HitType.Body:
Window.Current.CoreWindow.PointerCursor = new Windows.UI.Core.CoreCursor(Windows.UI.Core.CoreCursorType.Hand, 0);
break;
case HitType.UL:
case HitType.LR:
Window.Current.CoreWindow.PointerCursor = new Windows.UI.Core.CoreCursor(Windows.UI.Core.CoreCursorType.SizeNorthwestSoutheast, 0);
break;
case HitType.LL:
case HitType.UR:
Window.Current.CoreWindow.PointerCursor = new Windows.UI.Core.CoreCursor(Windows.UI.Core.CoreCursorType.SizeNortheastSouthwest, 0);
break;
case HitType.T:
case HitType.B:
Window.Current.CoreWindow.PointerCursor = new Windows.UI.Core.CoreCursor(Windows.UI.Core.CoreCursorType.SizeNorthSouth, 0);
break;
case HitType.L:
case HitType.R:
Window.Current.CoreWindow.PointerCursor = new Windows.UI.Core.CoreCursor(Windows.UI.Core.CoreCursorType.SizeWestEast, 0);
break;
}
}
// Start dragging.
private void canvas1_MouseDown(object sender, PointerRoutedEventArgs e)
{
isRotate = false;
var position = e.GetCurrentPoint(canvas1);
MouseHitType = SetHitType(rectangle1, position.Position);
SetMouseCursor();
DragInProgress = true;
if (MouseHitType == HitType.None)
{
//var Pos = e.GetCurrentPoint(canvas1);
//double dx = Pos.Position.X - ImageCenter.X;
//double dy = Pos.Position.Y - ImageCenter.Y;
isRotate = true;
//StartAngle = (float)Math.Atan2(dy, dx);
//return;
}
var lastPos = e.GetCurrentPoint(canvas1);
LastPoint = lastPos.Position;
}
// If a drag is in progress, continue the drag.
// Otherwise display the correct cursor.
private void canvas1_MouseMove(object sender, PointerRoutedEventArgs e)
{
var position = e.GetCurrentPoint(canvas1);
if (!DragInProgress)
{
MouseHitType = SetHitType(rectangle1, position.Position);
SetMouseCursor();
}
else
{
// See how much the mouse has moved.
Point point = position.Position;
double offset_x = point.X - LastPoint.X;
double offset_y = point.Y - LastPoint.Y;
// Get the rectangle's current position.
double new_x = Canvas.GetLeft(rectangle1);
double new_y = Canvas.GetTop(rectangle1);
double new_width = rectangle1.Width;
double new_height = rectangle1.Height;
// Update the rectangle.
switch (MouseHitType)
{
case HitType.Body:
new_x += offset_x;
new_y += offset_y;
break;
case HitType.UL:
new_x += offset_x;
new_y += offset_y;
new_width -= offset_x;
new_height -= offset_y;
break;
case HitType.UR:
new_y += offset_y;
new_width += offset_x;
new_height -= offset_y;
break;
case HitType.LR:
new_width += offset_x;
new_height += offset_y;
break;
case HitType.LL:
new_x += offset_x;
new_width -= offset_x;
new_height += offset_y;
break;
case HitType.L:
new_x += offset_x;
new_width -= offset_x;
break;
case HitType.R:
new_width += offset_x;
break;
case HitType.B:
new_height += offset_y;
break;
case HitType.T:
new_y += offset_y;
new_height -= offset_y;
break;
}
// Don't use negative width or height.
if ((new_width > 0) && (new_height > 0))
{
// Update the rectangle.
Canvas.SetLeft(rectangle1, new_x);
Canvas.SetTop(rectangle1, new_y);
rectangle1.Width = new_width;
rectangle1.Height = new_height;
// Save the mouse's new location.
LastPoint = point;
}
}
}
// Stop dragging.
private void canvas1_MouseUp(object sender, PointerRoutedEventArgs e)
{
isRotate = false;
DragInProgress = false;
}
private void canvas1_ManipulationDelta(object sender, ManipulationDeltaRoutedEventArgs e)
{
if (isRotate)
{
image_rotate.Angle += e.Delta.Translation.X;
}
}
}