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

tangzhong 2008-12-09 11:09:06
我用的是vb.net 2005,我想做个自动填写网页表单的程序(从数据库中读取数据填写),但要判断相关条件。现在有个问题,当一个证件号码已经存在时,网页会用alert弹出提示;而当这个号码错误时,内容又不一样,所以我想问下要怎样得到网页弹出的对话框的内容?然后根据内容执行下一步操作?
简单的举个例子吧。
<script>
function CheckForm()
{
if(document.f.title.value.length<2)
{
alert("请输入留言主题!");
document.f.title.focus();
return false;
}
if(document.f.content.value.length<10)
{
alert("请认真输入留言内容!");
document.f.content.focus();
return false;
}
}
</script>

我的程序用的webbrower控件,打开这个网页后,怎么获取到这其中的alert的内容呢? 注意,当我填写正确时,就进行下一步操作;如果错了,就要根据这个alert的内容进行对应的处理。不知道怎么得到?
有一个思路是枚举所有窗体,得到这个alert的句柄再得到其内容,好像有个getdlgtext,但我不知道用,恳请指点,非常感谢。
(我用过有个SysExporter的程序可以做到,但不知道原理,大家可以参考下)
...全文
338 6 打赏 收藏 转发到动态 举报
写回复
用AI写文章
6 条回复
切换为时间正序
请发表友善的回复…
发表回复
tangzhong 2008-12-11
  • 打赏
  • 举报
回复
我自己从msdn论坛找到答案了,公布给有需要的人:
'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
tangzhong 2008-12-10
  • 打赏
  • 举报
回复
谢谢你帮我顶,但我还是希望能找到答案
josephSC 2008-12-10
  • 打赏
  • 举报
回复
可否把alert里的值变成function,然后后台调用前台代码啊?
mohongmao 2008-12-09
  • 打赏
  • 举报
回复
帮你顶上去………………
tangzhong 2008-12-09
  • 打赏
  • 举报
回复
非常感谢你的关注,但你可能弄错我的意思了
我是说在我的VB.NET程序中获取这个alert的内容,也就是用我自己写的程序打开这个页面后,有可能触发这个alert,那么我的程序要捕获这个消息框的内容,问怎么得到?
希望大家帮我下,谢谢~~
g1y9d861213 2008-12-09
  • 打赏
  • 举报
回复
<script>
function CheckForm()
{
if(document.f.title.value.length <2)
{
alert("请输入留言主题!");
document.f.title.focus();
return false;
}
if(document.f.content.value.length <10)
{
alert("请认真输入留言内容!");
document.f.content.focus();
return false;
}
}
</script>
看看下面的行吗??len(str)获取字符串的长度
if (len(textbox1.text)<2) then
response.write("<script>alert('请输入留言主题');</script>")
exit sub
else
if(len(textbox1.text)<10) then
response.write("<script>alert('请认真输入留言内容');</script>")
exit sub
end if
end if

16,717

社区成员

发帖
与我相关
我的任务
社区描述
VB技术相关讨论,主要为经典vb,即VB6.0
社区管理员
  • VB.NET
  • 水哥阿乐
  • 无·法
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧