Here is designer code xaml:
- <UserControl x:Class="Clock.Page"
- xmlns="http://schemas.microsoft.com/client/2007"
- xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
- Width="400" Height="400">
- <Canvas Width="400" Height="400" Loaded="Canvas_Loaded">
- <Canvas.Resources>
- <Storyboard x:Name="clockStoryboard">
-
- <!-- This animation targets the hour hand transform -->
- <DoubleAnimation x:Name="hourAnimation"
- Storyboard.TargetName="hourHandTransform"
- Storyboard.TargetProperty="Angle"
- Duration="12:0:0" RepeatBehavior="Forever"/>
-
- <!-- This animation targets the minute hand transform -->
- <DoubleAnimation x:Name="minuteAnimation"
- Storyboard.TargetName="minuteHandTransform"
- Storyboard.TargetProperty="Angle"
- Duration="1:0:0" RepeatBehavior="Forever"/>
-
- <!-- This animation targets the second hand transform -->
- <DoubleAnimation x:Name="secondAnimation"
- Storyboard.TargetName="secondHandTransform"
- Storyboard.TargetProperty="Angle"
- Duration="0:1:0" RepeatBehavior="Forever"/>
- </Storyboard>
- </Canvas.Resources>
-
- <!-- Clock Shadow -->
- <!--<Ellipse Width="330" Height="330" Canvas.Left="40"
- Canvas.Top="40" Fill="Black" Opacity="0.5"/>-->
-
- <!-- Outer rim -->
- <Ellipse Stroke="#FF000000" x:Name="outerCircle" Width="330" Height="330"
- Canvas.Left="32" Canvas.Top="32">
- <Ellipse.Fill>
- <!--This linear gradient creates a subtle shadow effect on the outer rim. -->
- <LinearGradientBrush EndPoint="0.196,0.127" StartPoint="0.852,0.814">
- <GradientStop Color="#FFC0C0C0" Offset="0.788"/>
- <GradientStop Color="#FFE4E5F4" Offset="0.995"/>
- </LinearGradientBrush>
- </Ellipse.Fill>
- </Ellipse>
-
- <!-- Bevel -->
- <Ellipse Stroke="#FF000000" Width="290" Height="281" Canvas.Left="52"
- Canvas.Top="57">
- <Ellipse.Fill>
- <!-- This linear gradient creates a subtle shadow effect on
- the outer rim. -->
- <LinearGradientBrush EndPoint="0.867,0.848" StartPoint="0.232,0.126">
- <GradientStop Offset="0.1"/>
- <GradientStop Color="#FFE4E5F4" Offset="0.995"/>
- </LinearGradientBrush>
- </Ellipse.Fill>
- </Ellipse>
-
- <!-- Clock Face -->
- <!--<Ellipse Stroke="#FF000000" Width="273" Height="265"
- Canvas.Left="60" Canvas.Top="65" Fill="#FF000000"/>-->
-
- <!-- Central Clock Circle -->
- <Ellipse StrokeThickness="7"
- Width="32" Height="31" Canvas.Left="180" Canvas.Top="190">
- <Ellipse.Stroke>
- <LinearGradientBrush EndPoint="1,0.5" StartPoint="0,0.5">
- <GradientStop Color="Black" Offset="0" />
- <GradientStop Color="White" Offset="1" />
- </LinearGradientBrush>
- </Ellipse.Stroke>
- <Ellipse.Fill>
- <LinearGradientBrush EndPoint="1,0.5" StartPoint="0,0.5">
- <GradientStop Color="Black" Offset="1" />
- <GradientStop Color="White" Offset="0" />
- </LinearGradientBrush>
- </Ellipse.Fill>
- </Ellipse>
- <!-- Minute Hand -->
- <Rectangle Fill="Gray" Width="8" Height="80" Canvas.Left="192.5" Canvas.Top="226"
- RenderTransformOrigin="0.41,-0.26" x:Name="MinutedHand">
- <Rectangle.RenderTransform>
- <RotateTransform x:Name="minuteHandTransform"/>
- </Rectangle.RenderTransform>
- </Rectangle>
-
- <!-- Hour Hand -->
- <Rectangle Fill="Gray" Width="10" Height="60" Canvas.Left="192.5" Canvas.Top="226"
- RenderTransformOrigin="0.35,-0.35" x:Name="HourHand">
- <Rectangle.RenderTransform>
- <RotateTransform x:Name="hourHandTransform"/>
- </Rectangle.RenderTransform>
- </Rectangle>
-
- <!-- Second Hand -->
- <Rectangle Fill="Gray" Width="5" Height="80" Canvas.Left="192.5" Canvas.Top="226"
- RenderTransformOrigin="0.65,-0.26" x:Name="SecondHand" >
- <Rectangle.RenderTransform>
- <RotateTransform x:Name="secondHandTransform"/>
- </Rectangle.RenderTransform>
- </Rectangle>
-
- </Canvas>
- </UserControl>
Now go to view code in C#:
- using System;
- using System.Collections.Generic;
- using System.Linq;
- using System.Windows;
- using System.Windows.Controls;
- using System.Windows.Documents;
- using System.Windows.Input;
- using System.Windows.Media;
- using System.Windows.Media.Animation;
- using System.Windows.Shapes;
-
- namespace Clock
- {
- public partial class Page : UserControl
- {
- public Page()
- {
- InitializeComponent();
- }
-
-
- public void Canvas_Loaded(object sender, EventArgs e)
- {
-
-
- System.DateTime date = DateTime.Now;
-
-
-
- double hourangle = (((float)date.Hour) / 12) * 360 + date.Minute / 2;
-
-
-
-
- hourangle += 180;
-
-
- double minangle = (((float)date.Minute) / 60) * 360;
- minangle += 180;
-
-
- double secangle = (((float)date.Second) / 60) * 360;
- secangle += 180;
-
-
-
- hourAnimation.From = hourangle;
-
-
-
-
-
-
- hourAnimation.To = hourangle + 360;
-
-
- minuteAnimation.From = minangle;
- minuteAnimation.To = minangle + 360;
-
-
- secondAnimation.From = secangle;
- secondAnimation.To = secangle + 360;
-
-
- clockStoryboard.Begin();
-
- }
-
- }
-
- }
Canvas and StoryBoard controls are main part of this.
Here is 3D Clock in silverlight with C#.