How to open a userform in the top left corner of the graphics area?

How to open a userform in the top left corner of the graphics area?

LiteracyFanatic
Explorer Explorer
1,389 Views
4 Replies
Message 1 of 5

How to open a userform in the top left corner of the graphics area?

LiteracyFanatic
Explorer
Explorer

I would like to use Inventor's API to get the coordinates of the top left corner of the graphics area so that I can open a form in this location from VBA similar to how many of Inventor's native dialog boxes snap to this location. I already know how to open a form at a given location and how to get the coordinates of the overall application window, but I am unsure what object in the API represents the graphics area and how to get its coordinates. The image below shows the point where I would like to open my form.

 

Capture.PNG

 

0 Likes
1,390 Views
4 Replies
Replies (4)
Message 2 of 5

ThatWorksForMe
Advocate
Advocate

I had nearly the same problems.

Here is a solution:

https://forums.autodesk.com/t5/inventor-customization/vba-userform-position-throws-run-time-error/td...

"activeview" is the keyword you are looking for.

PDM/CAD Administrator
Using Inventor/Vault 2024.3
0 Likes
Message 3 of 5

LiteracyFanatic
Explorer
Explorer

Thank you for your reply. ActiveView definitely pointed me in the right direction. However, I found that I had to multiply the values by a factor of 0.75 to get the form to appear in the correct location on the screen. Here is the code that worked for me:

 

With UserForm1
    .Left = 0.75 * ThisApplication.ActiveView.Left
    .Top = 0.75 * ThisApplication.ActiveView.Top
    .Show
End With

Any idea where this seemingly arbitrary factor comes from? I'd like to make sure that this works properly regardless of the monitor it is displayed on. If it matters, this is the monitor setup I have:

 

Capture.PNG

0 Likes
Message 4 of 5

ThatWorksForMe
Advocate
Advocate

This factor is related to the left menu.

If you pull the left menu to the middle of the screen (like in the screenshot) your code wont work.

Edit the code like this will show you a messagebox with a number. Its the width from the menu in pixel.

MsgBox ThisApplication.ActiveView.Left
With UserForm1 .Left = 0.75 * ThisApplication.ActiveView.Left .Top = 0.75 * ThisApplication.ActiveView.Top .Show End With

 

I use the following code to center the userform on the the screen.

I have the same resolution like you und with my code it is unimportant on which screen the application is running.

Option Explicit
Public Declare PtrSafe Function GetSystemMetrics Lib "user32.dll" (ByVal index As Long) As LongPtr
Public Const sm_cxscreen = 0
Public Const sm_cyscreen = 1
Sub TEST()

    UserForm2.Show
    Dim xr As Integer
    Dim yr As Integer
    Dim a As Application
    Set a = ThisApplication
    With UserForm2
        .StartUpPosition = 0
        If a.Left < 0 Then
            xr = (0.5 * GetSystemMetrics(sm_cxscreen)) - (1.5 * .Width)
        Else
            xr = GetSystemMetrics(sm_cxscreen) + (0.25 * GetSystemMetrics(sm_cxscreen)) - (1.5 * .Width)
        End If
        yr = (0.5 * GetSystemMetrics(sm_cyscreen)) - (0.5 * .Height) - 2 * 136
        .Left = xr
        .Top = yr
    End With
        
End Sub

But as you can see there are also some factors inside.

There is still a bug in it. The window will not open until the second call in the correct position.

Why? I dont know at the moment. Im still try to figure out.

PDM/CAD Administrator
Using Inventor/Vault 2024.3
0 Likes
Message 5 of 5

ThatWorksForMe
Advocate
Advocate

Just forget what I have posted above!

 

Option Explicit
Public Declare PtrSafe Function GetSystemMetrics Lib "user32.dll" (ByVal index As Long) As LongPtr
Public Const sm_cxscreen = 0
Public Const sm_cyscreen = 1
Sub TEST()

    UserForm1.Show
    Dim xr As Integer
    Dim yr As Integer
    Dim a As Application
    Set a = ThisApplication
    With UserForm2
        .StartUpPosition = 0
        xr = 0.75 * ThisApplication.ActiveView.Left
        yr = ThisApplication.ActiveView.Top - (0.25 * .Height) + 4
        .Left = xr
        .Top = yr
    End With
        
End Sub

 

This code should work on every resolution.

Tested on your resolution and on 2560x1440

PDM/CAD Administrator
Using Inventor/Vault 2024.3
0 Likes