日期:2014-05-17 浏览次数:20947 次
//项目中添加Micrsoft.mshtml引用 //Begin temp.htm------- <html> <head> <title>demo</title> <script language="JavaScript" type="text/javascript"> var testText = "Zswang"; function ShowMessage(AText) { alert(testText); alert(AText); } </script> </head> </html> //End temp.htm------- using mshtml; using System.Reflection; private void button1_Click(object sender, EventArgs e) { IHTMLDocument2 vDocument = webBrowser1.Document.DomDocument as IHTMLDocument2; IHTMLWindow2 vWindow = (IHTMLWindow2)vDocument.parentWindow; Type vWindowType = vWindow.GetType(); object testText = vWindowType.InvokeMember("testText", BindingFlags.GetProperty, null, vWindow, new object[] { }); // 读取 Console.WriteLine(testText); vWindowType.InvokeMember("testText", BindingFlags.SetProperty, null, vWindow, new object[] { "Zswang 路过" }); // 设置 vWindowType.InvokeMember("ShowMessage", BindingFlags.InvokeMethod, null, vWindow, new object[] { 12345 }); // 执行方法 } private void button2_Click(object sender, EventArgs e) { IHTMLDocument2 vDocument = webBrowser1.Document.DomDocument as IHTMLDocument2; IHTMLWindow2 vWindow = (IHTMLWindow2)vDocument.parentWindow; vWindow.execScript("ShowMessage(67890);", "JavaScript"); // 执行脚本 }