16,554
社区成员
发帖
与我相关
我的任务
分享
Public Const GW_CHILD = 5
Public Const GW_HWNDNEXT = 2
Public Declare Function GetClassName Lib "USER32" Alias "GetClassNameA" (ByVal hwnd As Integer, ByVal lpClassName As String, ByVal nMaxCount As Integer) As Integer
Public Declare Function GetWindow Lib "user32" (ByVal hWnd As Integer, ByVal wCmd As Integer) As Integer
'按类名获取子窗口句柄
Public Function FindControlHwndByClassName(ByVal nHwnd As Long, ByVal clsName As String) As Long
Dim fHwnd As Long, myStr As String, sHwnd As Long
fHwnd = GetWindow(nHwnd, GW_CHILD)
If fHwnd = 0 Then
Return fHwnd
End If
Do While fHwnd > 0
myStr = Space(100)
GetClassName(fHwnd, myStr, 100)
If InStr(myStr, clsName) > 0 Then
Return fHwnd’检测到条件符合,无法退出函数
End If
sHwnd = GetWindow(fHwnd, GW_CHILD)
If sHwnd > 0 Then
FindControlHwndByClassName(fHwnd, clsName)‘会再次执行这句
End If
fHwnd = GetWindow(fHwnd, GW_HWNDNEXT)
Loop
Return fHwnd
End Function
'调用代码
Dim a1 As Long = FindControlHwndByClassName(461452, "Shell DocObject View")
Debug.Print(a1)
Public Class Class2
Private Boolean1 As Boolean = True
Public Const GW_CHILD = 5
Public Const GW_HWNDNEXT = 2
Public Declare Function GetClassName Lib "USER32" Alias "GetClassNameA" (ByVal hwnd As Integer, ByVal lpClassName As String, ByVal nMaxCount As Integer) As Integer
Public Declare Function GetWindow Lib "user32" (ByVal hWnd As Integer, ByVal wCmd As Integer) As Integer
'按类名获取子窗口句柄
Public Function FindControlHwndByClassName(ByVal nHwnd As Long, ByVal clsName As String) As Long
Dim fHwnd As Long, myStr As String, sHwnd As Long
fHwnd = GetWindow(nHwnd, GW_CHILD)
If fHwnd = 0 Then
Return fHwnd
End If
Do While fHwnd > 0
myStr = Space(100)
GetClassName(fHwnd, myStr, 100)
If InStr(myStr, clsName) > 0 Then
sHwnd = 0
Return fHwnd '检测到条件符合,无法退出函数
Boolean1 = False
End If
sHwnd = GetWindow(fHwnd, GW_CHILD)
If sHwnd > 0 Then
FindControlHwndByClassName(fHwnd, clsName) '会再次执行这句
End If
If Boolean1 = False Then
Return fHwnd
End If
fHwnd = GetWindow(fHwnd, GW_HWNDNEXT)
Loop
Return fHwnd
End Function
'调用代码
Dim a1 As Long = FindControlHwndByClassName(461452, "Shell DocObject View")
Debug.Print(a1)
Public Class Class2
Private Boolean1 As Boolean = True
Public Const GW_CHILD = 5
Public Const GW_HWNDNEXT = 2
Public Declare Function GetClassName Lib "USER32" Alias "GetClassNameA" (ByVal hwnd As Integer, ByVal lpClassName As String, ByVal nMaxCount As Integer) As Integer
Public Declare Function GetWindow Lib "user32" (ByVal hWnd As Integer, ByVal wCmd As Integer) As Integer
'按类名获取子窗口句柄
Public Function FindControlHwndByClassName(ByVal nHwnd As Long, ByVal clsName As String) As Long
Dim fHwnd As Long, myStr As String, sHwnd As Long
fHwnd = GetWindow(nHwnd, GW_CHILD)
If fHwnd = 0 Then
Return fHwnd
End If
Do While fHwnd > 0
myStr = Space(100)
GetClassName(fHwnd, myStr, 100)
If InStr(myStr, clsName) > 0 Then
sHwnd = 0
' Return fHwnd '检测到条件符合,无法退出函数
Boolean1 = False
End If
sHwnd = GetWindow(fHwnd, GW_CHILD)
If sHwnd > 0 Then
FindControlHwndByClassName(fHwnd, clsName) '会再次执行这句
End If
If Boolean1 = False Then
Return fHwnd
End If
fHwnd = GetWindow(fHwnd, GW_HWNDNEXT)
Loop
Return fHwnd
End Function
'调用代码
Dim a1 As Long = FindControlHwndByClassName(461452, "Shell DocObject View")
Debug.Print(a1)
Public Function FindControlHwndByClassName(ByVal nHwnd As Long, ByVal clsName As String) As Long
Dim fHwnd As Long, myStr As String, sHwnd As Long
fHwnd = GetWindow(nHwnd, GW_CHILD)
Do While fHwnd > 0
myStr = Space(100)
GetClassName(fHwnd, myStr, 100)
If InStr(myStr, clsName) > 0 Then Return fHwnd
sHwnd = FindControlHwndByClassName(fHwnd, clsName)
If sHwnd > 0 Then Return sHwnd
fHwnd = GetWindow(fHwnd, GW_HWNDNEXT)
Loop
Return fHwnd
End Function
总之,超过“逻辑”表达要求,如果代码写得越多,可能越垃圾。 Public Function FindControlHwndByClassName(ByVal nHwnd As Long, ByVal clsName As String) As Long
Dim fHwnd As Long, myStr As String, sHwnd As Long
fHwnd = GetWindow(nHwnd, GW_CHILD)
If fHwnd = 0 Then Return fHwnd
Do While fHwnd > 0
myStr = Space(100)
GetClassName(fHwnd, myStr, 100)
If InStr(myStr, clsName) > 0 Then Return fHwnd '检测到条件符合,无法退出函数
sHwnd = FindControlHwndByClassName(fHwnd, clsName)
If sHwnd > 0 Then Return sHwnd
fHwnd = GetWindow(fHwnd, GW_HWNDNEXT)
Loop
Return fHwnd
End Function
你可以找一本数据结构的书学习一下,看看如何写递归代码才算是深度优先遍历。 Public Function FindControlHwndByClassName(ByVal nHwnd As Long, ByVal clsName As String) As Long
Dim fHwnd As Long, myStr As String, sHwnd As Long
fHwnd = GetWindow(nHwnd, GW_CHILD)
If fHwnd = 0 Then Return fHwnd
myStr = Space(100)
Do While fHwnd > 0
GetClassName(fHwnd, myStr, 100)
If InStr(myStr, clsName) > 0 Then Return fHwnd '检测到条件符合,无法退出函数
sHwnd = GetWindow(fHwnd, GW_CHILD)
If sHwnd > 0 Then Return FindControlHwndByClassName(fHwnd, clsName) '会再次执行这句
fHwnd = GetWindow(fHwnd, GW_HWNDNEXT)
Loop
Return fHwnd
End Function
Public Function FindControlHwndByClassName(ByVal nHwnd As Long, ByVal clsName As String) As Long
Dim fHwnd As Long, myStr As String, sHwnd As Long
fHwnd = GetWindow(nHwnd, GW_CHILD)
If fHwnd = 0 Then Return fHwnd
myStr = Space(100)
Do While fHwnd > 0
GetClassName(fHwnd, myStr, 100)
If InStr(myStr, clsName) > 0 Then Return fHwnd
sHwnd = GetWindow(fHwnd, GW_CHILD)
If sHwnd > 0 Then Return FindControlHwndByClassName(fHwnd, clsName) '会再次执行这句
fHwnd = GetWindow(fHwnd, GW_HWNDNEXT)
Loop
Return fHwnd
End Function
Public Class Form1
Public Const GW_CHILD = 5
Public Const GW_HWNDNEXT = 2
Public Declare Function GetClassName Lib "USER32" Alias "GetClassNameA" (ByVal hwnd As Integer, ByVal lpClassName As String, ByVal nMaxCount As Integer) As Integer
Public Declare Function GetWindow Lib "user32" (ByVal hWnd As Integer, ByVal wCmd As Integer) As Integer
Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
'调用代码
Dim a1 As Long = FindControlHwndByClassName(461452, "Shell DocObject View")
Debug.Print(a1)
End Sub
'按类名获取子窗口句柄
Public Function FindControlHwndByClassName(ByVal nHwnd As Long, ByVal clsName As String) As Long
Dim fHwnd As Long, myStr As String, sHwnd As Long
fHwnd = GetWindow(nHwnd, GW_CHILD)
If fHwnd = 0 Then
Return fHwnd
Else
Do While fHwnd > 0
myStr = Space(100)
GetClassName(fHwnd, myStr, 100)
If InStr(myStr, clsName) > 0 Then
'Return fHwnd '检测到条件符合,无法退出函数
Exit Do '防止死循环
End If
sHwnd = GetWindow(fHwnd, GW_CHILD)
If sHwnd > 0 Then
FindControlHwndByClassName(fHwnd, clsName) '会再次执行这句
End If
fHwnd = GetWindow(fHwnd, GW_HWNDNEXT)
Loop
Return fHwnd
End If
End Function
End Class
Public Class Form1
Public Const GW_CHILD = 5
Public Const GW_HWNDNEXT = 2
Public Declare Function GetClassName Lib "USER32" Alias "GetClassNameA" (ByVal hwnd As Integer, ByVal lpClassName As String, ByVal nMaxCount As Integer) As Integer
Public Declare Function GetWindow Lib "user32" (ByVal hWnd As Integer, ByVal wCmd As Integer) As Integer
Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
'调用代码
Dim a1 As Long = FindControlHwndByClassName(461452, "Shell DocObject View")
Debug.Print(a1)
End Sub
'按类名获取子窗口句柄
Public Function FindControlHwndByClassName(ByVal nHwnd As Long, ByVal clsName As String) As Long
Dim fHwnd As Long, myStr As String, sHwnd As Long
fHwnd = GetWindow(nHwnd, GW_CHILD)
If fHwnd = 0 Then
Return fHwnd
Exit Function
Else
Do While fHwnd > 0
myStr = Space(100)
GetClassName(fHwnd, myStr, 100)
If InStr(myStr, clsName) > 0 Then
'Return fHwnd '检测到条件符合,无法退出函数
Exit Do '防止死循环
End If
sHwnd = GetWindow(fHwnd, GW_CHILD)
If sHwnd > 0 Then
FindControlHwndByClassName(fHwnd, clsName) '会再次执行这句
End If
fHwnd = GetWindow(fHwnd, GW_HWNDNEXT)
Loop
Return fHwnd
End If
End Function
End Class
[code=vb]
Public Class Form1
Public Const GW_CHILD = 5
Public Const GW_HWNDNEXT = 2
Public Declare Function GetClassName Lib "USER32" Alias "GetClassNameA" (ByVal hwnd As Integer, ByVal lpClassName As String, ByVal nMaxCount As Integer) As Integer
Public Declare Function GetWindow Lib "user32" (ByVal hWnd As Integer, ByVal wCmd As Integer) As Integer
Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
'调用代码
Dim a1 As Long = FindControlHwndByClassName(461452, "Shell DocObject View")
Debug.Print(a1)
End Sub
'按类名获取子窗口句柄
Public Function FindControlHwndByClassName(ByVal nHwnd As Long, ByVal clsName As String) As Long
Dim fHwnd As Long, myStr As String, sHwnd As Long
fHwnd = GetWindow(nHwnd, GW_CHILD)
If fHwnd = 0 Then
Return fHwnd
Exit Function
End If
Do While fHwnd > 0
myStr = Space(100)
GetClassName(fHwnd, myStr, 100)
If InStr(myStr, clsName) > 0 Then
Return fHwnd '检测到条件符合,无法退出函数
Exit Function
End If
sHwnd = GetWindow(fHwnd, GW_CHILD)
If sHwnd > 0 Then
FindControlHwndByClassName(fHwnd, clsName) '会再次执行这句
End If
fHwnd = GetWindow(fHwnd, GW_HWNDNEXT)
Loop
Return fHwnd
End Function
End Class
[/code]