Silverlight 制作简单时钟
2008-06-09 09:30
387 查看
以前看到一些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;
}
代码比较简单,不多解释。
运行效果:
实现原理是根据时间计算角度,再计算时钟指针的终点。
代码比较简单:
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;
}
代码比较简单,不多解释。
运行效果:
相关文章推荐
- css3--简单制作时钟
- 利用js制作简单的时钟
- Silverlight控件使用(三)制作数字时钟
- WCF+Silverlight 制作一个简单RSS的阅读器(一)
- 使用Silverlight制作简单的小游戏—Jewellery (Part 2)
- 【C】制作简单的时钟减法程序
- 使用Unity制作简单时钟动画
- 使用html与javascript制作简单的网页时钟
- Javascript—简单电子时钟制作总结
- 通过制作一个简单的时钟学习WPF中DispatcherTimer的使用 Level 100
- 使用Silverlight制作简单的小游戏—Jewellery (Part 1)
- 入门1--结合javascript制作一个简单的silverlight1.0例子
- JavaFX制作一个简单的时钟
- 入门1--结合javascript制作一个简单的silverlight1.0例子
- js简单制作一个时钟
- Silverlight 制作的时钟
- 通过制作一个简单的时钟学习WPF中DispatcherTimer的使用 Level 100
- canvas制作的简单时钟特效
- 使用Silverlight制作简单的小游戏—Jewellery (Part 3)
- 使用SVG制作简单的时钟效果