Tick イベントの処理で、秒針、短針、長針の位置を計算して、Lineを再描画しています。
ウィンドウのタイトルにも時刻を表示します。
MainWindow.xaml
<Window x:Class="AnalogClock.MainWindow"xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
Height="280" Width="260">
<Canvas>
<Canvas.Resources>
<Style TargetType="Line">
<Setter Property="StrokeThickness" Value="0.1" />
<Setter Property="Stroke" Value="Black" />
</Style>
</Canvas.Resources>
<Canvas.RenderTransform>
<MatrixTransform>
<MatrixTransform.Matrix>
<Matrix OffsetX="120" OffsetY="120" M11="10" M12="0" M21="0" M22="-10"/>
</MatrixTransform.Matrix>
</MatrixTransform>
</Canvas.RenderTransform>
<Line X1="0" Y1="0" X2="0" Y2="0" Name="secondLine" StrokeThickness=".05" />
<Line X1="0" Y1="0" X2="0" Y2="0" Name="minuteLine" />
<Line X1="0" Y1="0" X2="0" Y2="0" Name="hourLine" StrokeThickness=".2" />
<Line X1="0" Y1="10" X2="0" Y2="11" />
<Line X1="10" Y1="0" X2="11" Y2="0" />
<Line X1="0" Y1="-10" X2="0" Y2="-11" />
<Line X1="-10" Y1="0" X2="-11" Y2="0" />
</Canvas>
</Window>
MainWindow.xaml.cs
using System;using System.Windows;
using System.Windows.Threading;
namespace AnalogClock
{
public partial class MainWindow : Window
{
public MainWindow()
{
InitializeComponent();
DispatcherTimer timer = new DispatcherTimer();
timer.Tick += timer_Tick;
timer.Interval = new TimeSpan(0, 0, 1);
timer.Start();
}
void timer_Tick(object sender, EventArgs e)
{
DateTime now = DateTime.Now;
this.Title = now.ToString("HH:mm:ss");
double secondRadian = 2 * Math.PI / 60 * now.Second;
this.secondLine.X2 = Math.Sin(secondRadian) * 10;
this.secondLine.Y2 = Math.Cos(secondRadian) * 10;
double minuteRadian = 2 * Math.PI / 60 * now.Minute;
this.minuteLine.X2 = Math.Sin(minuteRadian) * 10;
this.minuteLine.Y2 = Math.Cos(minuteRadian) * 10;
double hourRadian = 2 * Math.PI / 720 * ((now.Hour % 12) * 60 + now.Minute);
this.hourLine.X2 = Math.Sin(hourRadian) * 7;
this.hourLine.Y2 = Math.Cos(hourRadian) * 7;
}
}
}
0 件のコメント:
コメントを投稿