1,486
社区成员
发帖
与我相关
我的任务
分享
hProcess = OpenProcess(PROCESS_ALL_ACCESS, False, ProcessInfo.th32ProcessID)'得到进程的句柄 每一次运行的值都不一样的 有疑惑?
a = EnumChildWindows(hProcess, AddressOf EnumChildProc, 0) '得到相关窗体的控件的句柄
'EnumChildProc 这个函数不懂怎么写? 我自己写的就是返回1, a的值永远是0 ,还有如何循环? 用for?
'标准模块
Option Explicit
Private Declare Function GetClassName Lib "user32.dll" Alias "GetClassNameA" (ByVal hwnd As Long, ByVal lpClassName As String, ByVal nMaxCount As Long) As Long
'Public Declare Function EnumChildWindows Lib "user32.dll" (ByVal hWndParent As Long, ByVal lpEnumFunc As Long, ByVal lParam As Long) As Long
Public Declare Function EnumWindows Lib "user32" (ByVal lpEnumFunc As Long, ByVal lParam As Long) As Boolean
Private Declare Function GetWindowText Lib "user32" Alias "GetWindowTextA" (ByVal hwnd As Long, ByVal lpString As String, ByVal cch As Long) As Long
Private Declare Function GetWindowTextLength Lib "user32" Alias "GetWindowTextLengthA" (ByVal hwnd As Long) As Long
'Private Declare Function SendMessage Lib "user32.dll" Alias "SendMessageA" (ByVal hwnd As Long, ByVal wMsg As Long, ByVal wParam As Long, ByRef lParam As Any) As Long
'Private Const WM_CLOSE As Long = &H10&
Public Function EnumWindowsProc(ByVal hwnd As Long, ByVal lParam As Long) As Boolean
Dim WindowCaption As String, LengthCaption As Long, WindowClassName As String * 256
LengthCaption = GetWindowTextLength(hwnd)
WindowCaption = Space(LengthCaption)
Call GetWindowText(hwnd, WindowCaption, LengthCaption + 1)
Call GetClassName(hwnd, WindowClassName, 256)
WindowClassName = Left(WindowClassName, InStr(WindowClassName, Chr(0)) - 1)
Form1.List1.AddItem "窗口句柄:" & hwnd & " 窗口标题:" & WindowCaption & "窗口类名:" & WindowClassName
'If InStr(1, WindowCaption, "窗体的标题") > 0 Then
' SendMessage hwnd, WM_CLOSE, 0&, 0& '关闭指定窗体标题的窗体
'End If
EnumWindowsProc = True
End Function
'窗体Form1代码
Option Explicit
'窗体上添加一个命令按钮Command1,一个列表框List1
Private Sub Command1_Click()
EnumWindows AddressOf EnumWindowsProc, ByVal 0&
End Sub
'标准模块
Option Explicit
Private Declare Function GetClassName Lib "user32.dll" Alias "GetClassNameA" (ByVal hWnd As Long, ByVal lpClassName As String, ByVal nMaxCount As Long) As Long
Public Declare Function EnumChildWindows Lib "user32.dll" (ByVal hWndParent As Long, ByVal lpEnumFunc As Long, ByVal lParam As Long) As Long
Public Declare Function EnumWindows Lib "user32" (ByVal lpEnumFunc As Long, ByVal lParam As Long) As Boolean
Private Declare Function GetWindowText Lib "user32" Alias "GetWindowTextA" (ByVal hWnd As Long, ByVal lpString As String, ByVal cch As Long) As Long
Private Declare Function GetWindowTextLength Lib "user32" Alias "GetWindowTextLengthA" (ByVal hWnd As Long) As Long
'Private Declare Function SendMessage Lib "user32.dll" Alias "SendMessageA" (ByVal hwnd As Long, ByVal wMsg As Long, ByVal wParam As Long, ByRef lParam As Any) As Long
'Private Const WM_CLOSE As Long = &H10&
'枚举顶级窗口
Public Function EnumWindowsProc(ByVal hWnd As Long, ByVal lParam As Long) As Boolean
Dim WindowCaption As String, LengthCaption As Long, WindowClassName As String * 256
LengthCaption = GetWindowTextLength(hWnd)
WindowCaption = Space(LengthCaption)
Call GetWindowText(hWnd, WindowCaption, LengthCaption + 1)
Call GetClassName(hWnd, WindowClassName, 256)
WindowClassName = Left(WindowClassName, InStr(WindowClassName, Chr(0)) - 1)
Form1.List1.AddItem "窗口句柄:" & hWnd & " 窗口标题:" & WindowCaption & "窗口类名:" & WindowClassName
'If InStr(1, WindowCaption, "窗体的标题") > 0 Then
' SendMessage hwnd, WM_CLOSE, 0&, 0& '关闭指定窗体标题的窗体
'End If
EnumWindowsProc = True
End Function
'枚举所有子窗口
Public Function EnumChildWindowsProc(ByVal hWnd As Long, ByVal lParam As Long) As Boolean
Dim WindowCaption As String, LengthCaption As Long, WindowClassName As String * 256
LengthCaption = GetWindowTextLength(hWnd)
WindowCaption = Space(LengthCaption)
Call GetWindowText(hWnd, WindowCaption, LengthCaption + 1)
Call GetClassName(hWnd, WindowClassName, 256)
WindowClassName = Left(WindowClassName, InStr(WindowClassName, Chr(0)) - 1)
Form1.List2.AddItem "子窗口句柄:" & hWnd & " 子窗口标题:" & WindowCaption & " 子窗口类名:" & WindowClassName
'Debug.Print hWnd
'Form1.List2.AddItem hWnd & " " & WindowCaption
EnumChildWindowsProc = True
End Function
'窗体Form1代码
Option Explicit
'窗体上添加一个命令按钮Command1,二个列表框List1,List2
Private Sub Command1_Click()
EnumWindows AddressOf EnumWindowsProc, ByVal 0&
End Sub
Private Sub List1_Click()
Dim p1 As Long, p2 As Long
p1 = InStr(1, List1.List(List1.ListIndex), ":")
p2 = InStr(1, List1.List(List1.ListIndex), "窗口标题")
Debug.Print Mid$(List1.List(List1.ListIndex), p1 + 1, p2 - p1 - 1)
If p2 - p1 > 0 Then EnumChildWindows CLng(Mid$(List1.List(List1.ListIndex), p1 + 1, p2 - p1 - 1)), AddressOf EnumChildWindowsProc, ByVal 0&
End Sub