模拟螺旋桨
本文关键字:螺旋桨 模拟 | 更新日期: 2023-09-27 18:37:10
我有这段代码在画布上显示一个矩形:
XAML :
<Window x:Class="rotateRect.MainWindow"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
xmlns:local="clr-namespace:rotateRect"
mc:Ignorable="d"
Title="MainWindow" Height="402.027" Width="600.676">
<Grid>
<Canvas Name="my_c" HorizontalAlignment="Left" Height="24" Margin="160,149,0,0" VerticalAlignment="Top" Width="177" RenderTransformOrigin="0.5,0.5">
<Canvas.RenderTransform>
<TransformGroup>
<ScaleTransform/>
<SkewTransform/>
<RotateTransform Angle="-68.962"/>
<TranslateTransform/>
</TransformGroup>
</Canvas.RenderTransform>
</Canvas>
</Grid>
我可以使用"RenderTransform"在XAML中转换它。但我想在 c# 中实现这种旋转并创建一个"螺旋桨"。我试图为画布查找有关"旋转变换"的信息。Thys 是我的 c# 代码:
C# :
using System.Windows;
using System.Windows.Controls;
using System.Windows.Media;
using System.Windows.Shapes;
namespace rotateRect
{
public partial class MainWindow : Window
{
Rectangle my_r = new Rectangle();
public MainWindow()
{
InitializeComponent();
initializeRect(my_r, my_c);
}
private void initializeRect(Rectangle r, Canvas c)
{
// rectangle
r.Width = 10;
r.Height = 50;
// rectangle Color
r.Fill = new SolidColorBrush(Color.FromRgb(0, 0, 255));
// canvas
c.Children.Add(r);
c.Width = 10;
c.Height = 50;
}
}
}
我找到了一个位图对象的"RotateTransform"示例,但我没有找到画布的任何内容。C#,旋转图形?
是否可以在 c# 中将 RotateTransform 应用于画布?
修改你的 initializeRect 方法e
private void initializeRect(Rectangle r, Canvas c)
{
// rectangle
r.Width = 10;
r.Height = 50;
// rectangle Color
r.Fill = new SolidColorBrush(Color.FromRgb(0, 0, 255));
// canvas
c.Children.Add(r);
c.Width = 10;
c.Height = 50;
RotateTransform rt = new RotateTransform();
rt.Angle=-68.962;
c.RenderTransform = rt;
}
"螺旋桨"是指动画?为此,您需要动画。只有旋转变换是不够的。
例如,让我们复制带有矩形但在 XAML 中的画布:
<Canvas Name="cnvs" VerticalAlignment="Center" HorizontalAlignment="Center"
Loaded="cnvs_Loaded">
<Canvas.Resources>
<DoubleAnimation x:Key="rotator" From="0" To="360" Duration="0:0:1"
AutoReverse="False" RepeatBehavior="Forever" />
</Canvas.Resources>
<Canvas.RenderTransform>
<RotateTransform x:Name="canvasRotation" />
</Canvas.RenderTransform>
<Rectangle Width="10" Height="50" Fill="Blue" />
</Canvas>
并在我们的 Loaded 事件处理程序中启动动画。Canvas 有自己的大小调整怪癖,因此我们需要为转换设置 CenterX 和 CenterY poperties:
private void cnvs_Loaded(object sender, RoutedEventArgs e)
{
var canvas = (Canvas) sender;
var animation = canvas.Resources["rotator"] as DoubleAnimation;
var children = canvas.Children.OfType<FrameworkElement>().ToArray();
canvasRotation.CenterX = children.Max(c => c.ActualWidth) / 2;
canvasRotation.CenterY = children.Max(c => c.ActualHeight) / 2;
canvasRotation.BeginAnimation(RotateTransform.AngleProperty, animation);
}
<小时 />但是你真的需要这里的画布吗?我们可以用网格简化事情,例如:
<Grid Name="grid" VerticalAlignment="Top" HorizontalAlignment="Left"
Loaded="grid_Loaded" RenderTransformOrigin="0.5,0.5">
<Grid.Resources>
<DoubleAnimation x:Key="rotator" From="0" To="360" Duration="0:0:1"
AutoReverse="False" RepeatBehavior="Forever" />
</Grid.Resources>
<Grid.RenderTransform>
<RotateTransform x:Name="gridRotation" />
</Grid.RenderTransform>
<Rectangle Width="10" Height="50" Fill="Blue" />
</Grid>
和
private void grid_Loaded(object sender, RoutedEventArgs e)
{
var g = (Grid) sender;
var animation = g.Resources["rotator"] as DoubleAnimation;
gridRotation.BeginAnimation(RotateTransform.AngleProperty, animation);
}
或者,如果 Recntagle 是容器中唯一的对象,则仅对其进行动画处理。无需使事情复杂化。