FindWindowEx查找VB程序正常,查找VC程序却出问题了
我的程序原来拿一个VB程序测试的时候正常,后来换了一个VC的程序,就找不到子窗口的句柄了
(说明:类名我都是用SPY++看的,都错不了)
WindowHandle = FindWindow(vbNullString, “ABC”)
'''''''''''''''''''''''WindowHandle正常
CWHandle = FindWindowEx(hwnd, WindowHandle , "Button", vbNullString)
'''''''''''''''''''''''CWHandle 为0
附上主要程序:
''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''
Function FindCaptionHwnd(hwnd As Long, CmpStr As String) As Long
Dim CWHandle As Long
Dim x As Integer
Dim str As String
Dim str2 As String
Do While 1
CWHandle = FindWindowEx(hwnd, CWHandle, "Button", vbNullString)
If CWHandle Then
x = GetWindowTextLength(CWHandle)
str = String(x + 1, Chr$(0))
Call GetWindowText(CWHandle, str, Len(CmpStr) * 2 + 2)
Else
FindCaptionHwnd = 0
Exit Function
End If
str2 = Left(str, Len(str) - 1)
If CmpStr = str2 Then
FindCaptionHwnd = CWHandle
Exit Function
End If
Loop
End Function
''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''
Function FindCWHandle(FWtext As String, CWtext As String) As Long
Dim WindowHandle As Long
WindowHandle = FindWindow(vbNullString, FWtext)
If WindowHandle Then '如果获取句柄成功
WindowHandle = FindCaptionHwnd(WindowHandle, CWtext)
If WindowHandle Then
FindCWHandle = WindowHandle
Else
FindCWHandle = 0
End If
Exit Function
End If
FindCWHandle = WindowHandle
MsgBox "没有找到父窗口!"
End Function
'''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''
例如:
call FindCWHandle("父窗口标题","按钮标题")
[解决办法]
再仔细检查下,看看真的“错不了”么