SOLIDWORKS 2020 Launch Broadcast, on Tue, Oct 1 at 9:00 AM ET  REGISTER NOW ›

Add-in Series 4 – Handle Mouse Events [VIDEO]

Article by Adam Bridgman, CSWE created/updated January 9, 2014

There is a summary of all of the available mouse events in SolidWorks found under this help topic SolidWorks.Interop.sldworks Namespace

The mouse event handler gives us access to the following events:

DMouseEvents_MouseLBtnDblClkNotifyEventHandler

Fired when the left-mouse button is double-clicked.

DMouseEvents_MouseLBtnDownNotifyEventHandler

Fired when the left-mouse button is pressed down.

DMouseEvents_MouseLBtnUpNotifyEventHandler

Fired when the left-mouse button is released after being pressed.

DMouseEvents_MouseMBtnDblClkNotifyEventHandler

Fired when the middle-mouse button is double-clicked.

DMouseEvents_MouseMBtnDownNotifyEventHandler

Fired when the middle-mouse button is pressed down.

DMouseEvents_MouseMBtnUpNotifyEventHandler

Fired when the middle-mouse button is released after being pressed.

DMouseEvents_MouseMoveNotifyEventHandler

Fired when the mouse pointer is moved.

DMouseEvents_MouseNotifyEventHandler

Fired whenever a mouse event occurs.

DMouseEvents_MouseRBtnDblClkNotifyEventHandler

Fired when the right-mouse button is double-clicked.

DMouseEvents_MouseRBtnDownNotifyEventHandler

Fired when the right-mouse button is pressed down.

DMouseEvents_MouseRBtnUpNotifyEventHandler

Fired when the right-mouse button is released after being pressed down.

DMouseEvents_MouseSelectNotifyEventHandler

Fired when the user makes a selection in the model view using the mouse.

To make use of these events we will need to write a mouse event handler.  Custom event handlers are all very similar so this tutorial can be used for them as well.

Using SolidWorks Events to Customize Task Pane:

  1. Turn on event listeners
  2. Open EventHandling.vb, Add a global variable in the DocumentEventHandler class to keep track of which documents have events monitored and another one to access the host control.

 Protected openMouseEvents As New Hashtable()

Protected iTaskHost As UserControl1

  1.  Since we initiate the Mouse events class from the DocumentEventHandler class we need to pass the task pane object to it.  Use the following function to do this:

 

Sub SetTaskHost(ByRef iTaskHostIn As UserControl1)

iTaskHost = iTaskHostIn

End Sub

 

  1. Add the following class. This class should look very similar to the class DocView because it must attach to all open documents regardless of their type separately.  This class implements ModelView so that when the document is closed or the add-in is turned off an event is triggered to stop handling mouse events for that document.

 

‘Class for handling Mouse events

Public Class MouseEventHandler

    ‘Highlighted lines are different than DocView

    Dim WithEvents iMouse As Mouse

    Dim WithEvents iModelView As ModelView

    Dim userAddin As SwAddin

    Dim parentDoc As DocumentEventHandler

           Dim iTaskHost As UserControl1

    Dim swApp As SldWorks.SldWorks

    ‘Allow this class to access the task pane

    Friend Sub SetTaskPane(ByRef iTaskHostIn As UserControl1)

        iTaskHost = iTaskHostIn

    End Sub

    ‘Allow this class to access SolidWorks

    Friend Sub SetSwApp(ByRef swAppIn As SldWorks.SldWorks)

        swApp = swAppIn

    End Sub

    Function Init(ByVal addin As SwAddin, ByVal mView As ModelView, ByVal parent As DocumentEventHandler) As Boolean

        userAddin = addin

        iMouse = mView.GetMouse

        iModelView = mView

        parentDoc = parent

    End Function

    Function AttachEventHandlers() As Boolean

        AddHandler iModelView.DestroyNotify2, AddressOf Me.ModelView_DestroyNotify2

        AddHandler iModelView.RepaintNotify, AddressOf Me.ModelView_RepaintNotify

        AddHandler iMouse.MouseLBtnUpNotify, AddressOf Me.Mouse_LBtnUpNotify

    End Function

    Function DetachEventHandlers() As Boolean

        RemoveHandler iModelView.DestroyNotify2, AddressOf Me.ModelView_DestroyNotify2

        RemoveHandler iModelView.RepaintNotify, AddressOf Me.ModelView_RepaintNotify

        RemoveHandler iMouse.MouseLBtnUpNotify, AddressOf Me.Mouse_LBtnUpNotify

        parentDoc.DetachModelViewEventHandler(iModelView)

    End Function

    Function ModelView_DestroyNotify2(ByVal destroyTYpe As Integer) As Integer

        DetachEventHandlers()

    End Function

    Function ModelView_RepaintNotify(ByVal repaintTYpe As Integer) As Integer

    End Function

    Private Function Mouse_LBtnUpNotify(x As Integer, y As Integer, wparam As Integer) As Integer

        MsgBox(“Left Click”)

    End Function

End Class

 

  1. In EventHandling.v find ConnectModelViews() and DisconnectModelViews in the DocumentEventHandler  class and modify them to add event handlers for our new mouse event class

 

Function ConnectModelViews() As Boolean

        Dim iModelView As ModelView

        iModelView = iDocument.GetFirstModelView()

        While (Not iModelView Is Nothing)

            If Not openModelViews.Contains(iModelView) Then

                Dim mView As New DocView()

                mView.Init(userAddin, iModelView, Me)

                mView.AttachEventHandlers()

                openModelViews.Add(iModelView, mView)

                Dim mMouse As New MouseEventHandler()

                mMouse.Init(userAddin, iModelView, Me)

                mMouse.AttachEventHandlers()

                openMouseEvents.Add(iModelView, mMouse)

            End If

            iModelView = iModelView.GetNext

        End While

    End Function

    Function DisconnectModelViews() As Boolean

        ‘Close events on all currently open docs

        Dim mView As DocView

        Dim key As ModelView

        Dim numKeys As Integer

        numKeys = openModelViews.Count

        Dim keys() As Object = New Object(numKeys – 1) {}

        ‘Remove all ModelView event handlers

        openModelViews.Keys.CopyTo(keys, 0)

        For Each key In keys

            mView = openModelViews.Item(key)

            mView.DetachEventHandlers()

            openModelViews.Remove(key)

            mView = Nothing

            key = Nothing

        Next

        Dim mMouse As MouseEventHandler

        numKeys = openMouseEvents.Count

        keys = New Object(numKeys – 1) {}

        openMouseEvents.Keys.CopyTo(keys, 0)

        For Each key In keys

            mMouse = openMouseEvents.Item(key)

            mMouse.DetachEventHandlers()

            openMouseEvents.Remove(key)

            mMouse = Nothing

            key = Nothing

        Next

    End Function

 

This code could be shortened, but it is more readable this way

 

  1. In the previous video all event handlers were disabled.  To re-enable them open SwAddin and make the following changes to the code:

 

Function ConnectToSW(ByVal ThisSW As Object, ByVal Cookie As Integer) As Boolean Implements ISwAddin.ConnectToSW

iSwApp=ThisSW

addinID=Cookie

‘Setupcallbacks

‘iSwApp.SetAddinCallbackInfo(0,Me,addinID)

‘SetuptheCommandManager

‘iCmdMgr=iSwApp.GetCommandManager(Cookie)

‘AddCommandMgr()

‘SetuptheEventHandlers

        SwEventPtr = iSwApp

        openDocs = New Hashtable

 AttachEventHandlers()

 AddTaskPane()

‘SetupSamplePropertyManager

‘AddPMP()

ConnectToSW=True

End Function

Function DisconnectFromSW() As Boolean Implements ISwAddin.DisconnectFromSW

RemoveTaskPane()

‘RemoveCommandMgr()

‘RemovePMP()

DetachEventHandlers()

‘System.Runtime.InteropServices.Marshal.ReleaseComObject(iCmdMgr)

‘iCmdMgr=Nothing

System.Runtime.InteropServices.Marshal.ReleaseComObject(iSwApp)

iSwApp = Nothing

‘Theaddin_must_callGC.Collect()hereinordertoretrieveallmanagedcodepointers

GC.Collect()

GC.WaitForPendingFinalizers()

GC.Collect()

GC.WaitForPendingFinalizers()

DisconnectFromSW = True

       End Function

For more information, please watch this video:

Posts related to 'Add-in Series 4 – Handle Mouse Events [VIDEO]'

Find Related Content by TAG:

Adam Bridgman, CSWE

Want to learn SOLIDWORKS?

Take a training course from our team of Certified SOLIDWORKS Experts