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

Article by Adam Bridgman, CSWE 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:


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


Fired when the left-mouse button is pressed down.


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


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


Fired when the middle-mouse button is pressed down.


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


Fired when the mouse pointer is moved.


Fired whenever a mouse event occurs.


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


Fired when the right-mouse button is pressed down.


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


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


    End Function

    Function ModelView_DestroyNotify2(ByVal destroyTYpe As Integer) As Integer


    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)


                openModelViews.Add(iModelView, mView)

                Dim mMouse As New MouseEventHandler()

                mMouse.Init(userAddin, iModelView, Me)


                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 = Nothing

            key = Nothing


        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 = Nothing

            key = Nothing


    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









        SwEventPtr = iSwApp

        openDocs = New Hashtable






End Function

Function DisconnectFromSW() As Boolean Implements ISwAddin.DisconnectFromSW








iSwApp = Nothing






DisconnectFromSW = True

       End Function

For more information, please watch this video:

Related Links

Certified SOLIDWORKS Services available from Javelin

Javelin can help you to:

Find Related Content by TAG:

Adam Bridgman, CSWE

Adam Bridgman in SOLIDWORKS Certified Expert proving customers with technical support and training