Universal Windows Platform – Clock Control

Clock Control demonstrates how to create an Analogue Clock Control

Step 1

If not already, follow Setup and Start on how to Install and get Started with Visual Studio 2017 or in Windows 10 choose Start, and then from the Start Menu find and select Visual Studio 2017.

vs2017

Step 2

Once Visual Studio Community 2017 has started, from the Menu choose File, then New then Project…

vs2017-file-new-project

Step 3

From New Project choose Visual C# from Installed, Templates then choose Blank App (Universal Windows) and then type in a Name and select a Location and then select Ok to create the Project
vs2017-new-project-window

Step 4

Then in New Universal Windows Project you need to select the Target Version this should be at least the Windows 10 Fall Creators Update (10.0; Build 16299) and the Minimum Version to be the same.

vs2017-target-platform

The Target Version will control what features your application can use in Windows 10 so by picking the most recent version you’ll be able to take advantage of those features. To make sure you always have the most recent version, in Visual Studio 2017 select Tools Extensions and Updates… then and then see if there are any Updates

Step 5

Once done select from the Menu, Project, then Add New Item…

vs2017-project-add-new-item

Step 6

From the Add New Item window select Visual C#, then XAML from Installed then select User Control from the list, then type in the Name as Clock.xaml before selecting Add to add the file to the Project

vs2017-add-new-item-clock-control

Step 7

Once in the Design View for Clock.xaml between the Grid and /Grid elements the following should be entered:

<Canvas Height="300" Width="300" Name="Display" Background="{Binding Background, ElementName=userControl}" Loaded="Display_Loaded"/>

In the Clock.xaml a Canvas has been defined, this will be where the Clock itself will appear within.

Step 8

From the Menu choose View and then Code

vs2017-view-code

Step 9

Once in the Code View, below the end of public Clock() { … } the following Code should be entered:

private DispatcherTimer _timer = new DispatcherTimer();
private Canvas _markers = new Canvas();
private Canvas _face = new Canvas();
private Windows.UI.Xaml.Shapes.Rectangle _secondsHand;
private Windows.UI.Xaml.Shapes.Rectangle _minutesHand;
private Windows.UI.Xaml.Shapes.Rectangle _hoursHand;

private static Windows.UI.ViewManagement.UISettings _uiSettings = new Windows.UI.ViewManagement.UISettings();
private Brush _foreground = new SolidColorBrush(_uiSettings.GetColorValue(Windows.UI.ViewManagement.UIColorType.Background));
private Brush _background = new SolidColorBrush(_uiSettings.GetColorValue(Windows.UI.ViewManagement.UIColorType.Accent));

private int _secondsWidth = 1;
private int _secondsHeight;
private int _minutesWidth = 5;
private int _minutesHeight;
private int _hoursWidth = 8;
private int _hoursHeight;
private double _diameter;

public bool IsRealTime { get; set; } = true;
public bool ShowSeconds { get; set; } = true;
public bool ShowMinutes { get; set; } = true;
public bool ShowHours { get; set; } = true;
public DateTime Time { get; set; } = DateTime.Now;

private Windows.UI.Xaml.Shapes.Rectangle Hand(double width, double height, double radiusX, double radiusY, double thickness)
{
	Windows.UI.Xaml.Shapes.Rectangle hand = new Windows.UI.Xaml.Shapes.Rectangle
	{
		Width = width,
		Height = height,
		Fill = _background,
		StrokeThickness = thickness,
		RadiusX = radiusX,
		RadiusY = radiusY
	};
	return hand;
}

private void RemoveHand(ref Windows.UI.Xaml.Shapes.Rectangle hand)
{
	if (hand != null && _face.Children.Contains(hand))
	{
		_face.Children.Remove(hand);
	}
}

private void AddHand(ref Windows.UI.Xaml.Shapes.Rectangle hand)
{
	if (!_face.Children.Contains(hand))
	{
		_face.Children.Add(hand);
	}
}

private TransformGroup TransformGroup(double angle, double x, double y)
{
	TransformGroup transformGroup = new TransformGroup();
	TranslateTransform firstTranslate = new TranslateTransform
	{
		X = x,
		Y = y
	};
	transformGroup.Children.Add(firstTranslate);
	RotateTransform rotateTransform = new RotateTransform
	{
		Angle = angle
	};
	transformGroup.Children.Add(rotateTransform);
	TranslateTransform secondTranslate = new TranslateTransform
	{
		X = _diameter / 2,
		Y = _diameter / 2
	};
	transformGroup.Children.Add(secondTranslate);
	return transformGroup;
}

private void SecondHand(int seconds)
{
	RemoveHand(ref _secondsHand);
	if (ShowSeconds)
	{
		_secondsHand = Hand(_secondsWidth, _secondsHeight, 0, 0, 0);
		_secondsHand.RenderTransform = TransformGroup(seconds * 6,
		-_secondsWidth / 2, -_secondsHeight + 4.25);
		AddHand(ref _secondsHand);
	}
}

private void MinuteHand(int minutes, int seconds)
{
	RemoveHand(ref _minutesHand);
	if (ShowMinutes)
	{
		_minutesHand = Hand(_minutesWidth, _minutesHeight, 2, 2, 0.6);
		_minutesHand.RenderTransform = TransformGroup(6 * minutes + seconds / 10,
		-_minutesWidth / 2, -_minutesHeight + 4.25);
		AddHand(ref _minutesHand);
	}
}

private void HourHand(int hours, int minutes, int seconds)
{
	RemoveHand(ref _hoursHand);
	if (ShowHours)
	{
		_hoursHand = Hand(_hoursWidth, _hoursHeight, 3, 3, 0.6);
		_hoursHand.RenderTransform = TransformGroup(30 * hours + minutes / 2 + seconds / 120,
		-_hoursWidth / 2, -_hoursHeight + 4.25);
		AddHand(ref _hoursHand);
	}
}

private void Layout(ref Canvas canvas)
{
	canvas.Children.Clear();
	_diameter = canvas.Width;
	double inner = _diameter - 15;
	Windows.UI.Xaml.Shapes.Ellipse rim = new Windows.UI.Xaml.Shapes.Ellipse
	{
		Height = _diameter,
		Width = _diameter,
		Stroke = RimBackground,
		StrokeThickness = 20
	};
	canvas.Children.Add(rim);
	_markers.Children.Clear();
	_markers.Width = inner;
	_markers.Height = inner;
	for (int i = 0; i < 60; i++)
	{
		Windows.UI.Xaml.Shapes.Rectangle marker = 
			new Windows.UI.Xaml.Shapes.Rectangle
		{
			Fill = RimForeground
		};
		if ((i % 5) == 0)
		{
			marker.Width = 3;
			marker.Height = 8;
			marker.RenderTransform = TransformGroup(i * 6, -(marker.Width / 2),
			-(marker.Height * 2 + 4.5 - rim.StrokeThickness / 2 - inner / 2 - 6));
		}
		else
		{
			marker.Width = 1;
			marker.Height = 4;
			marker.RenderTransform = TransformGroup(i * 6, -(marker.Width / 2),
			-(marker.Height * 2 + 12.75 - rim.StrokeThickness / 2 - inner / 2 - 8));
		}
		_markers.Children.Add(marker);
	}
	canvas.Children.Add(_markers);
	_face.Width = _diameter;
	_face.Height = _diameter;
	canvas.Children.Add(_face);
	_secondsHeight = (int)_diameter / 2 - 20;
	_minutesHeight = (int)_diameter / 2 - 40;
	_hoursHeight = (int)_diameter / 2 - 60;
}

public Brush RimForeground
{
	get { return _foreground; }
	set
	{
		_foreground = value;
		Layout(ref Display);
	}
}

public Brush RimBackground
{
	get { return _background; }
	set
	{
		_background = value;
		Layout(ref Display);
	}
}

public bool Enabled
{
	get { return _timer.IsEnabled; }
	set
	{
		if (_timer.IsEnabled)
		{
			_timer.Stop();
		}
		else
		{
			_timer.Start();
		}
	}
}

private void Display_Loaded(object sender, RoutedEventArgs e)
{
	Layout(ref Display);
	_timer.Interval = TimeSpan.FromSeconds(1);
	_timer.Tick += (object s, object obj) =>
	{
		if (IsRealTime) Time = DateTime.Now;
		SecondHand(Time.Second);
		MinuteHand(Time.Minute, Time.Second);
		HourHand(Time.Hour, Time.Minute, Time.Second);
	};
	_timer.Start();
}

Below the Clock() Method are the Members that make up the various elements of the Clock, there are also some settings that allow the Clock to take advantage of standard system Colours such as the global Background and Accent colours. The Hand Method is used to create the hands of the Clock itself, RemoveHand and AddHand is used for adding or removing the hands and TransformGroup is used to manupulate the hands to rotate them into the correct positions to represent the time. SecondHand, MinuteHand and HourHand are used to set up the various hands, with Layout used to create the main layout of the Clock face including markers around the rim. Then there are various properties for the look of the Clock including RimForeground and RimBackground, the Display_Loaded happens when the Control is loaded and will initialise the look-and-feel of the Control.

Step 10

Once done select from the Menu, Build, then Build Solution

vs2017-build-build-solution

Step 11

In the Solution Explorer select MainPage.xaml

vs2017-mainpage

Step 12

From the Menu choose View and then Designer

vs2017-view-designer

Step 13

The Design View will be displayed along with the XAML View and in this between the Grid and /Grid elements, enter the following XAML:

<Viewbox>
	<local:Clock Margin="50"/>
</Viewbox>

The block of XAML represents the Clock Control and it will also appear to work in the Designer.

Step 14

That completes the Universal Windows Platform Application so Save the Project then in Visual Studio select the Local Machine to run the Application

vs2017-local-machine

Step 15

After the Application has started running it should then appear with the Clock Control displayed with the current time

ran-clock-control

Step 16

To Exit the Application select the Close button in the top right of the Application

vs2017-close

Creative Commons License

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s