Touch Support Example for Blazor

Nevron NOV UserInterface for Blazor ships with hundreds of examples with full source code, as part of the Nevron Open Vision Suite. All Blazor UserInterface examples are also available as a stand-alone Visual Studio solution in C#, which you can download from the links below.

Touch Support Example for Blazor Description

Demonstrates the core touch support in NOV. Use your fingers to draw on the canvas and explore the touch events that NOV sends to the application. Note that touch input is only available to touch enabled enviroments.


Touch Support Example

C# Touch Support Example Code for Nevron UserInterface for Blazor
Language: C#

using Nevron.Nov;
using Nevron.Nov.DataStructures;
using Nevron.Nov.Dom;
using Nevron.Nov.Graphics;
using Nevron.Nov.Layout;
using Nevron.Nov.UI;
using Nevron.Nov.Windows.Forms;
using System;
using System.Windows.Forms;

namespace NovWinFormsProject
{
	public partial class Form1 : Form
	{
		#region Constructors

		public Form1()
		{
			InitializeComponent();

			// Set form title
			Text = "NOV WinForms - NTouchSupportExample";

			// Create the NOV example and its controls
			NWidget exampleContent = CreateExampleContent();
			NWidget exampleControls = CreateExampleControls();
			if (exampleControls != null)
			{
				exampleControls.PreferredWidth = 300;
			}

			// Place the example and its controls in a pair box
			NPairBox pairBox = new NPairBox(exampleContent, exampleControls);
			pairBox.FillMode = ENStackFillMode.First;
			pairBox.FitMode = ENStackFitMode.First;
			pairBox.Margins = new NMargins(NDesign.HorizontalSpacing, NDesign.VerticalSpacing);

			// Host the example in the form
			NNovWidgetHost host = new NNovWidgetHost(pairBox);
			host.Dock = DockStyle.Fill;
			Controls.Add(host);
		}

		#endregion

        #region Example

        public NWidget CreateExampleContent()
        {
            m_Canvas = new NCanvas();
            m_Canvas.PrePaint += OnCanvasPrePaint;

            // subscribe for touch events
            m_Canvas.TouchDown += OnCanvasTouchDown;
            m_Canvas.TouchMove += OnCanvasTouchMove;
            m_Canvas.TouchUp += OnCanvasTouchUp;

            // subscribe for mouse events
            m_Canvas.MouseDown += OnCanvasMouseDown;
            m_Canvas.MouseUp += OnCanvasMouseUp;
            m_Canvas.MouseMove += OnCanvasMouseMove;

            return m_Canvas;
        }
        public NWidget CreateExampleControls()
        {
            NStackPanel stack = new NStackPanel();
            stack.FitMode = ENStackFitMode.Last;
            stack.FillMode = ENStackFillMode.Last;

            // capture touch check
            m_CaptureTouchCheck = new NCheckBox("Capture Touch");
            stack.Add(m_CaptureTouchCheck);

            // handle touch events check
            m_HandleTouchEventsCheck = new NCheckBox("Handle Touch Events");
            stack.Add(m_HandleTouchEventsCheck);

            // track move events
            m_LogMoveEventsCheck = new NCheckBox("Track Move Events");
            stack.Add(m_LogMoveEventsCheck);

            // Create clear canvas button
            NButton clearCanvasButton = new NButton("Clear Canvas");
            clearCanvasButton.Click += clearCanvas_Click;
            stack.Add(clearCanvasButton);

            // Create the events log
            m_EventsLog = new NExampleEventsLog();
            stack.Add(m_EventsLog);

            return stack;
        }
        public string GetExampleDescription()
        {
            return @"

Demonstrates the core touch support in NOV. Use your fingers to draw on the canvas and explore the touch events that NOV sends to the application. Note that touch input is only available to touch enabled enviroments.

"; } /// /// Gets the relevant documentation links for the example /// /// public (string Name, string Url)[] GetDocumentationUrls() { return new[] { ("Touch Input", "https://www.nevron.com/docs/openvision/ui-touch-input.html"), ("User Input", "https://www.nevron.com/docs/openvision/ui-user-input.html"), }; } #endregion #region Implementation #endregion #region Event Handlers void OnCanvasPrePaint(NPaintEventArgs arg) { for (int i = 0; i < m_TouchPoints.Count; i++) { m_TouchPoints[i].Paint(arg.PaintVisitor); } } void OnCanvasTouchUp(NTouchActionEventArgs arg) { AddTouchPoint(arg); m_EventsLog.LogEvent("Touch Up"); if (m_HandleTouchEventsCheck.Checked) { arg.Cancel = true; } } void OnCanvasTouchMove(NTouchActionEventArgs arg) { AddTouchPoint(arg); if (m_LogMoveEventsCheck.Checked) { m_EventsLog.LogEvent("Touch Move"); } if (m_HandleTouchEventsCheck.Checked) { arg.Cancel = true; } } void OnCanvasTouchDown(NTouchActionEventArgs arg) { AddTouchPoint(arg); m_EventsLog.LogEvent("Touch Down"); if (m_CaptureTouchCheck.Checked) { m_Canvas.CaptureTouch(arg.Device); m_EventsLog.LogEvent("Captured"); } if (m_HandleTouchEventsCheck.Checked) { arg.Cancel = true; } } void OnCanvasMouseMove(NMouseEventArgs arg) { if (m_LogMoveEventsCheck.Checked) { m_EventsLog.LogEvent("Mouse Move"); } } void OnCanvasMouseUp(NMouseButtonEventArgs arg) { m_EventsLog.LogEvent("Mouse Up"); } void OnCanvasMouseDown(NMouseButtonEventArgs arg) { m_EventsLog.LogEvent("Mouse Down"); } void AddTouchPoint(NTouchActionEventArgs arg) { m_TouchPoints.Add(new NTouchPoint(arg.TargetPosition, arg.ScreenSize, arg.DeviceState)); m_Canvas.InvalidateDisplay(); } void clearCanvas_Click(NEventArgs arg) { m_TouchPoints.Clear(); m_Canvas.InvalidateDisplay(); } #endregion #region Fields NCheckBox m_CaptureTouchCheck; NCheckBox m_HandleTouchEventsCheck; NCheckBox m_LogMoveEventsCheck; NCanvas m_Canvas; NExampleEventsLog m_EventsLog; NList m_TouchPoints = new NList(); #endregion #region Static Methods #endregion #region Constants private const string EnglishLanguageName = "English (US)"; private const string BulgarianLanguageName = "Bulgarian"; private const string GermanLanguageName = "German"; #endregion #region Nested Types #endregion } }

Download a fully functional C# (.cs) Touch Support Example for Blazor Solution

Related Documentation Links