|
|
|
|
|
|
Author Rank:
|
|
Technologies:
Silverlight, WPF,Visual C# .NET
|
|
Total downloads :
|
160
|
|
Total page views :
|
3299
|
|
Rating :
|
|
3/5
|
|
This article has been rated :
|
1 times
|
|
|
|
|
Download
Files:
|
|
|
|
|
|
|
|
|
|
|
Similar ArticlesMost ReadTop RatedLatest
|
|
Related EbooksTop Videos
|
|
|
Description
|
|
The Complete Visual C# Programmer's Guide, written by the authors of C# Corner, covers most of the major components that make up C# and the .NETenvironment including Windows Forms, ADO.NET, GDI+, Web Services, and Security. The book is geared toward the beginner to intermediate programmers.
|
|
|
|
|
|
|
|
|
|
|
|
|
Introduction
You might have seen many analog clocks made in either in blend 3 or Silverlight 2/3. But the code they used to provide is very hard to follow. Here we will try to minimize the code. Actually I got fed up with the code itself tried the old school.

Figure 1.1 Analog Clock Widget
Steps
To begin with we need the following requirements:
-
Silverlight 3
-
Visual Studio 2008 SP1
-
Expression Blend 3
Creating a Silverlight Project with or without RIA enabled
-
Create project in Visual Studio and open the solution in Expression Blend 3.
-
The following figure will guide you for that.

Figure 1.2 Create a new Silverlight project

Figure 1.3 Uncheck the bottom checkbox to create the project without RIA support
Designing the User Control in Expression Blend 3
The User Control we are going to make should contain a background which matches an analog clock. I am taking a simple .png file which is looking simple.

Figure 1.4 Analog Clock Background.
Now our task is to create hour hand, minute hand and second hand. We will take rectangle control and design a simple second hand; as you can see from the figure. We will further modify it to create our next two shapes that is minute hand and hour hand the below figure and XAML code will help you regarding this.

Figure 1.5 Designing the rectangular shapes.
The XAML will look like the following after all the design changes above.
<UserControl xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" xmlns:d="http://schemas.microsoft.com/expression/blend/2008" xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006" x:Class="AnalogClockSilverlight.MainPage" Width="260" Height="260" mc:Ignorable="d">
<Grid x:Name="LayoutRoot"> <Grid.RowDefinitions> <RowDefinition Height="*"/> </Grid.RowDefinitions> <Grid.ColumnDefinitions> <ColumnDefinition Width="*"/> </Grid.ColumnDefinitions>
<Image Source="images/myClock.png" d:IsLocked="True"/>
<Path x:Name="SecondHand" Stretch="Fill" Stroke="#00000000" Height="88" Margin="128.47,48,126.808,0" VerticalAlignment="Top" RenderTransformOrigin="0.394,0.941" Data="M0.5,0.5 L4.5,0.5 L4.5,95.5 L0.5,95.5 z" UseLayoutRounding="False" d:IsLocked="True"> <Path.Fill> <LinearGradientBrush EndPoint="0.5,1" StartPoint="0.5,0"> <GradientStop Color="#FFF80500" Offset="0.274"/> <GradientStop Color="#FFDBA63C" Offset="0.9"/> </LinearGradientBrush> </Path.Fill> <Path.RenderTransform> <TransformGroup> <RotateTransform x:Name="SecondRotate" Angle="0"/> </TransformGroup> </Path.RenderTransform> </Path> <Path x:Name="MinuteHand" Stretch="Fill" Stroke="#00000000" Height="76.25" Margin="128.331,59.75,126.669,0" VerticalAlignment="Top" RenderTransformOrigin="0.4,0.923" Data="M0.5,0.5 L4.5,0.5 L4.5,95.5 L0.5,95.5 z" UseLayoutRounding="False" d:IsLocked="True"> <Path.Fill> <LinearGradientBrush EndPoint="0.5,1" StartPoint="0.5,0"> <GradientStop Color="#FF80AACF" Offset="0.509"/> <GradientStop Color="#FF46B644" Offset="1"/> </LinearGradientBrush> </Path.Fill> <Path.RenderTransform> <TransformGroup> <RotateTransform x:Name="MinuteRotate" Angle="0"/> </TransformGroup> </Path.RenderTransform> </Path> <Path x:Name="HourHand" Stretch="Fill" Stroke="#00000000" Height="52.5" Margin="128.331,83.5,126.669,0" VerticalAlignment="Top" RenderTransformOrigin="0.4,0.889" Data="M0.5,0.5 L4.5,0.5 L4.5,95.5 L0.5,95.5 z" UseLayoutRounding="False" d:IsLocked="True"> <Path.Fill> <LinearGradientBrush EndPoint="0.5,1" StartPoint="0.5,0"> <GradientStop Color="#FF539DDE" Offset="0.73"/> <GradientStop Color="#FF46B644" Offset="1"/> <GradientStop Color="#FF47B546" Offset="1"/> </LinearGradientBrush> </Path.Fill> <Path.RenderTransform> <TransformGroup> <RotateTransform x:Name="HourRotate" Angle="0"/> </TransformGroup> </Path.RenderTransform> </Path> </Grid> </UserControl>
Rotating the Second Hand
This is simple mathemtics for rotating second hand. As it travels 360 degrees in 60seconds so it will rotate 6 degrees in each second. See the below code.
double secondRotateValue = Convert.ToDouble(DateTime.Now.Second.ToString()); secondRotateValue = Convert.ToDouble(DateTime.Now.Second.ToString())*6; //SecondRotate is the rectangle name SecondRotate.Angle = secondRotateValue;
Rotating the Minute Hand & Hour Hand
Now we have to go back to our college days and find out the logic for finding out the angle rotated by the minute hand or the hour hand. Or you can search it in google. The formula says as follows:
double hourRotateValue = Convert.ToDouble(DateTime.Now.Hour.ToString()); double minuteRotateValue = Convert.ToDouble(DateTime.Now.Minute.ToString()); minuteRotateValue = (minuteRotateValue + secondRotateValue/60)* 6; SecondRotate.Angle = secondRotateValue; MinuteRotate.Angle = minuteRotateValue;
Ticking in Real Time
If you want to tick your second hand in real time, then you need to add the following event and write the following codes.
public partial class ClockWidget : UserControl { public ClockWidget() { InitializeComponent(); StartTimer(null, null); }
public void StartTimer(object o, RoutedEventArgs sender) { System.Windows.Threading.DispatcherTimer myDispatcherTimer = new System.Windows.Threading.DispatcherTimer(); myDispatcherTimer.Interval = new TimeSpan(0, 0, 0, 0, 1000); // 100 Milliseconds myDispatcherTimer.Tick += new EventHandler(Each_Tick); myDispatcherTimer.Start(); }
// Fires every 1000 miliseconds while the DispatcherTimer is active. public void Each_Tick(object o, EventArgs sender) { double hourRotateValue = Convert.ToDouble(DateTime.Now.Hour.ToString()); double minuteRotateValue = Convert.ToDouble(DateTime.Now.Minute.ToString()); double secondRotateValue = Convert.ToDouble(DateTime.Now.Second.ToString()); hourRotateValue = (hourRotateValue + minuteRotateValue/60) * 30; minuteRotateValue = (minuteRotateValue + secondRotateValue/60)* 6; secondRotateValue = Convert.ToDouble(DateTime.Now.Second.ToString())*6; SecondRotate.Angle = secondRotateValue; MinuteRotate.Angle = minuteRotateValue; HourRotate.Angle = hourRotateValue; } }
Enjoy the Clock Widget, hope you like it.
|
|
|
Login
to add your contents and source code to this article
|
|
|
|
|
|
|
|
|
|
|
|
Diptimaya Patra
Diptimaya is working as a Software Engineer in UST Global Inc, Trivandrum Center. He is interested in Microsoft Technologies. He is a good learner. He has a large exposure on Microsoft Office SharePoint Services 2007, Windows SharePoint Services, Silverlight 2, Silverlight 3, Blend 2, Blend 3, WPF, WCF, ASP.NET, AJAX, and NHibernate.
He is a native of Cuttack, Orissa.
Reach him at diptimaya.patra@gmail.com
|
|
|
|
|
|
|
|
|
C# Consulting is founded in 2002 by the founders of C# Corner. Unlike a traditional
consulting company, our consultants are well-known experts in .NET and many of them
are MVPs, authors, and trainers. We specialize in Microsoft .NET development and
utilize Agile Development and Extreme Programming practices to provide fast pace
quick turnaround results. Our software development model is a mix of Agile Development,
traditional SDLC, and Waterfall models.
|
|
Click here to learn more about C# Consulting. |
|
|
|
|
|
|
|
Introducing MaxV - one click. infinite control. Hyper-V Hosting from MaximumASP.
Finally – a virtual platform that delivers next-generation Windows Server 2008 Hyper-V virtualization technology from a managed hosting partner you can truly depend on. Visit www.maximumasp.com/max for a FREE 30 day trial. Hurry offer ends soon.
Climb aboard the MaxV platform and take advantage of High Availability, Intelligent Monitoring, Recurrent Backups, and Scalability – with no hassle or hidden fees.
As a managed hosting partner focused solely on Microsoft technologies since 2000, MaximumASP is uniquely qualified to provide the superior support that our business is built on. Unparalleled expertise with Microsoft technologies lead to working directly with Microsoft as first to offer IIS 7 and SQL 2008 betas in a hosted environment; partnering in the Go Live Program for Hyper-V; and product co-launches built on WS 2008 with Hyper-V technology.
|
Dynamic PDF
ceTE software specializes in components for dynamic PDF generation and manipulation. The DynamicPDF™ product line allows you to dynamically generate PDF documents, merge PDF documents and new content to existing PDF documents from within your applications.
|
Go.NET
Build custom interactive diagrams, network, workflow editors, flowcharts, or software design tools. Includes many predefined kinds of nodes, links, and basic shapes. Supports layers, scrolling, zooming, selection, drag-and-drop, clipboard, in-place editing, tooltips, grids, printing, overview window, palette. 100% implemented in C# as a managed .NET Control. Document/View/Tool architecture with many properties&events. Optional automatic layout.
|
Dundas Software
Dundas Chart for .NET is the most advanced .NET charting package available today. With an extremely complete feature set, elegant architecture and easy implementation, Dundas Chart can quickly add advanced Charting functionality to enhance and transform ASP.NET and Windows Forms applications. Whether you are implementing charting into internal projects, or building applications for clients, Dundas Chart offers advanced technology and advanced results to get the most out of data.
|
Clickatell's SMS Gateway
Clickatell's Developer Solutions allow you to SMS enable any website or
application via a range of API's. Learn More about our API connections.
|
Microsoft Visual Studio 2010
Microsoft Visual Studio 2010 offers more to developers than any other
Visual Studio release. Work more productively and collaboratively-with
greater control over your work at every step. The Beta 2 can give you a
head start on achieving efficiency.
|
|
|
|
|
|
|
|
|
Download
Files:
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|