vb.net获取网页中对话框的内容

2026年09月11日 14:31
有2个网友回答
网友(1):

'api constant declarations...
Const WM_GETTEXT As Long = &HD
Const WM_GETTEXTLENGTH As Long = &HE
Const GW_ENABLEDPOPUP As Long = 6
Const BM_CLICK As Long = &HF5&
Const GW_CHILD As Long = 5
Const GW_HWNDNEXT As Long = 2

'function to retrieve the popup window associated with the form, as well as to find the child windows of the popup...
Private Declare Function GetWindow Lib "user32.dll" (ByVal hWnd As IntPtr, ByVal uCmd As Integer) As IntPtr

Public Declare Function FindWindow Lib "user32" Alias "FindWindowA" (ByVal lpClassName As String, ByVal lpWindowName As String) As Integer

'sendmessage overload that is used to send messages to the button on the dialog window...
Private Declare Function SendMessage Lib "user32.dll" Alias "SendMessage" (ByVal hWnd As IntPtr, ByVal Msg As Integer, _
ByVal wParam As Integer, ByRef lParam As IntPtr) As IntPtr

'sendmessage overloads used to retrieve the window text...
Private Declare Function SendMessageA Lib "user32.dll" Alias "SendMessageA" (ByVal hWnd As IntPtr, ByVal Msg As Integer, _
ByVal wParam As IntPtr, ByRef lParam As IntPtr) As IntPtr
_
Public Shared Function SendMessageString(ByVal hwnd As IntPtr, _
ByVal wMsg As Integer, ByVal wparam As Integer, ByVal lparam As System.Text.StringBuilder) As IntPtr
End Function

'these next three functions are used to enumerate the Windows handles of all Windows sited on the specified parent...
Function GetChildWindowHandles(ByVal ParentWindowHandle As IntPtr) As ArrayList

Dim b As Boolean
Dim ptrChild As IntPtr
Dim clsRet As New ArrayList

'get first child handle...
ptrChild = GetChildWindowHandle(ParentWindowHandle)

Do Until ptrChild.Equals(IntPtr.Zero)
'add to collection of handles...
clsRet.Add(ptrChild)
'get next child...
ptrChild = GetNextWindowHandle(ptrChild)
Loop

'return...
Return clsRet

End Function

Function GetChildWindowHandle(ByVal ParentWindowHandle As IntPtr) As IntPtr
Return GetWindow(ParentWindowHandle, GW_CHILD)
End Function

Function GetNextWindowHandle(ByVal CurrentWindowhandle As IntPtr) As IntPtr
Return GetWindow(CurrentWindowhandle, GW_HWNDNEXT)
End Function

'this function returns the text of the window, used so that we can confirm that we have the right dialog window...
Function GetWindowText(ByVal WindowHandle As IntPtr) As String

Dim ptrRet As IntPtr
Dim ptrLength As IntPtr

'get length for buffer...
ptrLength = SendMessageA(WindowHandle, WM_GETTEXTLENGTH, IntPtr.Zero, IntPtr.Zero)

'create buffer for return value...
Dim sb As New System.Text.StringBuilder(ptrLength.ToInt32 + 1)

'get window text...
ptrRet = SendMessageString(WindowHandle, WM_GETTEXT, ptrLength.ToInt32 + 1, sb)

'get return value...
Return sb.ToString

End Function

Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click

Dim ptrDialogWindow As IntPtr = FindWindow(vbNullString, "Microsoft Internet Explorer")
Dim clsChildHandles As ArrayList = GetChildWindowHandles(ptrDialogWindow)

For Each ptrHandle As IntPtr In clsChildHandles
Dim str As String = GetWindowText(ptrHandle)
If str <> "" And str <> "确定" Then
MsgBox(str)
End If

Next

End Sub

网友(2):

好难啊,不会~