下面是个类. 实现:由 Windows Class 得到 Title 或 ChildClass =================================================================== Option Explicit 'find windows... Declare Function FindWindow Lib "user32" Alias "FindWindowA" (ByVal lpClassName A s String, ByVal lpWindowName As String) As Long 'Declare Function FindWindowEx Lib "user 32" Alias "FindWindowExA" (ByVal hWnd1 A s Long, ByVal hWnd2 As Long, ByVal lpsz1 As String, ByVal lpsz2 As String) As Lon g 'find child by class... Declare Function GetWindow Lib "user32" (ByVal hwnd As Long, ByVal wCmd As Long) As Long Declare Function GetClassName Lib "user3 2" Alias "GetClassNameA" (ByVal hwnd As Long, ByVal lpClassName As String, ByVal nMaxCount As Long) As Long Declare Function GetNextWindow Lib "user 32" Alias "GetWindow" (ByVal hwnd As Lon g, ByVal wFlag As Long) As Long 'find child by title... Declare Function GetWindowText Lib "user 32" Alias "GetWindowTextA" (ByVal hwnd A s Long, ByVal lpString As String, ByVal cch As Long) As Long'...ByClass: Function FindChildByClass&(parent&, ByVa l class$) Dim temp$, class_$, child&, child_& temp$ = String(255, 0) child& = GetWindow(parent&, 5) Do: DoEvents class_$ = Left$(temp$, GetClassName(chil d&, temp$, 255)) If LCase(class$) = LCase(class_$) Then F indChildByClass& = child&: Exit FunctionIf GetWindow(child&, 5) <> 0 Then child_& = FindChildByClass(child&, class $) If child_& <> 0 Then FindChildByCl ass& = child_&: Exit Function End If child& = GetNextWindow(child&, 2) Loop Until child& = 0 FindChildByClass& = 0 End Function '...ByClassN: Function FindChildByClassN&(parent&, han dle$, num%) Dim num_%, child&, temp$, handle_$ num_% = 1 temp$ = String$(255, 0) child& = FindChildByClass(parent&, handl e$) Do: DoEvents child& = GetNextWindow(child&, 2) handle_$ = Left$(temp, GetClassName(chil d&, temp$, 255)) If LCase(handle$) = LCase(handle_$) Then num_% = num_% + 1 Loop Until num% = num_% Or child& = 0 FindChildByClassN& = child& End Function '...ByTitle: Function FindChildByTitle&(ByVal parent& , ByVal title$) Dim temp$, title_$, child&, child_& temp$ = String(255, 0) child& = GetWindow(parent&, 5) Do title_$ = Left$(temp$, GetWindowText(chi ld&, temp$, 255)) If title_$ <> "" And LCase(title$ & "*") Like LCase(title_$ & "*") Then Fi ndChildByTitle& = child&: Exit Function If GetWindow(child&, 5) <> 0 Then child_& = FindChildByTitle(child&, title $) If child_& <> 0 Then FindChildByTi tle = child_&: Exit Function End If child& = GetNextWindow(child&, 2) Loop Until child& = 0 FindChildByTitle& = 0 End Function
下面的程序实现了:通过类名,或title 查找指定应用程序. ===================================================================' 模块部分 Public Declare Function GetDesktopWindow Lib "user32" () As LongPublic Declare Function GetWindow Lib "user32" _ (ByVal hwnd As Long, _ ByVal wCmd As Long) As LongPublic Declare Function GetWindowText Lib "user32" _ Alias "GetWindowTextA" _ (ByVal hwnd As Long, _ ByVal lpString As String, _ ByVal cch As Long) As LongPublic Declare Function GetClassName Lib "user32" _ Alias "GetClassNameA" _ (ByVal hwnd As Long, _ ByVal lpClassName As String, _ ByVal nMaxCount As Long) As LongPublic Const GW_HWNDFIRST = 0 Public Const GW_HWNDLAST = 1 Public Const GW_HWNDNEXT = 2 Public Const GW_HWNDPREV = 3 Public Const GW_OWNER = 4 Public Const GW_CHILD = 5==================================================================='窗体部分 Option ExplicitPrivate Sub cmdStart_Click()
'Used to return window handles. Dim TitleToFind As String, ClassToFind As String
List1.Clear
'Set the FindWindowLike text values from 'the strings entered into the textboxes TitleToFind = (txtTitle) & "*" ClassToFind = (txtClass)
End Sub Private Function FindWindowLike(ByVal hWndStart As Long, _ WindowText As String, _ Classname As String) As Long Dim hwnd As Long Dim sWindowText As String Dim sClassname As String Dim r As Long
'Hold the level of recursion and 'hold the number of matching windows Static level As Integer
'Initialize if necessary. This is only executed 'when level = 0 and hWndStart = 0, normally 'only on the first call to the routine. If level = 0 Then If hWndStart = 0 Then hWndStart = GetDesktopWindow() End If
'Increase recursion counter level = level + 1
'Get first child window hwnd = GetWindow(hWndStart, GW_CHILD) Do Until hwnd = 0
'Search children by recursion Call FindWindowLike(hwnd, WindowText, Classname)
'Get the window text and class name sWindowText = Space$(255) r = GetWindowText(hwnd, sWindowText, 255) sWindowText = Left(sWindowText, r)
'uncommenting the next line causes the routine to 'only return the first matching window. 'Exit Do
End If
'Get next child window hwnd = GetWindow(hwnd, GW_HWNDNEXT)
Loop
'Reduce the recursion counter level = level - 1End Function
下面的程序实现了:通过类名,或title 查找指定应用程序. ===================================================================' 模块部分 Public Declare Function GetDesktopWindow Lib "user32" () As LongPublic Declare Function GetWindow Lib "user32" _ (ByVal hwnd As Long, _ ByVal wCmd As Long) As LongPublic Declare Function GetWindowText Lib "user32" _ Alias "GetWindowTextA" _ (ByVal hwnd As Long, _ ByVal lpString As String, _ ByVal cch As Long) As LongPublic Declare Function GetClassName Lib "user32" _ Alias "GetClassNameA" _ (ByVal hwnd As Long, _ ByVal lpClassName As String, _ ByVal nMaxCount As Long) As LongPublic Const GW_HWNDFIRST = 0 Public Const GW_HWNDLAST = 1 Public Const GW_HWNDNEXT = 2 Public Const GW_HWNDPREV = 3 Public Const GW_OWNER = 4 Public Const GW_CHILD = 5==================================================================='窗体部分 Option ExplicitPrivate Sub cmdStart_Click()
'Used to return window handles. Dim TitleToFind As String, ClassToFind As String
List1.Clear
'Set the FindWindowLike text values from 'the strings entered into the textboxes TitleToFind = (txtTitle) & "*" ClassToFind = (txtClass)
End Sub Private Function FindWindowLike(ByVal hWndStart As Long, _ WindowText As String, _ Classname As String) As Long Dim hwnd As Long Dim sWindowText As String Dim sClassname As String Dim r As Long
'Hold the level of recursion and 'hold the number of matching windows Static level As Integer
'Initialize if necessary. This is only executed 'when level = 0 and hWndStart = 0, normally 'only on the first call to the routine. If level = 0 Then If hWndStart = 0 Then hWndStart = GetDesktopWindow() End If
'Increase recursion counter level = level + 1
'Get first child window hwnd = GetWindow(hWndStart, GW_CHILD) Do Until hwnd = 0
'Search children by recursion Call FindWindowLike(hwnd, WindowText, Classname)
'Get the window text and class name sWindowText = Space$(255) r = GetWindowText(hwnd, sWindowText, 255) sWindowText = Left(sWindowText, r)
'uncommenting the next line causes the routine to 'only return the first matching window. 'Exit Do
End If
'Get next child window hwnd = GetWindow(hwnd, GW_HWNDNEXT)
Loop
'Reduce the recursion counter level = level - 1End Function
Private Declare Function FindWindow Lib "user32" Alias "FindWindowA" (ByVal lpClassName As String, ByVal lpWindowName As String) As Long Private Declare Function PostMessage Lib "user32" Alias "PostMessageA" (ByVal hwnd As Long, ByVal wMsg As Long, ByVal wParam As Long, lParam As Any) 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 ShowWindow Lib "user32" (ByVal hwnd As Long, ByVal nCmdShow As Long) As Long Const SW_SHOWNORMAL = 1 Const WM_CLOSE = &H10 Const gcClassnameMSWord = "OpusApp" Const gcClassnameMSExcel = "XLMAIN" Const gcClassnameMSIExplorer = "IEFrame" Const gcClassnameMSVBasic = "wndclass_desked_gsk" Const gcClassnameNotePad = "Notepad" Const gcClassnameMyVBApp = "ThunderForm" Private Sub Form_Load() Dim WinWnd As Long, Ret As String, RetVal As Long, lpClassName As String 'Ask for a Window title Ret = InputBox("Enter the exact window title:" + Chr$(13) + Chr$(10) + "Note: must be an exact match") 'Search the window WinWnd = FindWindow(vbNullString, Ret) If WinWnd = 0 Then MsgBox "Couldn't find the window ...": Exit Sub 'Show the window ShowWindow WinWnd, SW_SHOWNORMAL 'Create a buffer lpClassName = Space(256) 'retrieve the class name RetVal = GetClassName(WinWnd, lpClassName, 256) 'Show the classname MsgBox "Classname: " + Left$(lpClassName, RetVal) 'Post a message to the window to close itself PostMessage WinWnd, WM_CLOSE, 0&, 0& End Sub
把你的要求说得具体点么。
实现:由 Windows Class 得到 Title 或 ChildClass
===================================================================
Option Explicit
'find windows...
Declare Function FindWindow Lib "user32"
Alias "FindWindowA" (ByVal lpClassName A
s String, ByVal lpWindowName As String)
As Long
'Declare Function FindWindowEx Lib "user
32" Alias "FindWindowExA" (ByVal hWnd1 A
s Long, ByVal hWnd2 As Long, ByVal lpsz1
As String, ByVal lpsz2 As String) As Lon
g
'find child by class...
Declare Function GetWindow Lib "user32"
(ByVal hwnd As Long, ByVal wCmd As Long)
As Long
Declare Function GetClassName Lib "user3
2" Alias "GetClassNameA" (ByVal hwnd As
Long, ByVal lpClassName As String, ByVal
nMaxCount As Long) As Long
Declare Function GetNextWindow Lib "user
32" Alias "GetWindow" (ByVal hwnd As Lon
g, ByVal wFlag As Long) As Long
'find child by title...
Declare Function GetWindowText Lib "user
32" Alias "GetWindowTextA" (ByVal hwnd A
s Long, ByVal lpString As String, ByVal
cch As Long) As Long'...ByClass:
Function FindChildByClass&(parent&, ByVa
l class$)
Dim temp$, class_$, child&, child_&
temp$ = String(255, 0)
child& = GetWindow(parent&, 5)
Do: DoEvents
class_$ = Left$(temp$, GetClassName(chil
d&, temp$, 255))
If LCase(class$) = LCase(class_$) Then F
indChildByClass& = child&: Exit FunctionIf GetWindow(child&, 5) <> 0 Then
child_& = FindChildByClass(child&, class
$)
If child_& <> 0 Then FindChildByCl
ass& = child_&: Exit Function
End If
child& = GetNextWindow(child&, 2)
Loop Until child& = 0
FindChildByClass& = 0
End Function
'...ByClassN:
Function FindChildByClassN&(parent&, han
dle$, num%)
Dim num_%, child&, temp$, handle_$
num_% = 1
temp$ = String$(255, 0)
child& = FindChildByClass(parent&, handl
e$)
Do: DoEvents
child& = GetNextWindow(child&, 2)
handle_$ = Left$(temp, GetClassName(chil
d&, temp$, 255))
If LCase(handle$) = LCase(handle_$) Then
num_% = num_% + 1
Loop Until num% = num_% Or child& = 0
FindChildByClassN& = child&
End Function
'...ByTitle:
Function FindChildByTitle&(ByVal parent&
, ByVal title$)
Dim temp$, title_$, child&, child_&
temp$ = String(255, 0)
child& = GetWindow(parent&, 5)
Do
title_$ = Left$(temp$, GetWindowText(chi
ld&, temp$, 255))
If title_$ <> "" And LCase(title$
& "*") Like LCase(title_$ & "*") Then Fi
ndChildByTitle& = child&: Exit Function
If GetWindow(child&, 5) <> 0 Then
child_& = FindChildByTitle(child&, title
$)
If child_& <> 0 Then FindChildByTi
tle = child_&: Exit Function
End If
child& = GetNextWindow(child&, 2)
Loop Until child& = 0
FindChildByTitle& = 0
End Function
===================================================================' 模块部分
Public Declare Function GetDesktopWindow Lib "user32" () As LongPublic Declare Function GetWindow Lib "user32" _
(ByVal hwnd As Long, _
ByVal wCmd As Long) As LongPublic Declare Function GetWindowText Lib "user32" _
Alias "GetWindowTextA" _
(ByVal hwnd As Long, _
ByVal lpString As String, _
ByVal cch As Long) As LongPublic Declare Function GetClassName Lib "user32" _
Alias "GetClassNameA" _
(ByVal hwnd As Long, _
ByVal lpClassName As String, _
ByVal nMaxCount As Long) As LongPublic Const GW_HWNDFIRST = 0
Public Const GW_HWNDLAST = 1
Public Const GW_HWNDNEXT = 2
Public Const GW_HWNDPREV = 3
Public Const GW_OWNER = 4
Public Const GW_CHILD = 5==================================================================='窗体部分
Option ExplicitPrivate Sub cmdStart_Click()
'Used to return window handles.
Dim TitleToFind As String, ClassToFind As String
List1.Clear
'Set the FindWindowLike text values from
'the strings entered into the textboxes
TitleToFind = (txtTitle) & "*"
ClassToFind = (txtClass)
Call FindWindowLike(0, TitleToFind, ClassToFind)
lbCount = CStr(List1.ListCount) & " matches found"
End Sub
Private Function FindWindowLike(ByVal hWndStart As Long, _
WindowText As String, _
Classname As String) As Long Dim hwnd As Long
Dim sWindowText As String
Dim sClassname As String
Dim r As Long
'Hold the level of recursion and
'hold the number of matching windows
Static level As Integer
'Initialize if necessary. This is only executed
'when level = 0 and hWndStart = 0, normally
'only on the first call to the routine.
If level = 0 Then
If hWndStart = 0 Then hWndStart = GetDesktopWindow()
End If
'Increase recursion counter
level = level + 1
'Get first child window
hwnd = GetWindow(hWndStart, GW_CHILD) Do Until hwnd = 0
'Search children by recursion
Call FindWindowLike(hwnd, WindowText, Classname)
'Get the window text and class name
sWindowText = Space$(255)
r = GetWindowText(hwnd, sWindowText, 255)
sWindowText = Left(sWindowText, r)
sClassname = Space$(255)
r = GetClassName(hwnd, sClassname, 255)
sClassname = Left(sClassname, r)
'Check if window found matches the search parameters
If (sWindowText Like WindowText) And _
(sClassname Like Classname) Then
List1.AddItem hwnd & vbTab & _
sClassname & vbTab & _
sWindowText
FindWindowLike = hwnd
'uncommenting the next line causes the routine to
'only return the first matching window.
'Exit Do
End If
'Get next child window
hwnd = GetWindow(hwnd, GW_HWNDNEXT)
Loop
'Reduce the recursion counter
level = level - 1End Function
===================================================================' 模块部分
Public Declare Function GetDesktopWindow Lib "user32" () As LongPublic Declare Function GetWindow Lib "user32" _
(ByVal hwnd As Long, _
ByVal wCmd As Long) As LongPublic Declare Function GetWindowText Lib "user32" _
Alias "GetWindowTextA" _
(ByVal hwnd As Long, _
ByVal lpString As String, _
ByVal cch As Long) As LongPublic Declare Function GetClassName Lib "user32" _
Alias "GetClassNameA" _
(ByVal hwnd As Long, _
ByVal lpClassName As String, _
ByVal nMaxCount As Long) As LongPublic Const GW_HWNDFIRST = 0
Public Const GW_HWNDLAST = 1
Public Const GW_HWNDNEXT = 2
Public Const GW_HWNDPREV = 3
Public Const GW_OWNER = 4
Public Const GW_CHILD = 5==================================================================='窗体部分
Option ExplicitPrivate Sub cmdStart_Click()
'Used to return window handles.
Dim TitleToFind As String, ClassToFind As String
List1.Clear
'Set the FindWindowLike text values from
'the strings entered into the textboxes
TitleToFind = (txtTitle) & "*"
ClassToFind = (txtClass)
Call FindWindowLike(0, TitleToFind, ClassToFind)
lbCount = CStr(List1.ListCount) & " matches found"
End Sub
Private Function FindWindowLike(ByVal hWndStart As Long, _
WindowText As String, _
Classname As String) As Long Dim hwnd As Long
Dim sWindowText As String
Dim sClassname As String
Dim r As Long
'Hold the level of recursion and
'hold the number of matching windows
Static level As Integer
'Initialize if necessary. This is only executed
'when level = 0 and hWndStart = 0, normally
'only on the first call to the routine.
If level = 0 Then
If hWndStart = 0 Then hWndStart = GetDesktopWindow()
End If
'Increase recursion counter
level = level + 1
'Get first child window
hwnd = GetWindow(hWndStart, GW_CHILD) Do Until hwnd = 0
'Search children by recursion
Call FindWindowLike(hwnd, WindowText, Classname)
'Get the window text and class name
sWindowText = Space$(255)
r = GetWindowText(hwnd, sWindowText, 255)
sWindowText = Left(sWindowText, r)
sClassname = Space$(255)
r = GetClassName(hwnd, sClassname, 255)
sClassname = Left(sClassname, r)
'Check if window found matches the search parameters
If (sWindowText Like WindowText) And _
(sClassname Like Classname) Then
List1.AddItem hwnd & vbTab & _
sClassname & vbTab & _
sWindowText
FindWindowLike = hwnd
'uncommenting the next line causes the routine to
'only return the first matching window.
'Exit Do
End If
'Get next child window
hwnd = GetWindow(hwnd, GW_HWNDNEXT)
Loop
'Reduce the recursion counter
level = level - 1End Function
Private Declare Function PostMessage Lib "user32" Alias "PostMessageA" (ByVal hwnd As Long, ByVal wMsg As Long, ByVal wParam As Long, lParam As Any) 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 ShowWindow Lib "user32" (ByVal hwnd As Long, ByVal nCmdShow As Long) As Long
Const SW_SHOWNORMAL = 1
Const WM_CLOSE = &H10
Const gcClassnameMSWord = "OpusApp"
Const gcClassnameMSExcel = "XLMAIN"
Const gcClassnameMSIExplorer = "IEFrame"
Const gcClassnameMSVBasic = "wndclass_desked_gsk"
Const gcClassnameNotePad = "Notepad"
Const gcClassnameMyVBApp = "ThunderForm"
Private Sub Form_Load()
Dim WinWnd As Long, Ret As String, RetVal As Long, lpClassName As String
'Ask for a Window title
Ret = InputBox("Enter the exact window title:" + Chr$(13) + Chr$(10) + "Note: must be an exact match")
'Search the window
WinWnd = FindWindow(vbNullString, Ret)
If WinWnd = 0 Then MsgBox "Couldn't find the window ...": Exit Sub
'Show the window
ShowWindow WinWnd, SW_SHOWNORMAL
'Create a buffer
lpClassName = Space(256)
'retrieve the class name
RetVal = GetClassName(WinWnd, lpClassName, 256)
'Show the classname
MsgBox "Classname: " + Left$(lpClassName, RetVal)
'Post a message to the window to close itself
PostMessage WinWnd, WM_CLOSE, 0&, 0&
End Sub