개발/Application Develop

WPF Storyboard Animation_마우스 진입했을 때 애니메이션 설정

huiyu 2014. 1. 26. 00:00


//스택패널 생성 후 여백 20 설정 StackPanel myStackPanel = new StackPanel(); myStackPanel.Margin = new Thickness(20); //애니메이션을 설정할 사각형 설정 Rectangle myRectangle = new Rectangle(); myRectangle.Name = "MyRectangle"; myRectangle.Width = 100; myRectangle.Height = 100; // 새로운 NameScope생성 후 등록 NameScope.SetNameScope(this, new NameScope()); this.RegisterName(myRectangle.Name, myRectangle); //파랑색 설정 SolidColorBrush mySolidColorBrush = new SolidColorBrush(Colors.Blue); this.RegisterName("MySolidColorBrush", mySolidColorBrush); myRectangle.Fill = mySolidColorBrush; //가로로 커지는 애니메이션 설정 DoubleAnimation myDoubleAnimation = new DoubleAnimation(); myDoubleAnimation.From = 100; myDoubleAnimation.To = 200; myDoubleAnimation.Duration = new Duration(TimeSpan.FromSeconds(1)); Storyboard.SetTargetName(myDoubleAnimation, myRectangle.Name); Storyboard.SetTargetProperty(myDoubleAnimation, new PropertyPath(Rectangle.WidthProperty)); //칼라애니메이션 설정 ColorAnimation myColorAnimation = new ColorAnimation(); myColorAnimation.From = Colors.Blue; myColorAnimation.To = Colors.Red; myColorAnimation.Duration = new Duration(TimeSpan.FromSeconds(1)); Storyboard.SetTargetName(myColorAnimation, "MySolidColorBrush"); Storyboard.SetTargetProperty(myColorAnimation, new PropertyPath(SolidColorBrush.ColorProperty)); //스토리보드에 두 애니메이션 등록 Storyboard myStoryboard = new Storyboard(); myStoryboard.Children.Add(myDoubleAnimation); myStoryboard.Children.Add(myColorAnimation); //마우스가 진입했을 때 애니메이션 설정 myRectangle.MouseEnter += delegate(object sender, MouseEventArgs e) { myStoryboard.Begin(this); }; //스택패널에 사각형 객체 등록 myStackPanel.Children.Add(myRectangle); this.Content = myStackPanel;


728x90
반응형