Mouse Events Example for WinForms

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

Mouse Events Example for WinForms Description

Demonstrates the mouse support in NOV. Click and move the mouse over the canvas to explore the mouse events that NOV sends to the application. Mouse support is available to most desktop enviroments.


Mouse Events Example

C# Mouse Events Example Code for Nevron UserInterface for WinForms
Language: C#

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

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

		public Form1()
		{
			InitializeComponent();

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

			// 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 mouse events
            m_Canvas.MouseDown += OnCanvasMouseDown;
            m_Canvas.MouseUp += OnCanvasMouseUp;
            m_Canvas.MouseMove += OnCanvasMouseMove;
            m_Canvas.MouseWheel += OnCanvasMouseWheel;
            m_Canvas.MouseIn += OnCanvasMouseIn;
            m_Canvas.MouseOut += OnCanvasMouseOut;
            m_Canvas.GotMouseCapture += OnCanvasGotMouseCapture;
            m_Canvas.LostMouseCapture += OnCanvasLostMouseCapture;

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

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

            // capture mouse on left mouse down
            m_CaptureMouseOnLeftMouseDown = new NCheckBox("Capture Mouse on Left MouseDown");
            stack.Add(m_CaptureMouseOnLeftMouseDown);

            // capture mouse on right mouse down
            m_CaptureMouseOnRightMouseDown = new NCheckBox("Capture Mouse on Right MouseDown");
            stack.Add(m_CaptureMouseOnRightMouseDown);

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

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

Demonstrates the mouse support in NOV. Click and move the mouse over the canvas to explore the mouse events that NOV sends to the application. Mouse support is available to most desktop enviroments.

"; } /// /// Gets the relevant documentation links for the example /// /// public (string Name, string Url)[] GetDocumentationUrls() { return new[] { ("Mouse Input", "https://www.nevron.com/docs/openvision/ui-mouse-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) { NPaintVisitor visitor = arg.PaintVisitor; // paint background visitor.SetFill(NColor.Ivory); visitor.PaintRectangle(m_Canvas.GetContentEdge()); // paint mouse if (NMouse.IsOver(m_Canvas)) { // define some metrics first double buttonWidth = 10; double buttonHeight = 15; double buttonsOffset = 5; NSize pointerSize = new NSize(5, 5); NPoint buttonsCenter = new NPoint(m_MouseLocation.X, m_MouseLocation.Y + buttonsOffset + buttonHeight / 2); NRectangle buttonsFrame = NRectangle.FromCenterAndSize(buttonsCenter, buttonWidth * 3, buttonHeight); // paint left button, if down if (m_LeftMouseDown) { NRectangle buttonRect = new NRectangle(buttonsFrame.Left, buttonsFrame.Top, buttonWidth, buttonHeight); visitor.SetFill(NColor.Red); visitor.PaintRectangle(buttonRect); } // paint middle button, if down if (m_MiddleMouseDown) { NRectangle buttonRect = new NRectangle(buttonsFrame.Left + buttonWidth, buttonsFrame.Top, buttonWidth, buttonHeight); visitor.SetFill(NColor.Green); visitor.PaintRectangle(buttonRect); } // paint right button, if down if (m_RightMouseDown) { NRectangle buttonRect = new NRectangle(buttonsFrame.Right - buttonWidth, buttonsFrame.Top, buttonWidth, buttonHeight); visitor.SetFill(NColor.Blue); visitor.PaintRectangle(buttonRect); } // paint mouse pointer and buttons frame visitor.ClearStyles(); visitor.SetStroke(new NStroke(NColor.Black)); visitor.PaintEllipse(NRectangle.FromCenterAndSize(m_MouseLocation, pointerSize)); visitor.PaintRectangle(buttonsFrame); double leftSeparator = buttonsCenter.X - buttonWidth / 2; visitor.PaintLine(leftSeparator, buttonsFrame.Top, leftSeparator, buttonsFrame.Bottom); double rightSeparator = buttonsCenter.X + buttonWidth / 2; visitor.PaintLine(rightSeparator, buttonsFrame.Top, rightSeparator, buttonsFrame.Bottom); } // paint capture frame if (NMouse.IsCaptured(m_Canvas)) { NRectangle outerRect = m_Canvas.GetContentEdge(); NRectangle innerRect = outerRect; innerRect.Deflate(3); NGraphicsPath path = new NGraphicsPath(); path.AddRectangle(outerRect); path.AddRectangle(innerRect); visitor.ClearStyles(); visitor.SetFill(NColor.Red); visitor.PaintPath(path); } } void OnCanvasMouseMove(NMouseEventArgs arg) { // log event if (m_LogMoveEventsCheck.Checked) { m_EventsLog.LogEvent(String.Format("Mouse Move. Position X: {0}, Y; {1}", m_MouseLocation.X, m_MouseLocation.Y)); } // update mouse location m_MouseLocation = arg.CurrentTargetPosition; m_Canvas.InvalidateDisplay(); } void OnCanvasMouseUp(NMouseButtonEventArgs arg) { m_EventsLog.LogEvent("Mouse Up. Button: " + arg.Button); switch (arg.Button) { case ENMouseButtons.Left: m_LeftMouseDown = false; break; case ENMouseButtons.Right: m_RightMouseDown = false; break; case ENMouseButtons.Middle: m_MiddleMouseDown = false; break; default: break; } m_Canvas.InvalidateDisplay(); } void OnCanvasMouseDown(NMouseButtonEventArgs arg) { m_EventsLog.LogEvent("Mouse Down. Button: " + arg.Button); switch (arg.Button) { case ENMouseButtons.Left: m_LeftMouseDown = true; if (m_CaptureMouseOnLeftMouseDown.Checked) { m_Canvas.CaptureMouse(); } break; case ENMouseButtons.Right: m_RightMouseDown = true; if (m_CaptureMouseOnRightMouseDown.Checked) { m_Canvas.CaptureMouse(); } break; case ENMouseButtons.Middle: m_MiddleMouseDown = true; break; default: break; } m_Canvas.InvalidateDisplay(); } void OnCanvasMouseWheel(NMouseWheelEventArgs arg) { m_EventsLog.LogEvent("Mouse Wheel. Delta: " + arg.Delta); } void OnCanvasMouseOut(NMouseOverChangeEventArgs arg) { m_EventsLog.LogEvent("Mouse Out"); m_Canvas.InvalidateDisplay(); } void OnCanvasMouseIn(NMouseOverChangeEventArgs arg) { m_EventsLog.LogEvent("Mouse In"); m_Canvas.InvalidateDisplay(); } void OnCanvasGotMouseCapture(NMouseCaptureChangeEventArgs arg) { m_EventsLog.LogEvent("Got Mouse Capture"); m_Canvas.InvalidateDisplay(); } void OnCanvasLostMouseCapture(NMouseCaptureChangeEventArgs arg) { m_EventsLog.LogEvent("Lost Mouse Capture"); m_Canvas.InvalidateDisplay(); } #endregion #region Fields NCheckBox m_LogMoveEventsCheck; NCheckBox m_CaptureMouseOnLeftMouseDown; NCheckBox m_CaptureMouseOnRightMouseDown; NCanvas m_Canvas; NExampleEventsLog m_EventsLog; NPoint m_MouseLocation; bool m_LeftMouseDown; bool m_MiddleMouseDown; bool m_RightMouseDown; #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) Mouse Events Example for WinForms Solution

VB.NET Mouse Events Example Code for Nevron UserInterface for WinForms
Language: VB.NET

Imports Nevron.Nov
Imports Nevron.Nov.Dom
Imports Nevron.Nov.Graphics
Imports Nevron.Nov.Layout
Imports Nevron.Nov.UI
Imports Nevron.Nov.Windows.Forms

Public Class Form1

#Region "Constructors"

    Public Sub New()
        ' This call is required by the designer.
        InitializeComponent()

        ' Add any initialization after the InitializeComponent() call.

        ' Set form title
        Text = "NOV WinForms - NMouseEventsExample"

        ' Create the NOV example and its controls
        Dim exampleContent As NWidget = CreateExampleContent()
        Dim exampleControls As NWidget = CreateExampleControls()
        If Not exampleControls Is Nothing Then
            exampleControls.PreferredWidth = 300
        End If

        ' Place the example and its controls in a pair box
        Dim pairBox As NPairBox = 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
        Dim host As NNovWidgetHost(Of NWidget) = New NNovWidgetHost(Of NWidget)(pairBox)
        host.Dock = DockStyle.Fill
        Controls.Add(host)
    End Sub


#End Region

#Region "Example"

        Public Function CreateExampleContent() As NWidget
            m_Canvas = New NCanvas()
            Me.m_Canvas.PrePaint += AddressOf OnCanvasPrePaint

            ' subscribe for mouse events
            Me.m_Canvas.MouseDown += AddressOf OnCanvasMouseDown
            Me.m_Canvas.MouseUp += AddressOf OnCanvasMouseUp
            Me.m_Canvas.MouseMove += AddressOf OnCanvasMouseMove
            Me.m_Canvas.MouseWheel += AddressOf OnCanvasMouseWheel
            Me.m_Canvas.MouseIn += AddressOf OnCanvasMouseIn
            Me.m_Canvas.MouseOut += AddressOf OnCanvasMouseOut
            Me.m_Canvas.GotMouseCapture += AddressOf OnCanvasGotMouseCapture
            Me.m_Canvas.LostMouseCapture += AddressOf OnCanvasLostMouseCapture

            Return m_Canvas
        End Function
        Public Function CreateExampleControls() As NWidget
            Dim stack As NStackPanel = New NStackPanel()
            stack.FitMode = ENStackFitMode.Last
            stack.FillMode = ENStackFillMode.Last

            ' track move events
            m_LogMoveEventsCheck = New NCheckBox("Track Move Events")
            stack.Add(m_LogMoveEventsCheck)

            ' capture mouse on left mouse down
            m_CaptureMouseOnLeftMouseDown = New NCheckBox("Capture Mouse on Left MouseDown")
            stack.Add(m_CaptureMouseOnLeftMouseDown)

            ' capture mouse on right mouse down
            m_CaptureMouseOnRightMouseDown = New NCheckBox("Capture Mouse on Right MouseDown")
            stack.Add(m_CaptureMouseOnRightMouseDown)

            ' Create the events log
            m_EventsLog = New NExampleEventsLog()
            stack.Add(m_EventsLog)

            Return stack
        End Function
        Public Function GetExampleDescription() As String
            Return "

Demonstrates the mouse support in NOV. Click and move the mouse over the canvas to explore the mouse events that NOV sends to the application. Mouse support is available to most desktop enviroments.

" End Function ''' ''' Gets the relevant documentation links for the example ''' ''' Public Function GetDocumentationUrls() As (String, String)() Return {("Mouse Input", "https://www.nevron.com/docs/openvision/ui-mouse-input.html"), ("User Input", "https://www.nevron.com/docs/openvision/ui-user-input.html")} End Function #End Region #Region "Implementation" #End Region #Region "Event Handlers" Private Sub OnCanvasPrePaint(arg As NPaintEventArgs) Dim visitor = arg.PaintVisitor ' paint background visitor.SetFill(NColor.Ivory) visitor.PaintRectangle(m_Canvas.GetContentEdge()) ' paint mouse If NMouse.IsOver(m_Canvas) Then ' define some metrics first Dim buttonWidth As Double = 10 Dim buttonHeight As Double = 15 Dim buttonsOffset As Double = 5 Dim pointerSize As NSize = New NSize(5, 5) Dim buttonsCenter As NPoint = New NPoint(m_MouseLocation.X, m_MouseLocation.Y + buttonsOffset + buttonHeight / 2) Dim buttonsFrame = NRectangle.FromCenterAndSize(buttonsCenter, buttonWidth * 3, buttonHeight) ' paint left button, if down If m_LeftMouseDown Then Dim buttonRect As NRectangle = New NRectangle(buttonsFrame.Left, buttonsFrame.Top, buttonWidth, buttonHeight) visitor.SetFill(NColor.Red) visitor.PaintRectangle(buttonRect) End If ' paint middle button, if down If m_MiddleMouseDown Then Dim buttonRect As NRectangle = New NRectangle(buttonsFrame.Left + buttonWidth, buttonsFrame.Top, buttonWidth, buttonHeight) visitor.SetFill(NColor.Green) visitor.PaintRectangle(buttonRect) End If ' paint right button, if down If m_RightMouseDown Then Dim buttonRect As NRectangle = New NRectangle(buttonsFrame.Right - buttonWidth, buttonsFrame.Top, buttonWidth, buttonHeight) visitor.SetFill(NColor.Blue) visitor.PaintRectangle(buttonRect) End If ' paint mouse pointer and buttons frame visitor.ClearStyles() visitor.SetStroke(New NStroke(NColor.Black)) visitor.PaintEllipse(NRectangle.FromCenterAndSize(m_MouseLocation, pointerSize)) visitor.PaintRectangle(buttonsFrame) Dim leftSeparator = buttonsCenter.X - buttonWidth / 2 visitor.PaintLine(leftSeparator, buttonsFrame.Top, leftSeparator, buttonsFrame.Bottom) Dim rightSeparator = buttonsCenter.X + buttonWidth / 2 visitor.PaintLine(rightSeparator, buttonsFrame.Top, rightSeparator, buttonsFrame.Bottom) End If ' paint capture frame If NMouse.IsCaptured(m_Canvas) Then Dim outerRect As NRectangle = m_Canvas.GetContentEdge() Dim innerRect = outerRect innerRect.Deflate(3) Dim path As NGraphicsPath = New NGraphicsPath() path.AddRectangle(outerRect) path.AddRectangle(innerRect) visitor.ClearStyles() visitor.SetFill(NColor.Red) visitor.PaintPath(path) End If End Sub Private Sub OnCanvasMouseMove(arg As NMouseEventArgs) ' log event If m_LogMoveEventsCheck.Checked Then m_EventsLog.LogEvent(String.Format("Mouse Move. Position X: {0}, Y; {1}", m_MouseLocation.X, m_MouseLocation.Y)) End If ' update mouse location m_MouseLocation = arg.CurrentTargetPosition m_Canvas.InvalidateDisplay() End Sub Private Sub OnCanvasMouseUp(arg As NMouseButtonEventArgs) m_EventsLog.LogEvent("Mouse Up. Button: " & arg.Button.ToString()) Select Case arg.Button Case ENMouseButtons.Left m_LeftMouseDown = False Case ENMouseButtons.Right m_RightMouseDown = False Case ENMouseButtons.Middle m_MiddleMouseDown = False Case Else End Select m_Canvas.InvalidateDisplay() End Sub Private Sub OnCanvasMouseDown(arg As NMouseButtonEventArgs) m_EventsLog.LogEvent("Mouse Down. Button: " & arg.Button.ToString()) Select Case arg.Button Case ENMouseButtons.Left m_LeftMouseDown = True If m_CaptureMouseOnLeftMouseDown.Checked Then m_Canvas.CaptureMouse() End If Case ENMouseButtons.Right m_RightMouseDown = True If m_CaptureMouseOnRightMouseDown.Checked Then m_Canvas.CaptureMouse() End If Case ENMouseButtons.Middle m_MiddleMouseDown = True Case Else End Select m_Canvas.InvalidateDisplay() End Sub Private Sub OnCanvasMouseWheel(arg As NMouseWheelEventArgs) m_EventsLog.LogEvent("Mouse Wheel. Delta: " & arg.Delta.ToString()) End Sub Private Sub OnCanvasMouseOut(arg As NMouseOverChangeEventArgs) m_EventsLog.LogEvent("Mouse Out") m_Canvas.InvalidateDisplay() End Sub Private Sub OnCanvasMouseIn(arg As NMouseOverChangeEventArgs) m_EventsLog.LogEvent("Mouse In") m_Canvas.InvalidateDisplay() End Sub Private Sub OnCanvasGotMouseCapture(arg As NMouseCaptureChangeEventArgs) m_EventsLog.LogEvent("Got Mouse Capture") m_Canvas.InvalidateDisplay() End Sub Private Sub OnCanvasLostMouseCapture(arg As NMouseCaptureChangeEventArgs) m_EventsLog.LogEvent("Lost Mouse Capture") m_Canvas.InvalidateDisplay() End Sub #End Region #Region "Fields" Private m_LogMoveEventsCheck As NCheckBox Private m_CaptureMouseOnLeftMouseDown As NCheckBox Private m_CaptureMouseOnRightMouseDown As NCheckBox Private m_Canvas As NCanvas Private m_EventsLog As NExampleEventsLog Private m_MouseLocation As NPoint Private m_LeftMouseDown As Boolean Private m_MiddleMouseDown As Boolean Private m_RightMouseDown As Boolean #End Region #Region "Static Methods" #End Region #Region "Constants" Private Const EnglishLanguageName As String = "English (US)" Private Const BulgarianLanguageName As String = "Bulgarian" Private Const GermanLanguageName As String = "German" #End Region #Region "Nested Types" #End Region End Class

Download a fully functional VB.NET (.vb) Mouse Events Example for WinForms Solution

Related Documentation Links