Private Declare Function FindWindow Lib "user32" Alias "FindWindowA" (ByVal lpClassName As String, ByVal lpWindowName As String) As Long
Private Declare Function GetWindowText Lib "user32.dll" Alias "GetWindowTextA" (ByVal hWnd As Long, ByVal lpString As String, ByVal cch As Long) As Long
Private Declare Function GetWindowTextLength Lib "user32.dll" Alias "GetWindowTextLengthA" (ByVal hWnd As Long) As Long
Function 查找窗口句柄(窗口类名,窗口标题)
If Cstr(窗口类名) = "0" Then
查找窗口句柄 = FindWindow(vbNullString, Cstr(窗口标题))
ElseIf Cstr(窗口标题) = "0" Then
查找窗口句柄 = FindWindow(Cstr(窗口类名), vbNullString)
Else
查找窗口句柄 = FindWindow(Cstr(窗口类名), Cstr(窗口标题))
End If
End Function
'示例:根据类名查找句柄:hwnd = 查找窗口句柄("TXGuifoundation",0)
'根据窗口句柄得到该窗口的标题
Function getCaption(hWnd As Long)
Dim hWndlength As Long, hWndTitle As String, A As Long
hWndlength = GetWindowTextLength(hWnd)
hWndTitle = String$(hWndlength, 0)
A = GetWindowText(hWnd, hWndTitle, (hWndlength + 1))
getCaption = hWndTitle
End Function
Private Declare Function FindWindow Lib "user32" Alias "FindWindowA" (ByVal lpClassName As String, ByVal lpWindowName As String) As Long
Private Declare Function GetWindowText Lib "user32" Alias "GetWindowTextA" (ByVal hwnd As Long, ByVal lpString As String, ByVal cch As Long) As Long
Private Sub Command1_Click()
Dim n As Long, s As String, l As Long
n = FindWindow("窗体类名", vbNullString)
If n<>0 Then
s = String(200, 0)
l = GetWindowText(n, s, 200)
s = Trim(Left(s, l))
If s <> "" Then MsgBox "窗体的标题是:" & s
End If
End Sub
如果只有一个同类型的窗体被打开,可以解决