Click here to Skip to main content
15,355,659 members
Articles / Desktop Programming / WPF
Posted 17 Sep 2008


51 bookmarked

Analog Clock in WPF

Rate me:
Please Sign up or sign in to vote.
4.67/5 (43 votes)
17 Sep 2008CPOL
A simple analog clock in WPF
Analog clock in WPF


This is an analog clock in WPF.

Using the Code

My XAML code is as follows:

<Window x:Class="clock.Window1"
    Title="Clock" Margin="2" Height="327" Width="311" AllowsTransparency="True" 
        WindowStyle="None" Background="Transparent" WindowStartupLocation="CenterScreen" 
        ResizeMode="NoResize" Topmost="False" Opacity="1">
    <Grid Width="300" Height="300" MouseLeftButtonDown="Grid_MouseDown">
       <Image Source="images/backGround.png"></Image>

        <Label Name="christianityCalendar" Foreground="White" 
        Margin="0, 0, 0, 52" HorizontalAlignment="Center" 
        VerticalAlignment="Bottom" Height="Auto"
        <Label Name="persianCalendar" Foreground="White" 
        Margin="0, 0, 0, 75" HorizontalAlignment="Center" 
        VerticalAlignment="Bottom" Height="Auto" 
        <!--<span class="code-comment"> Second  --></span>
        <Rectangle Margin="150,0,149,150" Name="rectangleSecond" 
        Stroke="White" Height="120" VerticalAlignment="Bottom">
                <RotateTransform x:Name="secondHand" CenterX="0" 
                CenterY="120" Angle="0" />
        <!--<span class="code-comment"> Minute  --></span>
        <Rectangle Margin="150,49,149,151" Name="rectangleMinute" 
                <RotateTransform x:Name="minuteHand" CenterX="0" 
                CenterY="100" Angle="0" />
        <!--<span class="code-comment"> Hour  --></span>
        <Rectangle Margin="150,80,149,150" Name="rectangleHour" 
                <RotateTransform x:Name="hourHand" CenterX="0" 
                CenterY="70" Angle="0" />

My C# code is as follows:

System.Timers.Timer timer = new System.Timers.Timer(1000);
public Window1()

    MDCalendar mdCalendar = new MDCalendar();
    DateTime date = DateTime.Now;
    TimeZone time = TimeZone.CurrentTimeZone;
    TimeSpan difference = time.GetUtcOffset(date);
    uint currentTime = mdCalendar.Time() + (uint)difference.TotalSeconds;
    persianCalendar.Content = mdCalendar.Date("Y/m/D  W", currentTime, true);
    christianityCalendar.Content = mdCalendar.Date("P Z/e/d", currentTime, false);

    timer.Elapsed += new System.Timers.ElapsedEventHandler(timer_Elapsed);
    timer.Enabled = true;

void timer_Elapsed(object sender, System.Timers.ElapsedEventArgs e)
    this.Dispatcher.Invoke(DispatcherPriority.Normal, (Action)(() =>
        secondHand.Angle = DateTime.Now.Second * 6;
        minuteHand.Angle = DateTime.Now.Minute * 6;
        hourHand.Angle = (DateTime.Now.Hour * 30) + (DateTime.Now.Minute * 0.5);

private void Grid_MouseDown(object sender, MouseButtonEventArgs e)


  • 17th September, 2008: Initial post


This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)


About the Author

Mohammad Dayyan
Iran (Islamic Republic of) Iran (Islamic Republic of)
No Biography provided

Comments and Discussions

QuestionI used this as a basline and made many improvements Pin
MarkyK6-May-16 6:13
MemberMarkyK6-May-16 6:13 
AnswerRe: I used this as a basline and made many improvements Pin
ZevSpitz7-May-20 7:00
professionalZevSpitz7-May-20 7:00 
GeneralRe: I used this as a basline and made many improvements Pin
Member 1315944427-Apr-21 7:09
MemberMember 1315944427-Apr-21 7:09 
AnswerRe: I used this as a basline and made many improvements Pin
FransClasener1-Mar-22 8:42
MemberFransClasener1-Mar-22 8:42 
Questioncan i resize this Pin
Member 1232179114-Feb-16 4:05
MemberMember 1232179114-Feb-16 4:05 
QuestionMDCalendar Pin
KarolinaNS7-Dec-14 2:54
MemberKarolinaNS7-Dec-14 2:54 
AnswerRe: MDCalendar Pin
Mohammad Dayyan7-Dec-14 3:21
MemberMohammad Dayyan7-Dec-14 3:21 
QuestionNIce Demo Pin
pitambar sahoo10-Nov-14 19:43
Memberpitambar sahoo10-Nov-14 19:43 
QuestionNIce Demo Pin
pitambar sahoo10-Nov-14 19:43
Memberpitambar sahoo10-Nov-14 19:43 
Questionvery good Pin
vahidsing0083-May-14 0:43
Membervahidsing0083-May-14 0:43 
QuestionGood Job Man Pin
Aydin Homay21-Mar-14 3:32
MemberAydin Homay21-Mar-14 3:32 
SuggestionAdded code Pin
Aster Veigas9-Aug-13 10:36
MemberAster Veigas9-Aug-13 10:36 
Questionwhy use a System.Timers.Timer? Pin
tomtom198022-May-12 8:06
Membertomtom198022-May-12 8:06 
GeneralMy vote of 5 Pin
Javad Amiry7-Dec-11 4:50
MemberJavad Amiry7-Dec-11 4:50 
GeneralMy vote of 5 Pin
Shahin Khorshidnia24-Jul-11 1:17
professionalShahin Khorshidnia24-Jul-11 1:17 
QuestionResizable? Pin
Member 77725341-Apr-11 0:00
MemberMember 77725341-Apr-11 0:00 
AnswerRe: Resizable? Pin
Latious19-Dec-12 10:24
MemberLatious19-Dec-12 10:24 
AnswerRe: Resizable? Pin
GeorgeMc3-Jan-15 0:49
MemberGeorgeMc3-Jan-15 0:49 
GeneralMy vote of 3 Pin
Jyothikarthik_N23-Dec-10 18:45
MemberJyothikarthik_N23-Dec-10 18:45 
GeneralMy vote of 5 Pin
masoudxxx200213-Dec-10 19:57
Membermasoudxxx200213-Dec-10 19:57 
GeneralMy vote of 1 Pin
cpaynesai17-Oct-10 17:57
Membercpaynesai17-Oct-10 17:57 
GeneralMy vote of 3 Pin
NNTBIZ25-Aug-10 12:46
professionalNNTBIZ25-Aug-10 12:46 
Generalso beautiful! Pin
wanglinfu4-Jan-10 2:55
Memberwanglinfu4-Jan-10 2:55 
GeneralEY val baba Pin
syamah24-Aug-09 5:13
Membersyamah24-Aug-09 5:13 
GeneralRe: EY val baba Pin
Mohammad Dayyan26-Aug-09 9:16
MemberMohammad Dayyan26-Aug-09 9:16 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.