Help with mouse events

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

SolidworksApi macros
Answers 15
RJ
RJ 2018-01-14 Best Answer

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

SF
SF 2018-01-14

Hi Rajat,

This does not solves the issues. As when you make the form modal it stays on the top and does not let me click on the screen.

Regards,

Prashant Mohan

RE
RE 2018-01-14
SF
SF 2018-01-14

Hi Rob,

Yes i have looked at it too. It has the same issue, mouse events occur after the code is finished running. I want my code to wait till at least one event is fired (wait for me to click the mosue, fire the event and then proceed with code.

OD
OD 2018-01-15

Do you really need userform here?

I have attached both, with userform (Macro1) and without userform (Macro2) here.

Hope this helps....

Omkar

SF
SF 2018-01-15

Hi Omkar,

I tried both the versions. However, the macro goes directly to the sub function and displays the message "Hey". It does not even invoke the form or fire mouse event.

Regards,

Prashant

JB
JB 2018-01-15

Prashant,

Please study attached.

JB
JB 2018-01-15

Or, just in case you actually want to use the X and Y values...

SF
SF 2018-01-15

That worked Josh,

thank you much. Highly appreciated.

Regards,

Prashant

RE
RE 2018-01-15

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.  ​. Please would you be kind enough to comment

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

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

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.

JB
JB 2018-01-15

Look at this one.

RE
RE 2018-01-15

Very Nice...  Thankyou

SF
SF 2018-01-16

Yes that is correct. Also with form this gives me the ability to let user actually click on a button (like select a face) and then proceeds to select a face on an open part/assembly. This makes it more intuitive.

Regards,

Prashant

OD
OD 2018-01-16

i thought you will run it step by step.

Also, you could have created a shortcut with proper starting procedure.

Anyways, Its good that you've found the solution though...

Omkar