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

我用的是vb.net 2005,我想做个自动填写网页表单的程序(从数据库中读取数据填写),但要判断相关条件。现在有个问题,当一个证件号码已经存在时,网页会用alert弹出提示;而当这个号码错误时,内容又不一样,所以我想问下要怎样得到网页弹出的对话框的内容?然后根据内容执行下一步操作?
简单的举个例子吧。
<script>
if id='123' then
alert('重复号码')
end if

if id='234' then
alert('错误的号码')
end if
</script>

然后我的程序用的webbrower控件,打开这个网页后,怎么获取到这其中的alert的内容呢?
恳请指教,非常感谢。
(我用过有个SysExporter的程序可以做到,但不知道原理,大家可以参考下)
无满意答案的悬赏分竟然不返还,那不是浪费了?谁给我随便说点,我给你们罗~~

'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
<DllImport("User32.dll", CharSet:=CharSet.Auto, Entrypoint:="SendMessage")> _
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

参考资料:http://forums.microsoft.com/china

温馨提示:答案为网友推荐,仅供参考
第1个回答  2008-12-08
好难啊,不会~
相似回答