Click here to Skip to main content
6,291,522 members and growing! (13,665 online)
Email Password   helpLost your password?
Platforms, Frameworks & Libraries » Windows Presentation Foundation » General     Intermediate License: The Code Project Open License (CPOL)

Analog Clock in Expression Blend 2

By Mohammad Dayyan

A simple analog clock in Expression Blend 2
XML, C# 3.0WinXP, Vista, .NET 3.5, XAML, WPF, VS2008, Dev
Posted:19 Nov 2008
Views:7,155
Bookmarked:14 times
Announcements
Loading...
 
Search    
Advanced Search
printPrint   Broken Article?Report       add Share
  Discuss Discuss   Recommend Article Email
18 votes for this article.
Popularity: 4.32 Rating: 3.44 out of 5
4 votes, 22.2%
1

2
4 votes, 22.2%
3
4 votes, 22.2%
4
6 votes, 33.3%
5

EB2.jpg

Introduction

This is a simple WPF App in Expression Blend 2 SP1 that shows you how we can create an analog clock with some effects like Shadow and Blur.
I haven't used any images in this project and everything that you see has been created with XAML code.

Background

Some days ago, I created another very simple anolog clock in Visual Studio 2008. If you think this project is a little difficult, you can see that first.
These training videos can also help you to work with Expression Blend. I've used them.

Using the Code

In Expression Blend, we don't need to edit XAML code manually (typically), but I think that if we know how the XAML code works, it helps us to create a better WPF app.

Well, the difficult part of this project is creating three pointers for our clock with rotation ability. For that, I've used this XAML code:

  <Rectangle Fill="#FFDCDCDC" Margin="86.169,8,85.904,88.555" 
  Stroke="#FFDCDCDC" StrokeDashCap="Round" 
  StrokeEndLineCap="Round" StrokeLineJoin="Round" 
  StrokeStartLineCap="Round" StrokeThickness="1" 
  RadiusX="1" RadiusY="1" Opacity="0.8" 
  x:Name="rectangleSecond" RenderTransformOrigin="0.5,1" 
  d:IsHidden="True"> 
		<Rectangle.RenderTransform>
			<TransformGroup>
				<ScaleTransform ScaleX="1" ScaleY="1"/>
				<SkewTransform AngleX="0" AngleY="0"/>
				<RotateTransform x:Name="secondHand" Angle="0"/>
				<TranslateTransform X="0" Y="0"/>
			</TransformGroup>
		</Rectangle.RenderTransform>
</Rectangle>

Now we can rotate this pointer easily with this C# code:

secondHand.Angle = DateTime.Now.Second * 6;

That was very easy. Wasn't it?

Now we use a simple method for rotating our three pointers:

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); 
	}));
}

For further learning about the above method, you can see here.
That's all.

History

  • 20th November, 2008: First 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


Member

Location: Iran, Islamic Republic Of Iran, Islamic Republic Of

Other popular Windows Presentation Foundation articles:

Article Top
You must Sign In to use this message board.
FAQ FAQ 
 
Noise Tolerance  Layout  Per page   
 Msgs 1 to 5 of 5 (Total in Forum: 5) (Refresh)FirstPrevNext
GeneralNice clock. Simple and pretty PinmemberRoger Bacon12:22 21 Apr '09  
GeneralInvalid tags Pinmemberberzie20:31 24 Jan '09  
GeneralMy vote of 1 PinmemberCountry Man2:58 27 Nov '08  
Generalnice clock Pinmemberchristoph braendle23:04 19 Nov '08  
GeneralRe: nice clock PinmemberMohammad Dayyan5:10 20 Nov '08  

General General    News News    Question Question    Answer Answer    Joke Joke    Rant Rant    Admin Admin   

PermaLink | Privacy | Terms of Use
Last Updated: 19 Nov 2008
Editor: Deeksha Shenoy
Copyright 2008 by Mohammad Dayyan
Everything else Copyright © CodeProject, 1999-2009
Web13 | Advertise on the Code Project