15 Replies Latest reply on Jan 15, 2018 11:12 PM by Mr Omkar Deshpande

    Help with mouse events

    Prashant Mohan

      I have a simple macro that notifies whenever left mouse button is pressed. and shows the coordinates of the mouse pointer. There is a form with a button on it. When you press the button it starts the mouse event.  I want to know how do i bring the command back to code (main) after the event it fired. If you look below i want "Hey" to be displayed after the i press the left mouse button and coordinates at the mouse tip are shown.

       

      I have attached the code as well

      I am new to mouse events and did not find anything that could solve this problem. Any help will be appreciated.

       

      Sub main()

       

          UserForm1.Show vbModeless

         

          MsgBox ("Hey")

         

      End Sub

       

      Dim swApp As SldWorks.SldWorks

      Dim swModel As SldWorks.ModelDoc2

      Dim swView As SldWorks.ModelView

      Dim WithEvents swMouse As SldWorks.Mouse

      Dim X_, Y_ As Double

      Dim listenForClick As Boolean

       

      Private Sub CommandButton1_Click()

         

          Set swModel = swApp.ActiveDoc

          Set swView = swModel.ActiveView

          Set swMouse = swView.GetMouse

          listenForClick = True

         

      End Sub

       

      Private Function swMouse_MouseLBtnDownNotify(ByVal X As Long, ByVal Y As Long, ByVal WParam As Long) As Long

         

          If listenForClick Then

              X_ = X

              Y_ = Y

              MsgBox (X & "; " & Y)

              listenForClick = False

             

              Unload UserForm1

          End If

         

      End Function

       

       

      Private Sub UserForm_Initialize()

       

          Set swApp = Application.SldWorks

       

      End Sub

        • Re: Help with mouse events
          Rajat Jain

          Hi Prashant,

           

          I think the simplest approach for you is to show your form as Modal form. This would solve your issue.

          Sub main()

               UserForm1.Show vbModal

                  MsgBox ("Hey")

          End Sub

          Hope it helps!!!

          Rajat Jain

          • Re: Help with mouse events
            Rob Edwards

            Prashant

             

            I can see you got your answer which is great:)  I am interested in learning how to handle events so had a go at this myself and got something that seems to work, but I am not sure if it is a good way or not.  Whilst I was working it out I found I had to be very careful or Solidworks would crash when I unloaded the form or set the mouse to Nothing in the wrong place. 

             

            I would love to hear from an expert if the following apporoach is 'a good' or 'a bad' idea.  Josh . Please would you be kind enough to comment

             

            Public e As MyEvents
            
            Sub main()
                Set e = New MyEvents
            End Sub
            
            Public Function ShowForm()
                UserForm1.Show vbModeless
            End Function
            
            Public Function CloseForm()
                Unload UserForm1
            End Function
            
            Public Function DoNextThing()
                MsgBox ("Hey!")
            End Function
            
            Public Function Finish()
                Debug.Print ("finish events")
                Set e = Nothing
            End Function
            

             

            'MyEvents Class
            Option Explicit
            
            Dim WithEvents swapp As SldWorks.SldWorks
            Dim WithEvents swMouse As SldWorks.Mouse
            
            Public taskcounter As Integer
            Dim taskComplete(4) As Boolean
            
            Sub class_initialize()
                Set swapp = Application.SldWorks
                taskcounter = 0
            End Sub
            
            Public Function NextTask()
                taskComplete(taskcounter) = True
                taskcounter = taskcounter + 1
            End Function
            
            Public Function swApp_OnIdleNotify() As Long
            
                Debug.Print "idle"
                
                If Not taskComplete(taskcounter) Then
                
                    Select Case taskcounter
                    Case 0
                            Me.NextTask
                            Call ShowForm
                            Set swMouse = swapp.ActiveDoc.ActiveView.GetMouse
                    Case 1
                            Rem wait
                            ' progression controlled by mouse handler
                    Case 2
                            Me.NextTask
                            Set swMouse = Nothing
                            Call CloseForm
                    Case 3
                            Me.NextTask
                            Call DoNextThing
                    Case 4
                            Me.NextTask
                            Call Finish
                    End Select
                End If
            End Function
            
            Private Function swMouse_MouseLBtnDownNotify(ByVal X As Long, ByVal Y As Long, ByVal WParam As Long) As Long
                If UserForm1.CheckBox1.Value Then
                    MsgBox "x:" & X & " y:" & Y
                Else
                    Me.NextTask
                End If
            End Function
            
              • Re: Help with mouse events
                Josh Brady

                Rob... It seems like you're trying to make a macro-in-progress sit and wait.  You really don't want to do that.

                 

                The reason Prashant has a UserForm is (I assume) that you can't declare WithEvents variables in a regular module.  You have to use a Class Module or a UserForm.

                 

                Once you declare something WithEvents, you'll get notified whenever the event you're interested in happens to the object that's WithEvents'd.  As long as that object exists (isn't Nothing and doesn't go out of scope), you'll get the notification.

                 

                As long as a form is loaded, it's variables declared at Module Level don't go out of scope.  They belong to the form.  Once the form is unloaded, they go out of existence and you don't get the notify anymore.

                 

                A class module is sort of like the form, except that it's not visible.  Once you instantiate it, it won't go away until you set it to Nothing.  The class instance will sit there just like the form does and wait for you to do something with it.  It can close itself too.

                 

                This may or may not make sense.  I'm typing stream of consciousness.