Windows 10 Universal Windows Platform – Dial Control

Step 1

Download Visual Studio Community 2015 and install it onto your computer, if it’s already downloaded and installed select Launch to start Visual Studio Community 2015 or if it has already been downloaded and installed then start the application you may also need to Enable your device for development.

10-home

Step 2

Once Visual Studio Community 2015 has started select File, then New, then Project… from the Menu.

10-file-new-project

Step 3

From the New Project window select Visual C# from Installed, Templates then select Blank App (Windows Universal) from the list, then type in the Name DialControl and select a Location to save to before selecting Ok to create the Project.

10-new-project-dialcontrol

Step 4

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

10-project-add-new-item

Step 5

From the Add New Item window select Visual C# from Installed then select Templated Control from the list, then type in the Name as Dial.cs before selecting Add to add the file to the Project

10-add-new-item-dialcontrol

Step 6

Once in the Code View for Dial.cs below the public Dial() { … } the following should be entered:

Grid knob;
RotateTransform value;
bool hasCapture = false;

public static readonly DependencyProperty ValueProperty =
DependencyProperty.Register("Value", typeof(double),
typeof(Dial), null);

public static readonly DependencyProperty MinimumProperty =
DependencyProperty.Register("Minimum", typeof(double),
typeof(Dial), null);

public static readonly DependencyProperty MaximumProperty =
DependencyProperty.Register("Maximum", typeof(double),
typeof(Dial), null);

public static readonly DependencyProperty KnobProperty =
DependencyProperty.Register("Knob", typeof(UIElement),
typeof(Dial), null);

public static readonly DependencyProperty FaceProperty =
DependencyProperty.Register("Face", typeof(UIElement),
typeof(Dial), null);

public double Value
{
	get { return (double)GetValue(ValueProperty); }
	set { SetValue(ValueProperty, value); }
}

public double Minimum
{
	get { return (double)GetValue(MinimumProperty); }
	set { SetValue(MinimumProperty, value); }
}

public double Maximum
{
	get { return (double)GetValue(MaximumProperty); }
	set { SetValue(MaximumProperty, value); }
}

public UIElement Knob
{
	get { return (UIElement)GetValue(KnobProperty); }
	set { SetValue(KnobProperty, value); }
}

public UIElement Face
{
	get { return (UIElement)GetValue(FaceProperty); }
	set { SetValue(FaceProperty, value); }
}

private double angleQuadrant(double width, double height, Windows.Foundation.Point point)
{
	double radius = width / 2;
	Windows.Foundation.Point centre = new Windows.Foundation.Point(radius, height / 2);
	Windows.Foundation.Point start = new Windows.Foundation.Point(0, height / 2);
	double triangleTop = Math.Sqrt(Math.Pow((point.X - centre.X), 2)
	  + Math.Pow((centre.Y - point.Y), 2));
	double triangleHeight = (point.Y > centre.Y) ?
	  point.Y - centre.Y : centre.Y - point.Y;
	return ((triangleHeight * Math.Sin(90)) / triangleTop) * 100;
}

private double getAngle(Windows.Foundation.Point point)
{
	double diameter = knob.ActualWidth;
	double height = knob.ActualHeight;
	double radius = diameter / 2;
	double rotation = angleQuadrant(diameter, height, point);
	if ((point.X > radius) && (point.Y <= radius))
	{
		rotation = 90.0 + (90.0 - rotation);
	}
	else if ((point.X > radius) && (point.Y > radius))
	{
		rotation = 180.0 + rotation;
	}
	else if ((point.X < radius) && (point.Y > radius))
	{
		rotation = 270.0 + (90.0 - rotation);
	}
	return rotation;
}

private void setPosition(double rotation)
{
	if (Minimum > 0 && Maximum > 0 && Minimum < 360 && Maximum <= 360)
	{
		if (rotation < Minimum) { rotation = Minimum; }
		if (rotation > Maximum) { rotation = Maximum; }
	}
	value.Angle = rotation;
	Value = rotation;
}

protected override void OnApplyTemplate()
{
	base.OnApplyTemplate();
	knob = ((Grid)GetTemplateChild("Knob"));
	value = ((RotateTransform)GetTemplateChild("DialValue"));
	if (Minimum > 0 && Minimum < 360) { setPosition(Minimum); }
	knob.PointerReleased += (object sender, PointerRoutedEventArgs e) =>
	{
		hasCapture = false;
	};
	knob.PointerPressed += (object sender, PointerRoutedEventArgs e) =>
	{
		hasCapture = true;
		setPosition(getAngle(e.GetCurrentPoint(knob).Position));
	};
	knob.PointerMoved += (object sender, PointerRoutedEventArgs e) =>
	{
		if (hasCapture)
		{
			setPosition(getAngle(e.GetCurrentPoint(knob).Position));
		}
	};
	knob.PointerExited += (object sender, PointerRoutedEventArgs e) =>
	{
		hasCapture = false;
	};
}

It should then appear as such:

10-templated-code-dialcontrol

Step 7

Select from the Menu, Build, then Build Solution

10-build

Step 8

From the Solution Explorer select Generic.xaml

10-generic-dialcontrol

Step 9

Select from the Menu, View then Designer

10-designer

Step 10

Along with Design View will be the XAML View and in this remove the following XAML:

<Border
	Background="{TemplateBinding Background}"
	BorderBrush="{TemplateBinding BorderBrush}"
	BorderThickness="{TemplateBinding BorderThickness}">
</Border>

And replace with the following XAML;

<Grid x:Name="Knob">
	<ContentPresenter x:Name="DialFace" Content="{TemplateBinding Face}"/>
	<ContentPresenter x:Name="DialKnob" Content="{TemplateBinding Knob}" RenderTransformOrigin="0.5,0.5">
		<ContentPresenter.RenderTransform>
			<TransformGroup>
				<RotateTransform x:Name="DialValue" Angle="0"/>
			</TransformGroup>
		</ContentPresenter.RenderTransform>
	</ContentPresenter>
</Grid>

It should appear as such:

10-templated-xaml-dialcontrol

Step 11

Then select from the Menu, Build, then Build Solution

10-build

Step 12

From the Solution Explorer select MainPage.xaml

10-mainpage

Step 13

Select from the Menu, View then Designer

10-designer

Step 14

The Design View will be displayed along with the XAML View and in this below <Grid Background=”{ThemeResource ApplicationPageBackgroundThemeBrush}”> enter the following XAML:

<local:Dial x:Name="Dial" Height="300" Width="300" Minimum="90.0" Maximum="270.0">
	<local:Dial.Knob>
		<Grid>
			<Ellipse Fill="{ThemeResource ApplicationSecondaryForegroundThemeBrush}"/>
			<Rectangle Height="40" Width="150" Margin="0,0,150,0" RadiusX="20" RadiusY="20" Fill="{ThemeResource ApplicationForegroundThemeBrush}"/>
		</Grid>
	</local:Dial.Knob>
</local:Dial>

It should appear as such:

10-xaml-dialcontrol

Step 15

That completes the Windows Universal Application so Save the Project then select the Debug and Simulator option to run the Application

10-simulator

Step 16

Once the Simulator has started the Application should then appear with the Dial displayed

10-simulator-run-dialcontrol

Step 17

To Exit the application select Stop Debugging in Visual Studio Community 2015

10-stop

Step 18

Another option is to run as a Windows Phone application, select Debug and select Emulator 10.0.1.0 WVGA 4 inch 512MB option to run the Application

10-emulator

Step 19

Once the Emulator has started the Application should then appear with the Dial displayed

10-emulator-run-dialcontrol

Step 20

To Exit the application select Stop Debugging in Visual Studio Community 2015

10-stop

Creative Commons License

Advertisements

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 )

Google+ photo

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

Connecting to %s