Click here to Skip to main content
Licence CPOL
First Posted 17 Sep 2008
Views 49,484
Downloads 2,466
Bookmarked 36 times

Analog Clock in WPF

By | 17 Sep 2008 | Article
A simple analog clock in WPF
Analog clock in WPF

Introduction

This is an analog clock in WPF.

Using the Code

My XAML code is as follows:

<Window x:Class="clock.Window1"
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
    Title="Clock" Margin="2" Height="327" Width="311" AllowsTransparency="True" 
        WindowStyle="None" Background="Transparent" WindowStartupLocation="CenterScreen" 
        Icon="images/clock.ico" 
        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"
         Width="Auto">Value
        </Label>
        <Label Name="persianCalendar" Foreground="White" 
        Margin="0, 0, 0, 75" HorizontalAlignment="Center" 
        VerticalAlignment="Bottom" Height="Auto" 
        Width="Auto">Value
        </Label>
        
        <!-- Second  -->
        <Rectangle Margin="150,0,149,150" Name="rectangleSecond" 
        Stroke="White" Height="120" VerticalAlignment="Bottom">
            <Rectangle.RenderTransform>
                <RotateTransform x:Name="secondHand" CenterX="0" 
                CenterY="120" Angle="0" />
            </Rectangle.RenderTransform>
        </Rectangle>
        <!---->
        
        <!-- Minute  -->
        <Rectangle Margin="150,49,149,151" Name="rectangleMinute" 
        Stroke="LightGreen">
            <Rectangle.RenderTransform>
                <RotateTransform x:Name="minuteHand" CenterX="0" 
                CenterY="100" Angle="0" />
            </Rectangle.RenderTransform>
        </Rectangle>
        <!---->
        
        <!-- Hour  -->
        <Rectangle Margin="150,80,149,150" Name="rectangleHour" 
        Stroke="LightYellow">
            <Rectangle.RenderTransform>
                <RotateTransform x:Name="hourHand" CenterX="0" 
                CenterY="70" Angle="0" />
            </Rectangle.RenderTransform>
        </Rectangle>
        <!---->
        
    </Grid>
</Window>

My C# code is as follows:

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

    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)
{
    this.DragMove();
}

History

  • 17th September, 2008: Initial post

License

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)

Member



Sign Up to vote   Poor Excellent
Add a reason or comment to your vote: x
Votes of 3 or less require a comment

Comments and Discussions

 
You must Sign In to use this message board. (secure sign-in)
 
Search this forum  
 FAQ
    Noise  Layout  Per page   
  Refresh
Questionwhy use a System.Timers.Timer? Pinmembertomtom19808:06 22 May '12  
GeneralMy vote of 5 PinmemberJavad Amiry4:50 7 Dec '11  
GeneralMy vote of 5 PinmemberShahin Khorshidnia1:17 24 Jul '11  
QuestionResizable? PinmemberMember 77725340:00 1 Apr '11  
GeneralMy vote of 3 PinmemberJyothikarthk18:45 23 Dec '10  
GeneralMy vote of 5 Pinmembermasoudxxx200219:57 13 Dec '10  
GeneralMy vote of 1 Pinmembercpaynesai17:57 17 Oct '10  
GeneralMy vote of 3 PinmemberNNTBIZ12:46 25 Aug '10  
Generalso beautiful! Pinmemberwanglinfu2:55 4 Jan '10  
GeneralEY val baba Pinmembersyamah5:13 24 Aug '09  
GeneralRe: EY val baba PinmemberMohammad Dayyan9:16 26 Aug '09  
GeneralSuggestion PinmemberDrew McGhie12:22 23 Apr '09  
Questionanti-alias? PinmemberUnruled Boy16:52 23 Sep '08  
the minute hand is zigzag Sigh | :sigh:
 
Regards,
unruledboy (at) gmail (dot) com
http://www.xnlab.com

GeneralKhaylee Mamnoon PinmemberJamal Alqabandi0:10 23 Sep '08  
GeneralRe: Khaylee Mamnoon PinmemberMohammad Dayyan1:12 23 Sep '08  
GeneralGood but could be better ;) PinmemberSiavash Mortazavi9:14 22 Sep '08  
GeneralRe: Good but could be better ;) PinmemberMohammad Dayyan9:39 22 Sep '08  
Generalvery god Pinmemberhassan_kahrizy10:59 20 Sep '08  
GeneralRe: very good PinmemberMohammad Dayyan11:42 20 Sep '08  
GeneralReally Wonderful Pinmembervkuttyp6:41 20 Sep '08  
GeneralRe: Really Wonderful PinmemberMohammad Dayyan12:43 21 Sep '08  

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

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

Permalink | Advertise | Privacy | Mobile
Web02 | 2.5.120604.1 | Last Updated 17 Sep 2008
Article Copyright 2008 by Mohammad Dayyan
Everything else Copyright © CodeProject, 1999-2012
Terms of Use
Layout: fixed | fluid