silverlight:Silverlight 制作简单时钟

  以前看到些flash制作很酷炫时钟自己没有艺术细胞UI设计就免了稍微介绍下实现背景换成比较炫图片就可以成为比较炫时钟了

  实现原理是根据时间计算角度再计算时钟指针终点

  代码比较简单:

  XAML:

<Canvas>
  <Ellipse Stroke="Transparent" Width="120" Height="120">
    <Ellipse.Fill>
      <RadialGradientBrush>
        <GradientStop Color="White" Off="0"></GradientStop>     
        <GradientStop Color="#DBDDE6" Off="0.7"></GradientStop>
        <GradientStop Color="#ACABC4" Off="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 secLen = 50;
const minLen = 43;
const horLen = 35;
DateTime dt;
sec;
min;
hor;
double angle;
public Page
{
  InitializeComponent;
  timer = DispatcherTimer;
  timer.Interval = TimeSpan(0, 0, 1);
  timer.Start;
  timer.Tick 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;
}


  代码比较简单不多解释

  运行效果:

Silverlight 制作简单时钟

Tags:  silverlight2 silverlight.2.0 silverlight是什么 silverlight

延伸阅读

最新评论

发表评论