2012-06-22 46 views
0

目前我正在開發一個涉及在WPF中爲自定義構建的2D形狀創建動畫的項目。形狀看起來像一個盒子,目前我似乎有一些小故障。創建具有動畫效果的2D wpf自定義形狀

這裏是我的代碼看起來像此刻

namespace Wpftrial4 
{ 
    /// <summary> 
    /// Interaction logic for MainWindow.xaml 
    /// </summary> 
    public partial class MainWindow : Window 
    { 

     SolidColorBrush fillcolor = new SolidColorBrush(); 
     SolidColorBrush bordercolor = new SolidColorBrush(); 


     public MainWindow() 
     { 
      InitializeComponent(); 

      fillcolor.Color = Colors.WhiteSmoke; 
      bordercolor.Color = Colors.Blue; 


     } 

     private void button1_Click(object sender, RoutedEventArgs e) 
     { 

      //Manually creating the required 2D Box shape 
      Path path_rectangle = new Path(); 

      path_rectangle.Fill = fillcolor; 
      path_rectangle.Stroke = bordercolor; 
      path_rectangle.StrokeThickness = 5; 

      RectangleGeometry rg = new RectangleGeometry(); 
      double width_rec = 100; 
      double height_rec = 50; 
      double left_rec = 130; 
      double top_rec = 100; 
      rg.Rect = new Rect(left_rec, top_rec, width_rec, height_rec); 
      path_rectangle.Data = rg; 
      canvas1.Children.Add(path_rectangle); 


      Path path2_top = new Path(); 
      path2_top.Stroke = new SolidColorBrush(Colors.Brown); 
      path2_top.StrokeThickness = 6; 
      RectangleGeometry rg2 = new RectangleGeometry(); 
      double width2 = 100; 
      double height2 = 2; 
      double left2 = 130; 
      double top2 = 90; 
      rg2.Rect = new Rect(left2, top2, width2, height2); 
      path2_top.Data = rg2; 
      canvas1.Children.Add(path2_top); 


      Path path3_topcover = new Path(); 
      path3_topcover.Stroke = new SolidColorBrush(Colors.White); 
      path3_topcover.StrokeThickness = 5; 
      RectangleGeometry rg3 = new RectangleGeometry(); 
      double width3 = 100; 
      double height3 = 5; 
      double left3 = 130; 
      double top3 = 100; 
      rg3.Rect = new Rect(left3, top3, width3, height3); 
      path3_topcover.Data = rg3; 
      canvas1.Children.Add(path3_topcover); 


      //combining part_Group type 
      GeometryGroup myGeometryGroup = new GeometryGroup(); 
      myGeometryGroup.Children.Add(rg); 
      myGeometryGroup.Children.Add(rg3); 
      Path myPath = new Path(); 
      myPath.Stroke = Brushes.Blue; 
      myPath.Data = myGeometryGroup; 
      canvas1.Children.Add(myPath); 





      ////test animate 
      //myCustomAnimatedBox MB2 = new myCustomAnimatedBox(200, 200, 300, 250) { Stroke = Brushes.Black, StrokeThickness = 2 }; 
      //canvas1.Children.Add(MB2); //new myCustomAnimatedBox(200, 200, 300, 250,500,500){Stroke = Brushes.Black, StrokeThickness = 2}); 
      //PointAnimation pa2 = new PointAnimation(); 
      ////DoubleAnimation da = new DoubleAnimation(); 
      //pa2.To = new Point(100, 100); 
      ////da.By = 50; 
      //pa2.Duration = TimeSpan.FromSeconds(10); 
      ////MB2.BeginAnimation(myCustomAnimatedBox.TopProperty,pa2);//(//MB2.BeginAnimation(myCustomAnimatedBox.CenterProperty, pa2); 

     } 

     //class- style custom2D shape creation 
     private void button2_Click(object sender, RoutedEventArgs e) 
     { 
      myCustomAnimatedBox MB = new myCustomAnimatedBox(200, 200, 350, 250) { Stroke = Brushes.Black, StrokeThickness = 2 }; 
      canvas1.Children.Add(MB); //new myCustomAnimatedBox(200, 200, 300, 250,500,500){Stroke = Brushes.Black, StrokeThickness = 2}); 
      //PointAnimation pa = new PointAnimation(); 
      ////DoubleAnimation da = new DoubleAnimation(); 
      //pa.To = new Point(100, 100); 
      ////da.By = 50; 
      //pa.Duration = TimeSpan.FromSeconds(10); 
      //MB.BeginAnimation(myCustomAnimatedBox.CenterProperty, pa); 



     } 
    } 

    //Class that defines the custom 2D shape 
    public class myCustomAnimatedBox : Shape 
    { 
     private double x1, width; 
     private double y1, height; 

     private GeometryGroup myBox = new GeometryGroup(); 


     public myCustomAnimatedBox(double XX1, double YY1, double widTH, double heiGHT) 
     { 
      makeBox(XX1, YY1, widTH, heiGHT); 

     } 



     //public static readonly DependencyProperty CenterProperty = DependencyProperty.Register("Center", typeof(Point), typeof(myCustomAnimatedBox), 
     // new PropertyMetadata(null,PropertyChanged));// FrameworkPropertyMetadata(FrameworkPropertyMetadataOptions.AffectsMeasure)); 
     ////new Point(cent_X, cent_Y) 

     //public Point Center 
     //{ 
     // get { return (Point)GetValue(CenterProperty); } 
     // set 
     // { 

     //  SetValue(CenterProperty, value); 
     //  myBox.Children.Clear(); 
     //  makeBox(myBox, value.X, value.Y, width, height); 
     // } 
     //} 

     //public Point Center 
     //{ 
     // get { return (Point)GetValue(CenterProperty); } 
     // set 
     // { 

     //  SetValue(CenterProperty, value); 
     //  myBox.Children.Clear(); 
     //  makeBox(myBox, value.X, value.Y, width, height); 
     // } 
     //} 
     //public Point Top 
     //{ 
     // get { return new Point(X1, Y1); } 
     // set 
     // { 
     //  //X1 = value.X; 
     //  //Y1 = value.Y; 
     //  Top = value; 

     // } 

     //} 

     //public static readonly DependencyProperty TopProperty = DependencyProperty.Register("Top", typeof(Point), typeof(myCustomAnimatedBox), new FrameworkPropertyMetadata(FrameworkPropertyMetadataOptions.AffectsMeasure)); 
      //new PropertyMetadata(null,PropertyChanged)); 

     public double X1 
     { 
      get { return x1; } 
      set 
      { 
       x1 = value; 

      } 
     } 

     public double Y1 
     { 
      get { return y1; } 
      set 
      { 
       y1 = value; 

      } 
     } 


     //Trial at creating a dependency property 
     //private static void TopPropertyChanged(DependencyObject d, DependencyPropertyChangedEventArgs e) 
     //{ 
     // myCustomAnimatedBox myB = (myCustomAnimatedBox)d; 


     // myB.InvalidateVisual(); 



     //} 

     protected override Geometry DefiningGeometry 
     { 
      get { return myBox; } 
     } 

     //Function that draws the object 
     private void makeBox(double XX1, double YY1, double widTH, double heiGHT) 
     { 

      width = widTH; 
      height = heiGHT; 
      this.X1 = XX1; 
      this.Y1 = YY1; 
      RectangleGeometry rectangle_part = new RectangleGeometry(); 
      rectangle_part.Rect = new Rect(X1,Y1,width,height); 
      RectangleGeometry rg_cover = new RectangleGeometry(); 
      RectangleGeometry rg_top = new RectangleGeometry(); 
      rg_top.Rect = new Rect(X1, Y1 - 10, width, 2); 
      rg_cover.Rect = new Rect(X1, Y1, width, 5); 
      myBox.Children.Add(rectangle_part); 
      myBox.Children.Add(rg_top); 
      myBox.Children.Add(rg_cover); 


     } 


    } 
} 

由於形狀會發生類似的動畫:箱開閉,我需要自定形狀有一個可移動的部分。然而,當需要動畫加其他數據綁定時,根據大多數作者的GeometryGroup似乎提供了最大的優勢。在我測試的代碼中手動創建了形狀。這可以在連接到Button1的代碼中看到。然而,當我嘗試實施班級實施時,我無法像改變個人組成特徵那樣獲得我想要的那種效果。我距離WPF還不到1個月,希望在以下方面提供幫助。

  1. 對於我想達到的目標,GeometryGroup是否理想?
  2. 將遵循此課程導致實現我的目標。即我將能夠動畫的一部分在WPF中構建的自定義2D形狀,即box - open;盒---關閉。
  3. 有人可以告訴我,我需要做什麼才能夠改變組成形狀的個別屬性,即我自定義形狀中的矩形。

感謝您的幫助。

回答

1

最適合做動畫2D形狀選擇使用Expression Blend的軟件:Download link software 和u可以看到一個實例教程這裏:Introduction to Blend: Animation

+0

感謝irosg但我混合起來,當它涉及到建立一個自定義形狀如上所述這是可以動畫的。更多建議? –