- Код: Выделить всё
'на форме - textbox, вывод списка заголовков окон - в textbox1.text
Private Declare Function apiGetClassName Lib "user32" Alias "GetClassNameA" (ByVal Hwnd As Long, ByVal lpClassname As String, ByVal nMaxCount As Long) As Long
Private Declare Function apiGetDesktopWindow Lib "user32" Alias "GetDesktopWindow" () As Long
Private Declare Function apiGetWindow Lib "user32" Alias "GetWindow" (ByVal Hwnd As Long, ByVal wCmd As Long) As Long
Private Declare Function apiGetWindowLong Lib "user32" Alias "GetWindowLongA" (ByVal Hwnd As Long, ByVal nIndex As Long) As Long
Private Declare Function apiGetWindowText Lib "user32" Alias "GetWindowTextA" (ByVal Hwnd As Long, ByVal lpString As String, ByVal aint As Long) As Long
Private Const mcGWCHILD = 5
Private Const mcGWHWNDNext = 2
Private Const mcGWLSTYLE = (-16)
Private Const mcWSVISIBLE = &H10000000
Private Const mconMAXLEN = 255
Function fEnumWindows()
Dim lngx As Long, lngLen As Long
Dim lngStyle As Long, strCaption As String
lngx = apiGetDesktopWindow()
'Return the first child To Desktop
lngx = apiGetWindow(lngx, mcGWCHILD)
Do While Not lngx = 0
strCaption = fGetCaption(lngx)
If Len(strCaption) > 0 Then
lngStyle = apiGetWindowLong(lngx, mcGWLSTYLE)
'enum visible windows Only
If lngStyle And mcWSVISIBLE Then
Text1.Text = Text1.Text & "Class = " & fGetClassName(lngx) & " Caption = " & fGetCaption(lngx) & Chr(13) & Chr(10)
End If
End If
lngx = apiGetWindow(lngx, mcGWHWNDNext)
Loop
End Function
Private Function fGetClassName(Hwnd As Long)
Dim strBuffer As String
Dim intCount As Integer
strBuffer = String$(mconMAXLEN - 1, 0)
intCount = apiGetClassName(Hwnd, strBuffer, mconMAXLEN)
If intCount > 0 Then
fGetClassName = Left$(strBuffer, intCount)
End If
End Function
Private Function fGetCaption(Hwnd As Long)
Dim strBuffer As String
Dim intCount As Integer
strBuffer = String$(mconMAXLEN - 1, 0)
intCount = apiGetWindowText(Hwnd, strBuffer, mconMAXLEN)
If intCount > 0 Then
fGetCaption = Left$(strBuffer, intCount)
End If
End Function
Private Sub Form_Load()
Call fEnumWindows
End Sub
мне нужно обойти апи, для того чтобы использовать код в vbscript, для этого я использую dynacall.dll, ссылку на которую я нашел на этом форуме.
код с использованием этой библиотеки преобразуется в следующий:
- Код: Выделить всё
Private Const mcGWCHILD = 5
Private Const mcGWHWNDNext = 2
Private Const mcGWLSTYLE = (-16)
Private Const mcWSVISIBLE = &H10000000
Private Const mconMAXLEN = 255
Dim uw, s, c
Function fEnumWindows()
Set uw = CreateObject("DynamicWrapper")
uw.Register "USER32.DLL", "GetClassNameA", "I=HSL", "R=L"
uw.Register "USER32.DLL", "GetDesktopWindow", "R=L"
uw.Register "USER32.DLL", "GetWindow", "I=HL", "R=L"
uw.Register "USER32.DLL", "GetWindowLongA", "I=hl", "R=L"
uw.Register "USER32.DLL", "GetWindowTextA", "I=HSL", "R=L"
Dim lngx As Long, lngLen As Long
Dim lngStyle As Long, strCaption As String
lngx = uw.GetDesktopWindow()
'Return the first child To Desktop
lngx = uw.GetWindow(lngx, mcGWCHILD)
Do While Not lngx = 0
strCaption = fGetCaption(lngx)
If Len(strCaption) > 0 Then
lngStyle = uw.GetWindowLongA(lngx, mcGWLSTYLE)
'enum visible windows Only
If lngStyle And mcWSVISIBLE Then
s = fGetClassName(lngx)
c = fGetCaption(lngx)
Text1.Text = Text1.Text & s & " " & c & Chr(13) & Chr(10)
End If
End If
lngx = uw.GetWindow(lngx, mcGWHWNDNext)
Loop
End Function
Private Function fGetClassName(Hwnd As Long)
Dim strBuffer As String
Dim intCount As Integer
strBuffer = String$(mconMAXLEN - 1, 0)
intCount = uw.GetClassNameA(Hwnd, strBuffer, mconMAXLEN)
If intCount > 0 Then
fGetClassName = Left$(strBuffer, intCount)
End If
End Function
Private Function fGetCaption(Hwnd As Long)
Dim strBuffer As String
Dim intCount As Integer
On Error Resume Next
strBuffer = String$(mconMAXLEN - 1, 0)
intCount = uw.GetWindowTextA(Hwnd, strBuffer, mconMAXLEN)
If intCount > 0 Then
fGetCaption = Left$(strBuffer, intCount)
End If
End Function
Private Sub Form_Load()
fEnumWindows
End Sub
Почему то у меня этот код не заполняет текстбох заголовками окон. Может кто-то использовал эту dll-ку?