WPF でアニメーションさせるには Storyboard を使う方法があるが、あらかじめ用意された動作しか表現できない。もっと自由にアニメーションさせるには CompositionTarget.Rendering を使う。
サンプルとして時計を作ってみた。
まず、Visual Studio で WPF プロジェクトを作り、MainWindow.xaml の Grid を Canvas に変更しておく。
次に、MainWindow.xaml.cs を以下のように編集する。
ちなみに、秒針を描画している箇所に smooth とコメントがある行の代わりに、その上の行を有効にすると秒針がチクタクという動きになる。
以上。
サンプルとして時計を作ってみた。
まず、Visual Studio で WPF プロジェクトを作り、MainWindow.xaml の Grid を Canvas に変更しておく。
次に、MainWindow.xaml.cs を以下のように編集する。
using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.Threading.Tasks; using System.Windows; using System.Windows.Controls; using System.Windows.Data; using System.Windows.Documents; using System.Windows.Input; using System.Windows.Media; using System.Windows.Media.Imaging; using System.Windows.Navigation; using System.Windows.Shapes; namespace WpfAnime1 { /// <summary> /// MainWindow.xaml の相互作用ロジック /// </summary> public partial class MainWindow : Window { public MainWindow() { InitializeComponent(); Canvas root = this.Content as Canvas; var outline = new Ellipse() { Name = "outline", Width = 200, Height = 200, Fill = Brushes.Black, }; root.Children.Add(outline); var hourHand = new Rectangle() { Name = "hourHand", Width = 3, Height = 60, Fill = Brushes.Red }; root.Children.Add(hourHand); var minuteHand = new Rectangle() { Name = "minuteHand", Width = 3, Height = 80, Fill = Brushes.LightBlue }; root.Children.Add(minuteHand); var secondHand = new Rectangle() { Name = "secondHand", Width = 2, Height = 90, Fill = Brushes.Green }; root.Children.Add(secondHand); CompositionTarget.Rendering += CompositionTarget_Rendering; } private void CompositionTarget_Rendering(object sender, EventArgs e) { var root = this.Content as Canvas; var center = new Point(this.ActualWidth / 2.0, this.ActualHeight / 2.0); var now = DateTime.Now; foreach (FrameworkElement child in root.Children) { if (child.Name == "outline") { Canvas.SetLeft(child, center.X - child.Width / 2); Canvas.SetTop(child, center.Y - child.Height / 2); } else if (child.Name == "hourHand") { Canvas.SetLeft(child, center.X); Canvas.SetTop(child, center.Y); float a = ((float)((now.Hour % 12) * 60 * 60 + now.Minute * 60 + now.Second) / (12 * 60 * 60)) * 360.0f + 180.0f; child.RenderTransform = new RotateTransform { Angle = a, CenterX = 1.5, CenterY = 0 }; } else if (child.Name == "minuteHand") { Canvas.SetLeft(child, center.X); Canvas.SetTop(child, center.Y); float a = ((float)(now.Minute * 60 + now.Second) / (60 * 60)) * 360.0f + 180.0f; child.RenderTransform = new RotateTransform { Angle = a, CenterX = 1.5, CenterY = 0 }; } else if (child.Name == "secondHand") { Canvas.SetLeft(child, center.X); Canvas.SetTop(child, center.Y); //float a = ((float)(now.Second) / (60)) * 360.0f + 180.0f; float a = ((float)(now.Second * 1000 + now.Millisecond) / (60 * 1000)) * 360.0f + 180.0f; // smooth child.RenderTransform = new RotateTransform { Angle = a, CenterX = 1.0, CenterY = 0 }; } } } } }
ちなみに、秒針を描画している箇所に smooth とコメントがある行の代わりに、その上の行を有効にすると秒針がチクタクという動きになる。
以上。
chaussure christian louboutin http://www.clairedargaud.com/escarpin-louboutin-pas-cher.html