1、确保组件对象模型(COM)必须能够访问脚本对象:即使窗体对 COM 可见,将 ComVisibleAttribute 属性添加到窗体类中: 在窗体的构造函数中设置 <System.Runtime.InteropServices.ComVisibleAttribute(True)> 例: <Global.Microsoft.VisualBasic.CompilerServices.DesignerGenerated()> _ <System.Runtime.InteropServices.ComVisibleAttribute(True)> _ Partial Class FormShow 2、窗体启动时,设置 webBrowser1.ObjectForScripting = me 3、确保要调用的函数为 public
以下是窗体完整代码: Public Class Form1 Private Sub Form1_Load(ByVal sender As System.Object,ByVal e As System.EventArgs) Handles MyBase.Load WebBrowser1.ObjectForScripting = Me End Sub
Private Sub Button1_Click(ByVal sender As System.Object,ByVal e As System.EventArgs) Handles Button1.Click Dim innerstr As String = "" innerstr = "<html><head><style>" & ControlChars.CrLf innerstr &= "body {font-size:12pt;}" & ControlChars.CrLf innerstr &= "</style>" & ControlChars.CrLf innerstr &= "<script language='vbscript'>" & ControlChars.CrLf innerstr &= "sub clickit()" & ControlChars.CrLf innerstr &= "ceshidiv.innertext=""zheshiyigeceshi""" & ControlChars.CrLf innerstr &= "end sub" & ControlChars.CrLf innerstr &= "sub testit(testindex)" & ControlChars.CrLf innerstr &= "window.external.testit(testindex)" & ControlChars.CrLf innerstr &= "end sub" & ControlChars.CrLf innerstr &= "</script>" & ControlChars.CrLf innerstr &= "</head><body>" & ControlChars.CrLf innerstr &= "<div id='ceshidiv'> 这是一个测试</div>" & ControlChars.CrLf innerstr &= "<input type='button' value='ceshi1' name='btn1' onclick='clickit'>" & ControlChars.CrLf innerstr &= "<input type='button' value='ceshi2' name='btn2' onclick='testit(1)'>" & ControlChars.CrLf innerstr &= "</body></html>" WebBrowser1.DocumentText = innerstr End Sub
Public Sub testit(ByVal curpage As Integer) MsgBox("这是winform测试:" & curpage) End Sub End Class
4、其他在设计时候需要注意的:webBrowser1.AllowWebBrowserDrop = false '禁止拖放文件 WebBrowser 控件到其上webBrowser1.IsWebBrowserContextMenuEnabled = false '禁止右击时显示快捷菜单(同ie中右键菜单)webBrowser1.WebBrowserShortcutsEnabled = false '禁止控件响应快捷键webBrowser1.ScriptErrorsSuppressed=true'禁止显示脚本代码问题的错误信息。 (编辑:李大同)
【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!
|