Windows 10 Universal Windows Platform – Draw Editor

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 a Name and select a Location to save to before selecting Ok to create the Project.

10-new-project

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

10-add-new-item-library

Step 6

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

using System;
using System.Collections.Generic;
using System.Globalization;
using System.Threading.Tasks;
using Windows.Foundation;
using Windows.Storage;
using Windows.Storage.Pickers;
using Windows.Storage.Provider;
using Windows.Storage.Streams;
using Windows.UI;
using Windows.UI.Core;
using Windows.UI.Input.Inking;
using Windows.UI.Popups;
using Windows.UI.Xaml.Controls;

public class Library
{
    private string colourToString(Color value)
    {
        return String.Format("{0:X2}{1:X2}{2:X2}{3:X2}",
            value.A, value.R, value.G, value.B);
    }

    private Color stringToColour(string value)
    {
        return Color.FromArgb(
        Byte.Parse(value.Substring(0, 2), NumberStyles.HexNumber),
        Byte.Parse(value.Substring(2, 2), NumberStyles.HexNumber),
        Byte.Parse(value.Substring(4, 2), NumberStyles.HexNumber),
        Byte.Parse(value.Substring(6, 2), NumberStyles.HexNumber));
    }

    public async Task<bool> Confirm(string content, string title, string ok, string cancel)
    {
        bool result = false;
        MessageDialog dialog = new MessageDialog(content, title);
        dialog.Commands.Add(new UICommand(ok, new UICommandInvokedHandler((cmd) => result = true)));
        dialog.Commands.Add(new UICommand(cancel, new UICommandInvokedHandler((cmd) => result = false)));
        await dialog.ShowAsync();
        return result;
    }

    public void Init(ref InkCanvas display, ref ComboBox size, ref ComboBox colour)
    {
        string selectedSize = ((ComboBoxItem)size.SelectedItem).Tag.ToString();
        string selectedColour = ((ComboBoxItem)colour.SelectedItem).Tag.ToString();
        InkDrawingAttributes attributes = new InkDrawingAttributes();
        attributes.Color = stringToColour(selectedColour);
        attributes.Size = new Size(int.Parse(selectedSize), int.Parse(selectedSize));
        attributes.IgnorePressure = false;
        attributes.FitToCurve = true;
        display.InkPresenter.UpdateDefaultDrawingAttributes(attributes);
        display.InkPresenter.InputDeviceTypes = 
           CoreInputDeviceTypes.Mouse | 
           CoreInputDeviceTypes.Pen | 
           CoreInputDeviceTypes.Touch;
    }

    public void Colour(ref InkCanvas display, ref ComboBox colour)
    {
        if (display != null)
        {
            string selectedColour = ((ComboBoxItem)colour.SelectedItem).Tag.ToString();
            InkDrawingAttributes attributes = display.InkPresenter.CopyDefaultDrawingAttributes();
            attributes.Color = stringToColour(selectedColour);
            display.InkPresenter.UpdateDefaultDrawingAttributes(attributes);
            
        }
    }

    public void Size(ref InkCanvas display, ref ComboBox size)
    {
        if (display != null)
        {
            string selectedSize = ((ComboBoxItem)size.SelectedItem).Tag.ToString();
            InkDrawingAttributes attributes = display.InkPresenter.CopyDefaultDrawingAttributes();
            attributes.Size = new Size(int.Parse(selectedSize), int.Parse(selectedSize));
            display.InkPresenter.UpdateDefaultDrawingAttributes(attributes);
        }
    }

    public async void New(InkCanvas display)
    {
        if (await Confirm("Create New Drawing?", "Draw Editor", "Yes", "No"))
        {
            display.InkPresenter.StrokeContainer.Clear();
        }
    }

    public async void Open(InkCanvas display)
    {
        try
        {
            FileOpenPicker picker = new FileOpenPicker();
            picker.SuggestedStartLocation = PickerLocationId.DocumentsLibrary;
            picker.FileTypeFilter.Add(".drw");
            StorageFile file = await picker.PickSingleFileAsync();
            using (IInputStream stream = await file.OpenSequentialReadAsync())
            {
                await display.InkPresenter.StrokeContainer.LoadAsync(stream);
            }
        }
        catch
        {

        }
    }

    public async void Save(InkCanvas display)
    {
        try
        {
            FileSavePicker picker = new FileSavePicker();
            picker.SuggestedStartLocation = PickerLocationId.DocumentsLibrary;
            picker.FileTypeChoices.Add("Drawing", new List<string>() { ".drw" });
            picker.DefaultFileExtension = ".drw";
            picker.SuggestedFileName = "Drawing";
            StorageFile file = await picker.PickSaveFileAsync();
            if (file != null)
            {
                CachedFileManager.DeferUpdates(file);
                using (IRandomAccessStream stream = await file.OpenAsync(FileAccessMode.ReadWrite))
                {
                    await display.InkPresenter.StrokeContainer.SaveAsync(stream);
                }
                FileUpdateStatus status = await CachedFileManager.CompleteUpdatesAsync(file);
            }
        }
        catch
        {

        }
    }
}

It should then appear as such:

10-library-draweditor

Step 7

From the Solution Explorer select MainPage.xaml

10-mainpage

Step 8

Select from the Menu, View then Designer

10-designer

Step 9

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

<Page.BottomAppBar>
	<AppBar IsOpen="True" IsSticky="True">
		<StackPanel Orientation="Horizontal">
			<AppBarButton Name="New" Icon="Page" Label="New" Click="New_Click"/>
			<AppBarButton Name="Open" Icon="Folder" Label="Open" Click="Open_Click"/>
			<AppBarButton Name="Save" Icon="Save" Label="Save" Click="Save_Click"/>
		</StackPanel>
	</AppBar>
</Page.BottomAppBar>

While still in the XAML View below <Grid Background=”{ThemeResource ApplicationPageBackgroundThemeBrush}”>enter the following XAML:

<Grid>
	<Grid.RowDefinitions>
		<RowDefinition Height="Auto"/>
		<RowDefinition Height="*"/>
	</Grid.RowDefinitions>
	<StackPanel Grid.Row="0" Orientation="Horizontal">
		<ComboBox Name="Size" VerticalAlignment="Center" Margin="20" SelectionChanged="Size_SelectionChanged">
			<ComboBoxItem Content="2" Tag="2"/>
			<ComboBoxItem Content="4" Tag="4"/>
			<ComboBoxItem Content="8" Tag="8"/>
			<ComboBoxItem Content="10" Tag="10" IsSelected="True"/>
			<ComboBoxItem Content="16" Tag="16"/>
			<ComboBoxItem Content="20" Tag="20"/>
			<ComboBoxItem Content="24" Tag="24"/>
			<ComboBoxItem Content="32" Tag="32"/>
			<ComboBoxItem Content="48" Tag="48"/>
			<ComboBoxItem Content="64" Tag="64"/>
		</ComboBox>
		<ComboBox Name="Colour" VerticalAlignment="Center" Margin="20" SelectionChanged="Colour_SelectionChanged">
			<ComboBoxItem Tag="FF000000" IsSelected="True">
				<Rectangle Width="25" Height="14" Fill="Black"/>
			</ComboBoxItem>
			<ComboBoxItem Tag="FF808080">
				<Rectangle Width="25" Height="14" Fill="Gray"/>
			</ComboBoxItem>
			<ComboBoxItem Tag="FFFF0000">
				<Rectangle Width="25" Height="14" Fill="Red"/>
			</ComboBoxItem>
			<ComboBoxItem Tag="FFFFA500">
				<Rectangle Width="25" Height="14" Fill="Orange"/>
			</ComboBoxItem>
			<ComboBoxItem Tag="FFFFFF00">
				<Rectangle Width="25" Height="14" Fill="Yellow"/>
			</ComboBoxItem>
			<ComboBoxItem Tag="FF008000">
				<Rectangle Width="25" Height="14" Fill="Green"/>
			</ComboBoxItem>
			<ComboBoxItem Tag="FF00FFFF">
				<Rectangle Width="25" Height="14" Fill="Cyan"/>
			</ComboBoxItem>
			<ComboBoxItem Tag="FF0000FF">
				<Rectangle Width="25" Height="14" Fill="Blue"/>
			</ComboBoxItem>
			<ComboBoxItem Tag="FFFF00FF">
				<Rectangle Width="25" Height="14" Fill="Magenta"/>
			</ComboBoxItem>
			<ComboBoxItem Tag="FF800080">
				<Rectangle Width="25" Height="14" Fill="Purple"/>
			</ComboBoxItem>
		</ComboBox>
	</StackPanel>
	<Grid Grid.Row="1" Background="White">
		<InkCanvas Name="Display"/>
	</Grid>         
</Grid>

It should appear as such:

10-xaml-draweditor

Step 10

Select from the Menu, View then Code

10-code

Step 11

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

public Library Library = new Library();

protected override void OnNavigatedTo(NavigationEventArgs e)
{
	Library.Init(ref Display, ref Size, ref Colour);
}

private void New_Click(object sender, RoutedEventArgs e)
{
	Library.New(Display);
}

private void Open_Click(object sender, RoutedEventArgs e)
{
	Library.Open(Display);
}

private void Save_Click(object sender, RoutedEventArgs e)
{
	Library.Save(Display);
}

private void Colour_SelectionChanged(object sender, SelectionChangedEventArgs e)
{
	Library.Colour(ref Display, ref Colour);
}

private void Size_SelectionChanged(object sender, SelectionChangedEventArgs e)
{
	Library.Size(ref Display, ref Size);
}

It should then appear as such:

10-code-draweditor

Step 12

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

10-simulator

Step 13

Once the Simulator has started the Application should then appear

10-simulator-run-draweditor

Step 14

After the Application has started running you can then select a draw size and colour and start drawing then you can Save the drawing to Open later or start a New drawing

10-simulator-ran-draweditor

Step 15

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

10-stop

Step 16

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 17

Once the Emulator has started the Application should then appear

10-emulator-run-draweditor

Step 18

After the Application has started running you can then select a draw size and colour and start drawing then you can Save the drawing to Open later or start a New drawing

10-emulator-ran-draweditor

Step 19

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

10-stop

Creative Commons License

Advertisements

One thought on “Windows 10 Universal Windows Platform – Draw Editor

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