4 Replies Latest reply on Feb 11, 2015 1:01 PM by JOHN GEORGE

    How to set a user form to the foreground??

    Jason George

      I have a macro that I am working and on the first time SolidWorks is opened and the macro is ran, user forms that require input do not come to the foreground.  At first you think SolidWorks is locked up but it's really just waiting for user input on the form you can't see.  A user has to hit Alt+tab to scroll and find the window to bring the form to the foreground and enter the requested info.  Using SolidWorks 2012 and Window 7 64 bit.

        • Re: How to set a user form to the foreground??
          Christopher Estelow

          Perhaps you can use some of the code located here to do what you want.



          • Re: How to set a user form to the foreground??
            JOHN GEORGE


            You can add  a module to your existing form as explained in this discussion

            Hope this helps

            • Re: How to set a user form to the foreground??
              Jason George

              Thanks for the info!  I am working on making some updates to my company's already existing macro.  My SolidWorks API and programming skills are fairly minimal so I am a bit confused as to how to update.  I would like to use this:



              Private Sub displayMyForm()
                  Dim form As New MyForm
                  If utils.SldWorks_IsX64(swApp) Then
                      doWindowHack form
                      form.Show vbModal
                  End If
              End Sub

              And then the doWindowHack method looks like this:



              Private Sub doWindowHack(ByRef form As MyForm)
                  'Hack around the fact that 32bit VBA dialogs appear behind 64 bit windows
                  form.Show vbModeless
                  If isX64(swApp) Then
                      hWndPartDataForm = FindWindow(vbNullString, form.Caption)
                      Dim pos As WINDOWPLACEMENT
                      Dim formX, formY, formW, formH As Long
                      Dim appX, appY, appW, appH As Long
                      Dim retval As Long
                      Dim winRect As rect
                      retval = GetWindowRect(hWndSolidWorks, winRect)
                      appX = winRect.Left
                      appY = winRect.Top
                      appW = winRect.Right - appX
                      appH = winRect.Bottom - appY
                      retval = GetWindowRect(hWndPartDataForm, winRect)
                      formX = winRect.Left
                      formY = winRect.Top
                      formW = winRect.Right - formX
                      formH = winRect.Bottom - formY
                      'Center form on current solidworks window
                      formX = appX + (appW / 2) - (formW / 2)
                      formY = appY + (appH / 2) - (formH / 2)
                      If ((hWndPartDataForm <> 0) And (hWndSolidWorks <> 0)) Then
                          hWndOriginalParent = utils.SetOwner(hWndPartDataForm, hWndSolidWorks)
                      End If
                      retval = SetWindowPos(hWndPartDataForm, HWND_NOTOPMOST, formX, formY, 0, 0, SWP_NOSIZE)
                  End If
              End Sub



              I am not sure where or how to add the private sub for my user form as the current code has showing the user form in the end of a if then statement:


              For i = 0 To UBound(vConfNameArr)

                              sConfigName = vConfNameArr(i)

                              Set config = iDoc.GetConfigurationByName(sConfigName)

                              If config.IsDerived = False Then

                                  wSynDrawingPartNumber = Left(iDoc.GetTitle, 6)

                                  wSynBOMPartNumber = wSynDrawingPartNumber + Left(sConfigName, 4)

                                  UserForm1.lblTitle.caption = "Enter # of Signal & Ground Pin Holes for " + sConfigName ' + " Configuration"

                                  UserForm1.lblSignal.caption = "# of Signal Pin Holes"

                                  UserForm1.lblGround.caption = "# of Ground Pin Holes"

                                  UserForm1.txtConfig.Text = sConfigName

                                  UserForm1.txtCompType.Text = ComponentTypeCombobox.ItemText(-1)

                                  UserForm1.txtSeries.Text = SeriesCombobox.ItemText(-1)

                                  UserForm1.txtSignal.Text = iDoc.CustomInfo2(sConfigName, "SynSignalPins")

                                  UserForm1.txtGround.Text = iDoc.CustomInfo2(sConfigName, "SynGroundPins")

                                  UserForm1.txtSites.Text = CStr(NoOfSitesNumberbox.Value)

                                  UserForm1.txtDevice.Text = StrConv(DeviceTypeTextbox.Text, vbUpperCase)

                                  UserForm1.txtPitch.Text = CStr(PitchNumberbox.Value)


                                  If UserForm1.Visible = False Then

                                      Call DeleteProperties(sConfigName)

                • Re: How to set a user form to the foreground??
                  JOHN GEORGE

                  I am not an API or Macro expert either.


                  If you look at the attachment "FormOnTop.swp" from my previous post link, that gives a better example of a form and module inside that macro.

                  I modified few macros running on SW2012 with 64Bit and works fine


                  There is another code from Keith (CADSharp) is available in this forum which is very simple and easy to insert into the main form code.