[C#] WinForm通过webbrower完成调用网页中JS →→→→→进入此内容的聊天室

来自 , 2021-03-07, 写在 C#, 查看 148 次.
URL http://www.code666.cn/view/dbd22ba3
  1. //项目中添加Micrsoft.mshtml引用
  2. //Begin temp.htm-------
  3. <html>
  4. <head>
  5. <title>demo</title>
  6. <script language="JavaScript" type="text/javascript">
  7. var testText = "Zswang";
  8. function ShowMessage(AText)
  9. {
  10.     alert(testText);
  11.     alert(AText);
  12. }
  13. </script>
  14. </head>
  15. </html>
  16. //End temp.htm-------
  17.  
  18. using mshtml;
  19. using System.Reflection;
  20.  
  21. private void button1_Click(object sender, EventArgs e)
  22. {
  23.     IHTMLDocument2 vDocument = webBrowser1.Document.DomDocument as IHTMLDocument2;
  24.     IHTMLWindow2 vWindow = (IHTMLWindow2)vDocument.parentWindow;
  25.     Type vWindowType = vWindow.GetType();
  26.     object testText = vWindowType.InvokeMember("testText",
  27.         BindingFlags.GetProperty, null, vWindow, new object[] { }); // 读取
  28.     Console.WriteLine(testText);
  29.     vWindowType.InvokeMember("testText",
  30.         BindingFlags.SetProperty, null, vWindow, new object[] { "stephen.kang 路过" }); // 设置
  31.     vWindowType.InvokeMember("ShowMessage",
  32.         BindingFlags.InvokeMethod, null, vWindow, new object[] { 12345 }); // 执行方法
  33. }
  34.  
  35. private void button2_Click(object sender, EventArgs e)
  36. {
  37.     IHTMLDocument2 vDocument = webBrowser1.Document.DomDocument as IHTMLDocument2;
  38.     IHTMLWindow2 vWindow = (IHTMLWindow2)vDocument.parentWindow;
  39.     vWindow.execScript("ShowMessage(67890);", "JavaScript"); // 执行脚本
  40. }
  41.  
  42.  
  43.  
  44.  
  45.  
  46.  
  47.     目前已经有最新的版本,不同于以上方法制作的工具:
  48.     见我的博客:http://www.cnblogs.com/axing/archive/2012/10/29/stephen_kang_WinForm.html

回复 "WinForm通过webbrower完成调用网页中JS"

这儿你可以回复上面这条便签

captcha