最近有个项目要在winfroms 中显示web页面
一。。 AxWebBrowser 和 WebBrowser
.Net已有的不支持javascript 的window.open打开在新窗口会在IE打开, 所以选老的 COM控件AxWebBrowser
使用参考 http://support.microsoft.com/kb/311290
创建示例
本节介绍如何承载 WebBrowser 控件在 Visual Basic.NET 应用程序中的、 如何处理 NewWindow2 事件,在 WebBrowser 控件的以及然后如何处理 WindowSetWidth 与 WindowSetHeight 事件调整您的应用程序。
- 如下所示在 Visual Basic 中创建新的 Windows 应用程序:
- 启动 Visual Studio.NET。
- 在 文件 菜单上指向 新建 ,然后单击 项目 。
- 在 项目类型 ,下单击 Visual Basic 项目 。 在 模板 ,下单击 Windows 应用程序 。
- 在工具箱中,单击 常规 ,右键单击,将的工具箱,然后单击 自定义工具箱 。
- 在 COM 组件 选项卡上选中 Microsoft Web 浏览器 复选框,然后单击 确定 。
- 在工具箱,双击在资源管理器窗口。
- 向窗体,请添加一个 Button 控件和一个 TextBox 控件。
- 双击若要查看到代码窗口中的按钮的 onClick 事件的在实现该按钮,然后添加以下代码: 此代码允许您浏览到您在文本框中指定的 URL。
Private Sub Button1_Click(ByVal sender As System.Object, _ ByVal e As System.EventArgs) Handles Button1.Click AxWebBrowser1.Navigate(TextBox1.Text) End Sub
- 添加以下代码为 NewWindow2 编写处理程序函数:
Private Sub AxWebBrowser1_NewWindow2(ByVal sender As Object, _ ByVal e As AxSHDocVw.DWebBrowserEvents2_NewWindow2Event) _ Handles AxWebBrowser1.NewWindow2 Dim frmWB As Form1 frmWB = New Form1() frmWB.AxWebBrowser1.RegisterAsBrowser = True e.ppDisp = frmWB.AxWebBrowser1.Application frmWB.Visible = True End Sub
- 添加以下代码为 WindowSetHeight 编写处理程序函数:
Private Sub AxWebBrowser1_WindowSetHeight(ByVal sender As Object, _ ByVal e As AxSHDocVw.DWebBrowserEvents2_WindowSetHeightEvent) _ Handles AxWebBrowser1.WindowSetHeight Dim heightDiff As Integer heightDiff = Me.Height - Me.AxWebBrowser1.Height Me.Height = heightDiff + e.height End Sub
- 添加以下代码为 WindowSetWidth 编写处理程序函数:
Private Sub AxWebBrowser1_WindowSetWidth(ByVal sender As Object, _ ByVal e As AxSHDocVw.DWebBrowserEvents2_WindowSetWidthEvent) _ Handles AxWebBrowser1.WindowSetWidth Dim widthDiff As Integer widthDiff = Me.Width - Me.AxWebBrowser1.Width Me.Width = widthDiff + e.width End Sub
完成代码示例
Public Class Form1 Inherits System.Windows.Forms.Form#Region " Windows Form Designer generated code " 'Omitted#End Region Private Sub Button1_Click(ByVal sender As System.Object, _ ByVal e As System.EventArgs) Handles Button1.Click AxWebBrowser1.Navigate(TextBox1.Text) End Sub Private Sub AxWebBrowser1_NewWindow2(ByVal sender As Object, _ ByVal e As AxSHDocVw.DWebBrowserEvents2_NewWindow2Event) _ Handles AxWebBrowser1.NewWindow2 'MessageBox.Show(AxWebBrowser1.Height & ":" & AxWebBrowser1.Width) 'MessageBox.Show(doc.body.innerHTML) Dim frmWB As Form1 frmWB = New Form1() frmWB.AxWebBrowser1.RegisterAsBrowser = True 'frmWB.AxWebBrowser1.Navigate2("about:blank") e.ppDisp = frmWB.AxWebBrowser1.Application frmWB.Visible = True 'MessageBox.Show(frmWB.AxWebBrowser1.Height & ":" & frmWB.AxWebBrowser1.Width) End Sub Private Sub AxWebBrowser1_WindowSetHeight(ByVal sender As Object, _ ByVal e As AxSHDocVw.DWebBrowserEvents2_WindowSetHeightEvent) _ Handles AxWebBrowser1.WindowSetHeight 'MessageBox.Show("In SetHeight" & Me.Height & ":" & e.height) Dim heightDiff As Integer heightDiff = Me.Height - Me.AxWebBrowser1.Height Me.Height = heightDiff + e.height End Sub Private Sub AxWebBrowser1_WindowSetWidth(ByVal sender As Object, _ ByVal e As AxSHDocVw.DWebBrowserEvents2_WindowSetWidthEvent) _ Handles AxWebBrowser1.WindowSetWidth 'MessageBox.Show("In SetWidth" & Me.Width & ":" & e.width) Dim widthDiff As Integer widthDiff = Me.Width - Me.AxWebBrowser1.Width Me.Width = widthDiff + e.width End SubEnd Class 验证在工作的
- 生成应用程序。
- 打开记事本。 创建一个名为 Test.htm 的新文件,然后添加以下代码:
<HTML><HEAD><META NAME="GENERATOR" Content="Microsoft Visual Studio 6.0"><TITLE></TITLE><script> function openWin() { var win; win = window.open("http://www.microsoft.com","blah","width=600, height=600"); }</script></HEAD><BODY><button onClick=openWin()>Open Window</button></BODY></HTML> - 将 test.htm 保存您的 Web 服务器上中。
- 运行该应用程序。
- 浏览到该 test.htm 页,然后单击按钮。 注意 Microsoft 公司 Web 站点打开应用程序的新实例中。 在窗体根据会调整通过 window.open 调用传递的功能。
二 。。 WebBrowser支持web form的提交submit , 但AxWebBrowser 不支持
自己扩展
以下是VB版扩展class和实例。
http://www.vbforums.com/archive/index.php/t-494278.html
I'm trying to build a reusable class for WebPage Manipulation in Vb.NET. I've used Kleinmas great code to base the class I'm posting here. My goal is to show you the class and allow you to build on it so you can simply copy/paste the class into your own project to use. As well, hopefully get some ideas from others on the functions/procedures available in the class.
If you think I should add a function/procedure, please just post here and I will try to edit the code below to reflect those changes.
This work was derived from Kleinma's work located in this thread:
http://www.vbforums.com/showthread.php?t=416275
You will need to add a few references before using the classes. Here are the steps to add the references:
In your Visual Studio 2005 Solution, we need to add the WebBrowser
control to the Toolbox, in Windows Forms, right click and select "Add
new item...", and select "COM components" tab. Here select "Microsoft
Web Browser". This will add this control to the Toolbox and generate two
Interop in your references: AxSHDocVw and SHDocVw.
You will also need to add a reference to Microsoft.mshtml. In your
vb.net project right-click the references folder and choose to add a
new reference. Choose the Microsoft.mshtml and add the reference.
Here is a couple of examples to use the class to get the source of a webpage and to fill out a simple webform. After adding a AxWebBrowser control and navigating to a page, you can do this: (make sure you rplace the Username, Password, SubmitButton names with their real names on the webpage you are trying to submit to.)
'declaration
dim wpM as new clsWebPageManipulation
Private Sub Form1_Load(byval yadadada) handles mybase.load
AxWebBrowser1.Navigate("http://www.vbforums.com")
Exit Sub
Private Sub AxWebBrowser1_DocumentComplete(ByVal sender As Object, ByVal e As AxSHDocVw.DWebBrowserEvents2_DocumentCompleteEvent) Handles AxWebBrowser1.DocumentComplete
dim strSource as string = wpM.GetSource(AxWebBrowser1)
console.writeLine(strSource)
End Sub
Private Sub FillFormAndClickSubmit
wpM.SetTextboxText(AxWebBrowser1, "loginform", "RickyH", "txtUserName")
wpM.SetTextboxText(AxWebBrowser1, "loginform", "myStrongPassword", "txtPassword")
wpM.ClickSubmitButton(AxWebBrowser1, "loginform", "LoginButtonName")
End Sub
See attached picture of the current functions available in the class.
Here is the class that you can store separately in your project:
Public Class clsWebPageManipulation
Friend Function GetCurrentWebDoc(ByVal wb As AxSHDocVw.AxWebBrowser) As mshtml.HTMLDocument
Try
Return DirectCast(wb.Document, mshtml.HTMLDocument)
Catch ex As Exception
Return Nothing
End Try
End Function
Friend Function GetCurrentWebForm(ByVal wb As AxSHDocVw.AxWebBrowser, ByVal formName As String) As mshtml.HTMLFormElement
Try
If GetCurrentWebDoc(wb).forms.length > 0 Then
Return DirectCast(GetCurrentWebDoc(wb).forms.item(formName), mshtml.HTMLFormElement)
Else
Return Nothing
End If
Catch ex As Exception
Return Nothing
End Try
End Function
Friend Function TextExists(ByVal Text As String, ByVal wb As AxSHDocVw.AxWebBrowser) As Boolean
Dim mydoc As mshtml.HTMLDocument = GetCurrentWebDoc(wb)
Dim MyRange As mshtml.IHTMLTxtRange = Nothing
MyRange = DirectCast(mydoc.selection().createRange, mshtml.IHTMLTxtRange)
If MyRange.findText(Text) Then
Return True
Else
Return False
End If
End Function
Friend Function ShowText(ByVal Text As String, ByVal wb As AxSHDocVw.AxWebBrowser) As Boolean
Dim mydoc As mshtml.HTMLDocument = GetCurrentWebDoc(wb)
Dim MyRange As mshtml.IHTMLTxtRange = Nothing
MyRange = DirectCast(mydoc.selection().createRange, mshtml.IHTMLTxtRange)
If MyRange.findText(Text) Then
Return True
Else
Return False
End If
End Function
Friend Sub SetTextboxText(ByVal wb As AxSHDocVw.AxWebBrowser, ByVal strFormName As String, ByVal TextToType As String, ByVal txtBoxName As String)
DirectCast(GetCurrentWebForm(wb, strFormName).item(txtBoxName), mshtml.HTMLInputElement).value = TextToType
End Sub
'CLICKS THE REGULAR HTML BUTTON (WHICH CALLS A JAVASCRIPT IN THE HTML PAGE TO SHOW THAT IT WORKED)
Friend Sub ClickNormalButton(ByVal wB As AxSHDocVw.AxWebBrowser, ByVal strFormName As String, ByVal strButtonName As String)
DirectCast(GetCurrentWebForm(wB, strFormName).item(strButtonName), mshtml.HTMLButtonElement).click()
End Sub
'THERE ARE 2 WAYS TO SUBMIT A FORM
Friend Sub ClickSubmitButton(ByVal wB As AxSHDocVw.AxWebBrowser, ByVal strFormName As String, ByVal strButtonName As String)
DirectCast(GetCurrentWebForm(wB, strFormName).item(strButtonName), mshtml.HTMLButtonElement).click()
Return 'so we don't call the below method
'THE OTHER WAY IS HERE, HOWEVER IT WILL BYPASS ANY JAVASCRIPT OR OTHER FUNCTIONALITY
'THAT MAY BE ATTACHED TO THE BUTTON'S CLICK, WHICH IS WHY THE ABOVE METHOS IS BETTER
'FOR ALMOST ALL SITUATIONS
GetCurrentWebForm(wB, strFormName).submit()
End Sub
Friend Sub ClickImageButton(ByVal wb As AxSHDocVw.AxWebBrowser, ByVal strImageButtonName As String)
Dim MyImageElement As mshtml.HTMLInputElement = _
DirectCast(GetCurrentWebDoc(wb).all.item(strImageButtonName), mshtml.HTMLInputElement)
MyImageElement.click()
End Sub
Friend Function TextSourceExists(ByVal wb As AxSHDocVw.AxWebBrowser, ByVal TextToFind As String) As Boolean
Try
TextToFind = TextToFind.ToLower
If GetCurrentWebDoc(wb).documentElement.outerHTML.ToString.ToLower.Contains(TextToFind) = True Then
Return True
Else
Return False
End If
Catch ex As Exception
Return False
End Try
End Function
Friend Function TextExistsInUrl(ByVal wB As AxSHDocVw.AxWebBrowser, ByVal TextToFind As String) As Boolean
Try
If GetCurrentWebDoc(wB).url.ToString.Contains(TextToFind) = True Then
Return True
Else
Return False
End If
Catch ex As Exception
Return False
End Try
End Function
Friend Function GetSource(ByVal wb As AxSHDocVw.AxWebBrowser) As String
Try
Dim str As String = ""
str = GetCurrentWebDoc(wb).documentElement.outerHTML.ToString
Return str
Catch ex As Exception
Return ""
End Try
End Function
Friend Sub SetTextareaText(ByVal wB As AxSHDocVw.AxWebBrowser, ByVal strFormName As String, ByVal strTextAreaName As String, ByVal strTextToType As String)
DirectCast(GetCurrentWebForm(wB, strFormName).item(strTextAreaName), mshtml.HTMLTextAreaElement).value = strTextToType
End Sub
Friend Function GetTextAreaData(ByVal wb As AxSHDocVw.AxWebBrowser, ByVal strFormName As String, ByVal strTextAreaBoxName As String)
Try
If DirectCast(GetCurrentWebForm(wb, strFormName).item(strTextAreaBoxName), mshtml.HTMLTextAreaElement).value <> "" Then
Return True
Else
Return False
End If
Catch ex As Exception
Return False
End Try
End Function
Friend Function GetTextBoxData(ByVal wb As AxSHDocVw.AxWebBrowser, ByVal strFormName As String, ByVal strTextBoxName As String)
Try
If DirectCast(GetCurrentWebForm(wb, strFormName).item(strTextBoxName), mshtml.HTMLInputElement).value <> "" Then
Return True
Else
Return False
End If
Catch ex As Exception
Return False
End Try
End Function
Friend Function GetUrl(ByVal wb As AxSHDocVw.AxWebBrowser) As String
Try
Return GetCurrentWebDoc(wb).url.ToString
Catch ex As Exception
Return ""
End Try
End Function
Friend Function GetWebFrameObject(ByVal PassDocumentObject As Object) As mshtml.HTMLWindow2
Try
Return DirectCast(PassDocumentObject, mshtml.HTMLWindow2)
Catch ex As Exception
Return Nothing
End Try
End Function
Friend Function GetWebForm(ByVal objDoc As mshtml.HTMLDocument) As mshtml.HTMLFormElement
Try
Return DirectCast(objDoc.forms.item(0), mshtml.HTMLFormElement)
Catch ex As Exception
Return Nothing
End Try
End Function
Friend Sub SetFrameTextBox(ByVal wB As AxSHDocVw.AxWebBrowser, ByVal FrameNumber As Integer, ByVal ValueToSendToBox As String, ByVal FieldToFill As String)
DirectCast(GetWebForm(GetWebFrameObject(GetCurrentWebDoc(wB).frames.item(FrameNumber)).document).ite m(FieldToFill, 0), mshtml.HTMLInputElement).value = ValueToSendToBox
End Sub
Friend Sub clickSubmitButtonFrames(ByVal wB As AxSHDocVw.AxWebBrowser, ByVal FrameNumber As Integer, ByVal FieldToFill As String)
DirectCast(GetWebForm(GetWebFrameObject(GetCurrentWebDoc(wB).frames.item(FrameNumber)).document).ite m(FieldToFill, 0), mshtml.HTMLButtonElement).click()
End Sub
End Class
C#版提交form部分
class clsWebPageManipulation
{
public mshtml.HTMLDocument GetCurrentWebDoc(AxSHDocVw.AxWebBrowser wb)
{
try
{
return (mshtml.HTMLDocument)wb.Document;
}
catch
{
return null;
}
}
public mshtml.HTMLFormElement GetCurrentWebForm(AxSHDocVw.AxWebBrowser wb, String formName)
{
try
{
if (GetCurrentWebDoc(wb).forms.length > 0)
{
return (mshtml.HTMLFormElement)GetCurrentWebDoc(wb).forms.item(formName,0);
}
else
{
return null;
}
}
catch
{
return null;
}
}
public Boolean TextExists(String Text, AxSHDocVw.AxWebBrowser wb)
{
mshtml.HTMLDocument mydoc = GetCurrentWebDoc(wb);
mshtml.IHTMLTxtRange MyRange = null;
MyRange = (mshtml.IHTMLTxtRange)(mydoc.selection.createRange());
if (MyRange.findText(Text,1,2))
{
return true;
}
else
{
return false;
}
}
public Boolean ShowText(String Text, AxSHDocVw.AxWebBrowser wb)
{
mshtml.HTMLDocument mydoc = GetCurrentWebDoc(wb);
mshtml.IHTMLTxtRange MyRange = null;
MyRange = (mshtml.IHTMLTxtRange)(mydoc.selection.createRange());
if (MyRange.findText(Text, 1, 2))
{
return true;
}
else
{
return false;
}
}
public void SetTextboxText(AxSHDocVw.AxWebBrowser wb,String strFormName,String TextToType,String txtBoxName)
{
mshtml.HTMLInputElement element = (mshtml.HTMLInputElement)GetCurrentWebForm(wb, strFormName).item(txtBoxName, 0);
element.value = TextToType;
}
//CLICKS THE REGULAR HTML BUTTON (WHICH CALLS A JAVASCRIPT IN THE HTML PAGE TO SHOW THAT IT WORKED)
public void ClickNormalButton(AxSHDocVw.AxWebBrowser wB, String strFormName,String strButtonName)
{
mshtml.HTMLButtonElement button = (mshtml.HTMLButtonElement)GetCurrentWebForm(wB, strFormName).item(strButtonName,0);
button.click();
}
//THERE ARE 2 WAYS TO SUBMIT A FORM
public void ClickSubmitButton(AxSHDocVw.AxWebBrowser wB,String strFormName,String strButtonName )
{
mshtml.HTMLButtonElement button = (mshtml.HTMLButtonElement)GetCurrentWebForm(wB, strFormName).item(strButtonName, 0);
button.click();
}
public void ClickSubmitButtonSubmitForm(AxSHDocVw.AxWebBrowser wB, String strFormName)
{
GetCurrentWebForm(wB, strFormName).submit();
}
回到顶端