Click here to Skip to main content
12,244,103 members (50,684 online)
Click here to Skip to main content
Add your own
alternative version

Stats

93.7K views
8.5K downloads
46 bookmarked
Posted

Analog Clock in WPF

, 17 Sep 2008 CPOL
Rate this:
Please Sign up or sign in to vote.
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>
        
        <!--<span class="code-comment"> Second  --></span>
        <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>
        <!---->
        
        <!--<span class="code-comment"> Minute  --></span>
        <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>
        <!---->
        
        <!--<span class="code-comment"> Hour  --></span>
        <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)

Share

About the Author

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

You may also be interested in...

Comments and Discussions

 
Questioncan i resize this Pin
Member 1232179114-Feb-16 5:05
memberMember 1232179114-Feb-16 5:05 
QuestionMDCalendar Pin
KarolinaNS7-Dec-14 3:54
memberKarolinaNS7-Dec-14 3:54 
AnswerRe: MDCalendar Pin
Mohammad Dayyan7-Dec-14 4:21
memberMohammad Dayyan7-Dec-14 4:21 
QuestionNIce Demo Pin
pitambar sahoo10-Nov-14 20:43
memberpitambar sahoo10-Nov-14 20:43 
QuestionNIce Demo Pin
pitambar sahoo10-Nov-14 20:43
memberpitambar sahoo10-Nov-14 20:43 
Questionvery good Pin
vahidsing0083-May-14 1:43
membervahidsing0083-May-14 1:43 
QuestionGood Job Man Pin
AYDIN EBRAHIMI HOMAY21-Mar-14 4:32
professionalAYDIN EBRAHIMI HOMAY21-Mar-14 4:32 
SuggestionAdded code Pin
Aster Veigas9-Aug-13 11:36
memberAster Veigas9-Aug-13 11:36 
Questionwhy use a System.Timers.Timer? Pin
tomtom198022-May-12 9:06
membertomtom198022-May-12 9:06 
GeneralMy vote of 5 Pin
Javad Amiry7-Dec-11 5:50
memberJavad Amiry7-Dec-11 5:50 
GeneralMy vote of 5 Pin
Shahin Khorshidnia24-Jul-11 2:17
memberShahin Khorshidnia24-Jul-11 2:17 
QuestionResizable? Pin
Member 77725341-Apr-11 1:00
memberMember 77725341-Apr-11 1:00 
AnswerRe: Resizable? Pin
Latious19-Dec-12 11:24
memberLatious19-Dec-12 11:24 
AnswerRe: Resizable? Pin
GeorgeMc3-Jan-15 1:49
memberGeorgeMc3-Jan-15 1:49 
GeneralMy vote of 3 Pin
Jyothikarthk23-Dec-10 19:45
memberJyothikarthk23-Dec-10 19:45 
GeneralMy vote of 5 Pin
masoudxxx200213-Dec-10 20:57
membermasoudxxx200213-Dec-10 20:57 
GeneralMy vote of 1 Pin
cpaynesai17-Oct-10 18:57
membercpaynesai17-Oct-10 18:57 
GeneralMy vote of 3 Pin
NNTBIZ25-Aug-10 13:46
memberNNTBIZ25-Aug-10 13:46 
Generalso beautiful! Pin
wanglinfu4-Jan-10 3:55
memberwanglinfu4-Jan-10 3:55 
GeneralEY val baba Pin
syamah24-Aug-09 6:13
membersyamah24-Aug-09 6:13 
GeneralRe: EY val baba Pin
Mohammad Dayyan26-Aug-09 10:16
memberMohammad Dayyan26-Aug-09 10:16 
GeneralSuggestion Pin
Drew McGhie23-Apr-09 13:22
memberDrew McGhie23-Apr-09 13:22 
Questionanti-alias? Pin
Unruled Boy23-Sep-08 17:52
memberUnruled Boy23-Sep-08 17:52 
GeneralKhaylee Mamnoon Pin
Jamal Alqabandi23-Sep-08 1:10
memberJamal Alqabandi23-Sep-08 1:10 
GeneralRe: Khaylee Mamnoon Pin
Mohammad Dayyan23-Sep-08 2:12
memberMohammad Dayyan23-Sep-08 2:12 

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.

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