Silverlight 制作简单时钟
2008-12-08 11:51:40 来源:WEB开发网以前看到一些flash制作的很酷炫的时钟,自己没有艺术细胞,UI设计就免了,稍微介绍一下实现,背景换成比较炫的图片就可以成为比较炫的时钟了
实现原理是根据时间计算角度,再计算时钟指针的终点。
代码比较简单:
XAML:
<Canvas>
<Ellipse Stroke="Transparent" Width="120" Height="120">
<Ellipse.Fill>
<RadialGradientBrush>
<GradientStop Color="White" Offset="0"></GradientStop>
<GradientStop Color="#DBDDE6" Offset="0.7"></GradientStop>
<GradientStop Color="#ACABC4" Offset="1"></GradientStop>
</RadialGradientBrush>
</Ellipse.Fill>
</Ellipse>
<Line x:Name="lnHor" Stroke="Red" StrokeThickness="3" X1="60" Y1="60" X2="60" Y2="60"></Line>
<Line x:Name="lnMin" Stroke="Yellow" StrokeThickness="2" X1="60" Y1="60" X2="60" Y2="60"></Line>
<Line x:Name="lnSec" Stroke="Blue" StrokeThickness="2" X1="60" Y1="60" X2="60" Y2="60"></Line>
<Ellipse Stroke="White" Width="6" Height="6" Fill="Black" Canvas.Left="57" Canvas.Top="57">
</Ellipse>
</Canvas>
C#:
DispatcherTimer timer;
const int secLen = 50;
const int minLen = 43;
const int horLen = 35;
DateTime dt;
int sec;
int min;
int hor;
double angle;
public Page()
{
InitializeComponent();
timer = new DispatcherTimer();
timer.Interval = new TimeSpan(0, 0, 1);
timer.Start();
timer.Tick += new EventHandler(timer_Tick);
}
void timer_Tick(object sender, EventArgs e)
{
dt = DateTime.Now;
sec = dt.Second;
min = dt.Minute;
hor = dt.Hour;
angle = Math.PI / 30 * sec - Math.PI / 2;
lnSec.X2 = 60 + Math.Cos(angle) * secLen;
lnSec.Y2 = 60 + Math.Sin(angle) * secLen;
angle = Math.PI / 1800 * (min * 60 + sec) - Math.PI / 2;
lnMin.X2 = 60 + Math.Cos(angle) * minLen;
lnMin.Y2 = 60 + Math.Sin(angle) * minLen;
angle = Math.PI / 21600 * (hor * 3600 + min * 60 + sec) - Math.PI / 2;
lnHor.X2 = 60 + Math.Cos(angle) * horLen;
lnHor.Y2 = 60 + Math.Sin(angle) * horLen;
}
代码比较简单,不多解释。
运行效果:
Tags:Silverlight 制作 简单
编辑录入:爽爽 [复制链接] [打 印]更多精彩
赞助商链接