Universal Windows Platform – Ruler Control

Ruler Control demonstrates how to create an on-screen Ruler or Rule featuring Metric Cm and Imperial Inch markings

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 the Name as RulerControl and select a Location and then select Ok to create the Project
vs2017-new-project-ruler-control

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.

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 Code from Installed then select Code File from the list, then type in the Name as Ruler.cs before selecting Add to add the file to the Project

vs2017-add-new-item-ruler-control

Step 7

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

using Windows.Foundation;
using Windows.UI;
using Windows.UI.Xaml;
using Windows.UI.Xaml.Controls;
using Windows.UI.Xaml.Media;
using Windows.UI.Xaml.Shapes;

namespace RulerControl
{
    public class Ruler : Canvas
    {
        private const double height = 40.0;

        public enum Units { Cm, Inch };

        public static readonly DependencyProperty ForegroundProperty =
        DependencyProperty.Register("Foreground", typeof(Brush),
        typeof(Ruler), new PropertyMetadata(new SolidColorBrush(Colors.Black)));

        public static readonly DependencyProperty LengthProperty =
        DependencyProperty.Register("Length", typeof(double),
        typeof(Ruler), new PropertyMetadata(10.0));

        public static readonly DependencyProperty SegmentProperty =
        DependencyProperty.Register("Segment", typeof(double),
        typeof(Ruler), new PropertyMetadata(20.0));

        public static readonly DependencyProperty UnitProperty =
        DependencyProperty.Register("Unit", typeof(double),
        typeof(Ruler), new PropertyMetadata(Units.Cm));

        public Brush Foreground
        {
            get { return (Brush)GetValue(ForegroundProperty); }
            set { SetValue(ForegroundProperty, value); }
        }

        public double Length
        {
            get { return (double)GetValue(LengthProperty); }
            set { SetValue(LengthProperty, value); }
        }

        public double Segment
        {
            get { return (double)GetValue(SegmentProperty); }
            set { SetValue(SegmentProperty, value); }
        }

        public Units Unit
        {
            get { return (Units)GetValue(UnitProperty); }
            set { SetValue(UnitProperty, value); }
        }

        private double CmToDip(double cm) => (cm * 96.0 / 2.54);

        private double InchToDip(double inch) => (inch * 96.0);

        private Path GetLine(Brush stroke, double thickness, Point start, Point finish)
        {
            return new Path()
            {
                Stroke = stroke,
                StrokeThickness = thickness,
                Data = new LineGeometry() { StartPoint = start, EndPoint = finish }
            };
        }

        private void Layout()
        {
            this.Children.Clear();
            for (double value = 0.0; value <= Length; value++)
            {
                double dip;
                if (Unit == Units.Cm)
                {
                    dip = CmToDip(value);
                    if (value < Length)
                    {
                        for (int i = 1; i <= 9; i++)
                        {
                            if (i != 5)
                            {
                                double mm = CmToDip(value + 0.1 * i);
                                this.Children.Add(GetLine(Foreground, 0.5, new Point(mm, this.Height),
                                new Point(mm, this.Height - Segment / 3.0)));
                            }
                        }
                        double middle = CmToDip(value + 0.5);
                        this.Children.Add(GetLine(Foreground, 1.0, new Point(middle, this.Height),
                        new Point(middle, this.Height - Segment * 2.0 / 3.0)));
                    }
                }
                else
                {
                    dip = InchToDip(value);
                    if (value < Length)
                    {
                        double quarter = InchToDip(value + 0.25);
                        this.Children.Add(GetLine(Foreground, 0.5, new Point(quarter, this.Height),
                        new Point(quarter, this.Height - Segment / 3.0)));
                        double middle = InchToDip(value + 0.5);
                        this.Children.Add(GetLine(Foreground, 1.0, new Point(middle, this.Height),
                        new Point(middle, this.Height - 0.5 * Segment * 2.0 / 3.0)));
                        double division = InchToDip(value + 0.75);
                        this.Children.Add(GetLine(Foreground, 0.5, new Point(division, this.Height),
                        new Point(division, this.Height - 0.25 * Segment / 3.0)));
                    }
                }
                this.Children.Add(GetLine(Foreground, 1.0, new Point(dip, this.Height),
                new Point(dip, this.Height - Segment)));
            }
        }

        public Ruler()
        {
            this.Loaded += (object sender, RoutedEventArgs e) => Layout();
        }

        protected override Size MeasureOverride(Size availableSize)
        {
            this.Height = double.IsNaN(this.Height) ? height : this.Height;
            Size desiredSize = (Unit == Units.Cm) ? new Size(CmToDip(Length), 
            this.Height) : new Size(InchToDip(Length), this.Height);
            return desiredSize;
        }
    }
}

The Ruler Class itself which inherits from a Canvas and there are using statements to include the necessary functionality. There is a const for height and an enum for the Units. There are DependencyProperty and Properties for Foreground, Length, Segment and Unit.

Also in the Ruler Class there are Expression Bodied Methods for CmToDip and InchToDip to convert to and from Device Independent Pixels to either Cenntimetres or Inches. There is a GetLine Method which will create a Path with a given LineGeometry, Stroke and StrokeThickness.

Still in the Ruler Class there is a Layout Method which is used to create the look-and-feel of the Control for each type of Units selected to use GetLine to populate the Markings. The Ruler Constructor is used to bind Layout to the Loaded Event of the Control and MeasureOverride Method is used to set the Size of the Control accordingly.

Step 8

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

vs2017-build-build-solution

Step 9

In the Solution Explorer select MainPage.xaml

vs2017-mainpage-ruler-control

Step 10

From the Menu choose View and then Designer

vs2017-view-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:

<local:Ruler Margin="50" HorizontalAlignment="Center" Length="15.0" 
Background="{ThemeResource SystemControlHighlightAccentBrush}" 
Foreground="{ThemeResource SystemControlBackgroundAltHighBrush}"/>

The MainPage has the Ruler Control itself with the Length, Background and Foreground set accordingly.

Step 12

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 13

After the Application has started running you can then see the Ruler Control with the given Length, Background and Foreground.

ran-ruler-control

Step 14

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

vs2017-close

This control shows how to create a control that emulates the look-and-feel of a physical Ruler, or Rule used to measure length. This example is based upon the Ruler in the Perspective.Wpf Control set by technodesigner on GitHub.

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 )

Google+ photo

You are commenting using your Google+ 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