2016-03-02 88 views
0

我有这样的代码,在画布上显示一个矩形:模拟一个螺旋桨

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#中实现这种旋转并创建一个“螺旋桨”。我试图为画布找到关于“RotateTransform”的信息。蒂斯是我的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#, rotating Graphics?

可以将RotateTransform应用于c#中的画布吗?

回答

0

修改initializeRect梅索德

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; 
     } 
+0

非常感谢!它工作得很好。 – Jguillot

0

通过 '推进器' 你的意思动画?为此,你需要动画。只有RotateTransform是不够的。

例如,让我们来复制你的画布与矩形,但在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事件处理动画。帆布有它自己的大小怪癖,所以我们需要设置的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如果它是你必须在容器中的唯一对象。没有必要使事情复杂化。

+0

嗨,谢谢你的回答。 为了模拟“螺旋桨”,我创建了一个像这样的“计时器”:http://www.wpf-tutorial.com/misc/dispatchertimer/ 它启动我的功能“螺旋桨”每个勾号。这模拟了一个像螺旋桨一样旋转的矩形。我没有添加到我的代码,因为我不想收取它。 我可以在上面编辑我的代码,告诉你我是如何实现它的。 – Jguillot

+0

@Jguillot,不需要,在代码背后创建控件并用定时器对它们进行动画处理也是WinForms-ey,而不是WPF方法。但是,你可以自由地以任何你喜欢的方式实现它,当然。 – icebat