Universal Windows Platform – Stick Control

Stick Control demonstrates how to create an on-screen Joystick Control and it will also indicate the Angle and Ratio that has been selected.

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.


Step 2

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


Step 3

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

Step 4

Then in New Universal Windows Project you need to select the Target Version this should be at least the Windows 10, version 1803 (10.0; Build 17134) which is the April 2018 Update and the Minimum Version to be the same.


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…


Step 6

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


Step 7

Once in the Code View for Stick.cs the following should be entered:

using System;
using Windows.UI;
using Windows.UI.Xaml;
using Windows.UI.Xaml.Controls;
using Windows.UI.Xaml.Data;
using Windows.UI.Xaml.Input;
using Windows.UI.Xaml.Media;
using Windows.UI.Xaml.Shapes;

namespace StickControl
    public class Stick : Grid
        private bool _capture;
        private Ellipse _knob;
        private Ellipse _face;
        private double x = 0;
        private double y = 0;
        private double _m = 0;
        private double _res = 0;
        private double _width = 0;
        private double _height = 0;
        private double _alpha = 0;
        private double _alphaM = 0;
        private double _centreX = 0;
        private double _centreY = 0;
        private double _distance = 0;
        private double _oldAlphaM = -999.0;
        private double _oldDistance = -999.0;

        public delegate void ValueChangedEventHandler(object sender, double angle, double ratio);
        public event ValueChangedEventHandler ValueChanged;

        private void Middle()
            _capture = false;
            Canvas.SetLeft(_knob, (this.Width - _width) / 2);
            Canvas.SetTop(_knob, (this.Height - _height) / 2);
            _centreX = this.Width / 2;
            _centreY = this.Height / 2;

        private void Move(PointerRoutedEventArgs e)
            double ToRadians(double angle)
                return Math.PI * angle / 180.0;

            double ToDegrees(double angle)
                return angle * (180.0 / Math.PI);

            x = e.GetCurrentPoint(this).Position.X;
            y = e.GetCurrentPoint(this).Position.Y;
            _res = Math.Sqrt((x - _centreX) *
            (x - _centreX) + (y - _centreY) * (y - _centreY));
            _m = (y - _centreY) / (x - _centreX);
            _alpha = ToDegrees(Math.Atan(_m) + Math.PI / 2);
            if (x < _centreX)
                _alpha += 180.0;
            else if (x == _centreX && y <= _centreY)
                _alpha = 0.0;
            else if (x == _centreX)
                _alpha = 180.0;
            if (_res > Radius)
                x = _centreX + Math.Cos(ToRadians(_alpha) - Math.PI / 2) * Radius;
                y = _centreY + Math.Sin(ToRadians(_alpha) - Math.PI / 2) * Radius 
                * ((_alpha % 180.0 == 0.0) ? -1.0 : 1.0);
                _res = Radius;
            if (Math.Abs(_alpha - _alphaM) >= Sensitivity ||
                Math.Abs(_distance * Radius - _res) >= Sensitivity)
                _alphaM = _alpha;
                _distance = _res / Radius;
            if (_oldAlphaM != _alphaM ||
                _oldDistance != _distance)
                Angle = _alphaM;
                Ratio = _distance;
                _oldAlphaM = _alphaM;
                _oldDistance = _distance;
                ValueChanged?.Invoke(this, Angle, Ratio);
            Canvas.SetLeft(_knob, x - _width / 2);
            Canvas.SetTop(_knob, y - _height / 2);

        private Ellipse GetCircle(double dimension, string path)
            Ellipse circle = new Ellipse()
                Height = dimension,
                Width = dimension
            circle.SetBinding(Shape.FillProperty, new Binding()
                Source = this,
                Path = new PropertyPath(path),
                Mode = BindingMode.TwoWay
            return circle;

        private void Init()
            _knob = GetCircle(Radius / 2, "Knob");
            _face = GetCircle(Radius * 2, "Face");
            _height = _knob.ActualHeight;
            _width = _knob.ActualWidth;
            this.Width = _width + Radius * 2;
            this.Height = _height + Radius * 2;
            this.PointerExited -= null;
            this.PointerExited += (object sender, PointerRoutedEventArgs e) =>
            _knob.PointerReleased += (object sender, PointerRoutedEventArgs e) =>
            _knob.PointerPressed += (object sender, PointerRoutedEventArgs e) =>
                _capture = true;
            _knob.PointerMoved += (object sender, PointerRoutedEventArgs e) =>
                if (_capture) Move(e);
            _knob.PointerExited += (object sender, PointerRoutedEventArgs e) =>

        private void Layout()
            Canvas canvas = new Canvas()
                Width = this.Width,
                Height = this.Height

        public static readonly DependencyProperty RadiusProperty =
        DependencyProperty.Register("Radius", typeof(int),
        typeof(Stick), new PropertyMetadata(100));

        public static readonly DependencyProperty KnobProperty =
        DependencyProperty.Register("Knob", typeof(Brush),
        typeof(Stick), new PropertyMetadata(new SolidColorBrush(Colors.Red)));

        public static readonly DependencyProperty FaceProperty =
        DependencyProperty.Register("Face", typeof(Brush),
        typeof(Stick), new PropertyMetadata(new SolidColorBrush(Colors.Black)));

        public static readonly DependencyProperty AngleProperty =
        DependencyProperty.Register("Angle", typeof(double),
        typeof(Stick), null);

        public static readonly DependencyProperty RatioProperty =
        DependencyProperty.Register("Ratio", typeof(double),
        typeof(Stick), null);

        public static readonly DependencyProperty SensitivityProperty =
        DependencyProperty.Register("Sensitivity", typeof(double),
        typeof(Stick), null);

        public int Radius
            get { return (int)GetValue(RadiusProperty); }
            set { SetValue(RadiusProperty, value); Layout(); }

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

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

        public double Angle
            get { return (double)GetValue(AngleProperty); }
            set { SetValue(AngleProperty, value); }

        public double Ratio
            get { return (double)GetValue(RatioProperty); }
            set { SetValue(RatioProperty, value); }

        public double Sensitivity
            get { return (double)GetValue(SensitivityProperty); }
            set { SetValue(SensitivityProperty, value); }

        public Stick() => Layout();

The Stick Class itself which inherits from a Grid and there are using statements to include the necessary functionality. There are Members for bool, Ellipse and double for various Values for the Control. There is a ValueChangedEventHandler which is used for the event for ValueChanged.

Also in the Stick Class there is a Middle Method which will set the _knob Element to be in the centre of a Canvas. The Move Method has two Local Functions for ToRadians and ToDegrees it uses GetCurrentPoint to determine where is being selected within the Control then using various Values works out the values needed for the Angle and Ratio Properties as well as positioning the _knob Element accordingly based up where it has been moved to. The GetCircle Method is used to return an Ellipse and also use SetBinding to Data Bind the Fill Property of the Ellipse to the named PropertyPath. The Init Method is used to help create elements that make up the Control plus initialise the Event Handlers and the Layout Method helps along with Init to create the look-and-feel of the Control.

Still in the Stick Class there are DependencyProperty for Radius, Knob and Face which set the size and Brush used to style the Control and Angle and Ratio are values returned when its being used with Sensitivity allowing that to be adjusted. There is an Expression Value call in the Constructor for the Stick Class that calls Layout.

Step 8

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


Step 9

In the Solution Explorer select MainPage.xaml


Step 10

From the Menu choose View and then Designer


Step 11

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

<StackPanel VerticalAlignment="Center" HorizontalAlignment="Center">
	<TextBlock Name="Label" HorizontalAlignment="Center" Style="{StaticResource SubtitleTextBlockStyle}"/>
	<local:Stick Radius="200" Knob="{ThemeResource AccentButtonBackground}" Face="WhiteSmoke" ValueChanged="Stick_ValueChanged"/>

The MainPage has a StackPanel with a TextBlock to indicate the Angle and Ratio selected from the Stick Control itself with the ValueChanged Event Handler set.

Step 12

From the Menu choose View and then Code


Step 13

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

private void Stick_ValueChanged(object sender, double angle, double ratio)
	Label.Text = $"Angle {angle}, Ratio {ratio}";

Below the MainPage() Method there is the Stick_ValueChanged Event Handler which will set the TextBlock to display the Angle and Ratio selected by the Stick Control.

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


Step 15

After the Application has started running you can then select and move the Knob of the Stick Control around the Face which will register an Angle around the Middle and Ratio from the Middle to edge of the Face in the ValueChanged Event.


Step 16

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


This control shows how to create a control that emulates the behaviour of a physical Joystick – that is when you tap-and-hold with Touch or press-and-hold with Mouse on the Knob then move it any Direction it will register the Angle around the Middle and the Ratio between the Middle and end of the Face of the Stick Control. Although this example is simple and just displays display text in a Label to show the Angle or Ratio, it could form the basis of a controller for a game.

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