Option Explicit
#If VBA7 Then
Private Declare PtrSafe Function GetWindow Lib "user32" _
(ByVal hwnd As LongPtr, ByVal uCmd As Long) As LongPtr
Private Declare PtrSafe Function GetClassName Lib "user32" Alias "GetClassNameA" _
(ByVal hwnd As LongPtr, ByVal lpClassName As String, ByVal nMaxCount As Long) As Long
Private Declare PtrSafe Function GetParent Lib "user32" _
(ByVal hwnd As LongPtr) As LongPtr
#Else
Private Declare Function GetWindow Lib "user32" _
(ByVal hwnd As Long, ByVal uCmd As Long) As Long
Private Declare Function GetClassName Lib "user32" Alias "GetClassNameA" _
(ByVal hwnd As Long, ByVal lpClassName As String, ByVal nMaxCount As Long) As Long
Private Declare Function GetParent Lib "user32" _
(ByVal hwnd As Long) As Long
#End If
Private Const GW_CHILD As Long = 5
Private Const GW_HWNDNEXT As Long = 2
Public Function FindThunderDFrameParent(hWndStart As LongPtr, hwndForm As LongPtr) As LongPtr
Dim hChild As LongPtr, cls As String * 64, l As Long
If hWndStart = 0 Then hWndStart = Application.hwnd
l = GetClassName(hWndStart, cls, Len(cls))
If left$(cls, l) = "ThunderDFrame" And hWndStart = hwndForm Then
FindThunderDFrameParent = GetParent(hWndStart)
Exit Function
End If
hChild = GetWindow(hWndStart, GW_CHILD)
Do While hChild <> 0
FindThunderDFrameParent = FindThunderDFrameParent(hChild, hwndForm)
If FindThunderDFrameParent <> 0 Then Exit Function
hChild = GetWindow(hChild, GW_HWNDNEXT)
Loop
End Function