问题描述
我有一个程序,可以在 Canvas $ c $中拖动,旋转和调整
System.Windows.Shapes.Ellipse
的大小c>面板。
I have a program that I can drag, rotate and resize a System.Windows.Shapes.Ellipse
in a Canvas
panel.
要在画布内调整椭圆的大小并拖动它并使其始终居中,我需要每次校正时都要校正,因为椭圆的起点是
To resize and drag the ellipse inside the canvas and always keep it center I need to correct every time its origin, because the ellipse has it origin in the top left corner.
有没有办法默认使 Ellipse
的原点居中?
Have a way to make the origin in the Ellipse
on center by default?
拖动:
Canvas.SetTop(ellipse, newX - (ellipse.Height / 2));
Canvas.SetLeft(ellipse, newY - (ellipse.Width / 2));
调整大小:
ellipse.Height = newHeight;
ellipse.Width = newWidth;
旋转:
ellipse.RenderTransform = new RotateTransform(angle,(ellipse.Width/2),(ellipse.Height/2));
推荐答案
如果宽度和高度固定,则最简单的解决方案将使用 X
RenderTransform 设置为 TranslateTransform
>和 Y
设置为负偏移量,分别等于椭圆的宽度和高度的一半:
If the width and height are fixed, the simplest solution would be to set the Ellipse's RenderTransform
to a TranslateTransform
with X
and Y
set to negative offsets equal to half the ellipse's width and height, respectively:
<Ellipse Width="100" Height="100" Fill="Red">
<Ellipse.RenderTransform>
<TranslateTransform X="-50" Y="-50" />
</Ellipse.RenderTransform>
</Ellipse>
请注意,使用 RenderTransform
的警告是该转换不会应用于布局(并且 LayoutTransform
您不能使用 TranslateTransform
)。对于 Canvas
来说,这不应该是一个问题,因为它处理布局的方式虽然对其他面板也可能有问题。
Note that a caveat of using RenderTransform
is that the transform is not applied to layout (and you cannot use a TranslateTransform
for the LayoutTransform
). That shouldn't be an issue with a Canvas
because of how it handles layout, though it might be problematic with other panels.
这篇关于将椭圆的原点放在WPF的中央的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!