I am trying to get the location of a solidworks window on my screen. I'm using the GetWindowRect function to do this. This works for all applications I've tried, except for Solidworks Applications. This is a Solidworks macro so its written in VBA:
Private Declare PtrSafe Function FindWindow Lib "user32" Alias "FindWindowA" ( _
ByVal lpClassName As String, _
ByVal lpWindowName As String _
) As LongPtr
Private Declare PtrSafe Function GetWindowRect Lib "user32" ( _
ByVal hwnd As LongPtr, _
lpRect As RECT _
) As Long
Private Type RECT
Left As Long
Top As Long
right As Long
bottom As Long
End Type
Private this As RECT
Public Sub swWindow(lpClassName As String, lpWindowName As String)
Dim hwnd As LongPtr
hwnd = FindWindow(lpClassName, lpWindowName)
Dim check As long
check = GetWindowRect(hwnd, this)
Debug.Print Err.LastDllError
End Sub
I get locations for all tested applications (excel, txt, etc), but Solidworks Applications only returns zero values.
- The "FindWindow" method does return a handle (724300)
- The "GetWindowRect" method returns a value of 1 (check variable), which means the function was successful.
- The Err.LastDllError is zero, which indicates nothing went wrong.
But my actual locations being return are all zeros. Anybody out there ran into a similar problem, or know what I'm doing wrong?
Thanks.
My problem was my
lpWindowName
was referencing a child window instead of the parent window. The caption at the top of the screen is the file name. If you use this forlpWindowname
, then you'll get the child handle. The correct format to get the parent is "SOLIDWORKS Premium 2022 SP4.0 - [filename]"To generate this programmatically for the currently active document, and get its parent handler, I did the following: