元素旋转后不希望正确调整大小 [英] Element does not want to re size correctly after it has been rotated
问题描述
大家好.
我有一个小问题.
我目前正在使用一个编辑器,该编辑器可以将图像和文本添加到画布上,并且效果很好.我还能够调整这些元素的大小并旋转它们.
现在,当我在画布上旋转某个元素(例如您的图像)后,它无法正确调整大小,实际上它只是从画布上消失了,所以问题就来了.
我使用了Adorner类来启用元素选择和调整大小.
这是我的代码片段:
//从右下角调整元素的大小
Hi all.
I have a small problem.
I am currently working on an editor that deals with adding images and text onto a canvas which works just fine. I am also able to re size these elements as well as rotate them.
Now my problem comes when after I rotate an element (such as your image) on my canvas it does not re size correctly, in fact it just disappears off my canvas.
I used the Adorner Class to enable element selection and resizing.
Here are snippets of my code :
//Resize element from the bottom Right corner
private void bottomRight_DragDelta(object sender , DragDeltaEventArgs e)
{
FrameworkElement adornedElement = this.AdornedElement as FrameworkElement;
Thumb hitThumb = sender as Thumb;
if (adornedElement == null || hitThumb == null) return;
FrameworkElement parentElement = adornedElement.Parent as FrameworkElement;
EnforeSize(adornedElement);
adornedElement.Width = Math.Max(adornedElement.Width + e.HorizontalChange, hitThumb.DesiredSize.Width);
adornedElement.Height = Math.Max(e.VerticalChange + adornedElement.Height, hitThumb.DesiredSize.Height);
}
//强制大小方法
//Enforce Size Method
private void EnforeSize(FrameworkElement adornedElement)
{
if (adornedElement.Width.Equals(Double.NaN))
adornedElement.Width = adornedElement.DesiredSize.Width;
if (adornedElement.Height.Equals(Double.NaN))
adornedElement.Height = adornedElement.DesiredSize.Height;
FrameworkElement parent = adornedElement.Parent as FrameworkElement;
if (parent != null)
{
adornedElement.MaxHeight = parent.ActualHeight;
adornedElement.MaxWidth = parent.ActualWidth;
}
}
//向右旋转元素
// Rotate element to the right
private void ROTATE_CLOCKWISE(UIElement element)
{
if (element != canvas2 && element != canvas3)
{
RotateTransform rotateTransform = new RotateTransform();
current_angle += 90;//int variable declared globally
rotateTransform.Angle = current_angle;
TransformGroup transGroup = new TransformGroup();
transGroup.Children.Add(rotateTransform);
element.RenderTransform = transGroup;
}
}
预先谢谢您.
Thank you in advance
推荐答案
这似乎是常见的错误:该元素只是超出了其容器的范围(画布?窗口?).原因如下:您使用RotateTransform
并仅设置角度,而不是默认为(0,0)的旋转中心.现在,请阅读:
This looks like the usual mistake: the element simply goes outside the its container (canvas? window?). Here is why: you useRotateTransform
and set only the angle, not center of rotation which defaults to (0, 0). Now, read it:
备注
使用RotateTransform
时,应意识到变换使特定对象的坐标系绕点(0,0)旋转.因此,根据对象的位置,它可能无法在原地(围绕其中心)旋转.例如,如果将对象沿x轴从0开始放置200个单位,则旋转30度可使对象沿围绕原点绘制的半径为200的圆摆动30度.要将对象旋转到位,请将RotateTransform
的CenterX
和CenterY
设置为要旋转的对象的中心.
Remarks
When you use aRotateTransform
, realize that the transformation rotates the coordinate system for a particular object about the point (0, 0). Therefore, depending on the position of the object, it might not rotate in place (around its center). For example, if an object is positioned 200 units from 0 along the x-axis, a rotation of 30 degrees can swing the object 30 degrees along a circle that has a radius of 200, which is drawn around the origin. To rotate an object in place, set theCenterX
andCenterY
of theRotateTransform
to the center of the object to rotate.
请参见 ^ ].
现在清楚了吗?旋转后检查元件的位置;进行移动或相应地更改中心或旋转.
See http://translate.google.com/#fr|en|[^].
Is it clear now? Check up the position of the element after rotation; shift it or change center or rotation accordingly.
这篇关于元素旋转后不希望正确调整大小的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!