WebBrowser 高级扩展 js扩展 js订阅C#事件
2008-10-15 12:17
429 查看
/* /r:"C:\Program Files\Microsoft.NET\Primary Interop Assemblies\Microsoft.mshtml.dll" */ namespace Test { using Microshaoft; using System; using System.Reflection; using System.Windows.Forms; using System.Linq; public class Form1 : Form { [STAThread] static void Main() { Application.EnableVisualStyles(); Application.SetCompatibleTextRenderingDefault(false); Application.Run(new Form1()); } private System.ComponentModel.IContainer components = null; /// <summary> /// Clean up any resources being used. /// </summary> /// <param name="disposing">true if managed resources should be disposed; otherwise, false.</param> protected override void Dispose(bool disposing) { if (disposing && (components != null)) { components.Dispose(); } base.Dispose(disposing); } #region Windows Form Designer generated code /// <summary> /// Required method for Designer support - do not modify /// the contents of this method with the code editor. /// </summary> private void InitializeComponent() { this.webBrowser1 = new System.Windows.Forms.WebBrowser(); this.button1 = new System.Windows.Forms.Button(); this.SuspendLayout(); // // webBrowser1 // this.webBrowser1.Location = new System.Drawing.Point(58, 60); this.webBrowser1.MinimumSize = new System.Drawing.Size(20, 20); this.webBrowser1.Name = "webBrowser1"; this.webBrowser1.Size = new System.Drawing.Size(199, 163); this.webBrowser1.TabIndex = 0; this.webBrowser1.Dock = DockStyle.Bottom; // // button1 // this.button1.Location = new System.Drawing.Point(105, 4); this.button1.Name = "button1"; this.button1.Size = new System.Drawing.Size(200, 27); this.button1.TabIndex = 1; this.button1.Text = "第二步: C# Invoke Script"; this.button1.UseVisualStyleBackColor = true; this.button1.Click += new System.EventHandler(this.button1_Click); // // Form1 // this.AutoScaleDimensions = new System.Drawing.SizeF(6F, 12F); this.AutoScaleMode = System.Windows.Forms.AutoScaleMode.Font; this.ClientSize = new System.Drawing.Size(292, 273); this.Controls.Add(this.button1); this.Controls.Add(this.webBrowser1); this.Name = "Form1"; this.Text = "Form1"; this.Load += new System.EventHandler(this.Form1_Load); this.ResumeLayout(false); } #endregion private System.Windows.Forms.WebBrowser webBrowser1; private System.Windows.Forms.Button button1; public Form1() { InitializeComponent(); } private DocumentScriptingObject _dso; private void Form1_Load(object sender, EventArgs e) { this._dso = new DocumentScriptingObject(); this.webBrowser1.ObjectForScripting = _dso; //HTML 在最下面 string s = @"C:\Documents and Settings\v-xiyu\桌面\mystock.gadget\mystock.gadget\index.html"; s = AppDomain.CurrentDomain.BaseDirectory + @"SimpleWbJsBridge.html"; //s = @"D:\MyC#\WebBrowser.FSO.OnExternalEvent\SimpleWbJsBridge.html"; this.webBrowser1.Navigate ( s ); webBrowser1.Navigated += new WebBrowserNavigatedEventHandler(webBrowser1_Navigated); } void webBrowser1_Navigated(object sender, WebBrowserNavigatedEventArgs e) { WebBrowser wb = sender as WebBrowser; mshtml.HTMLWindow2 win = (mshtml.HTMLWindow2)wb.Document.Window.DomWindow; win.execScript ( @" var PeripheringDeviceJs = window.external.PeripheringDevice; alert('C# exec JavaScript'); " , "javascript" ); } private void button1_Click(object sender, EventArgs e) { object[] args = new object[1]; args[0] = (object)"C# Invoke Script"; var peripheringDevice = _dso.PeripheringDevice; var listener = peripheringDevice.OnExternalEvent; listener .GetType() .InvokeMember ( "" , BindingFlags.InvokeMethod , null , listener , args ); foreach (var x in peripheringDevice.Listeners) { x .GetType() .InvokeMember ( "" , BindingFlags.InvokeMethod , null , x , args ); } } } } namespace Microshaoft { using System.Runtime.InteropServices; using System.Security.Permissions; using System.Collections.Generic; [PermissionSet(SecurityAction.Demand, Name = "FullTrust")] [ComVisible(true)] public class DocumentScriptingObject { public Device PeripheringDevice = new Device(); } [PermissionSet(SecurityAction.Demand, Name = "FullTrust")] [ComVisible(true)] public class Device { private List<object> _listeners = null; [ComVisible(false)] public List<object> Listeners { get { return _listeners; } } //[ComVisible(false)] public void AddJavaScriptListener(object listener) { if (_listeners == null) { _listeners = new List<object>(); } _listeners.Add(listener); } private object _listener = null; public object OnExternalEvent { get { return _listener; } set { _listener = value; } } } } /* <!doctype html> <html lang="en"> <head> <meta charset="UTF-8"> <meta name="Generator" content="EditPlus®,Microshaoft®"> <meta name="Author" content="EditPlus®,Microshaoft®"> <meta name="Keywords" content="EditPlus®,Microshaoft®"> <meta name="Description" content="EditPlus®,Microshaoft®"> <title>Document</title> <script type="text/javascript"> <!-- PeripheringDeviceJs.AddJavaScriptListener( function (data) { alert(data + "in AddJavaScriptListener 1"); } ); PeripheringDeviceJs.AddJavaScriptListener( function (data) { alert(data + " in AddJavaScriptListener 2"); } ); PeripheringDeviceJs.OnExternalEvent = function (data) { alert(data + " in OnExternalEvent"); }; //--> </script> </head> <body> <input type="text" id="" /> <textarea id="" rows="" cols=""></textarea> <input type="radio" id="" /> <input type="checkbox" id="" /> Hello World! </body> </html> */ |
相关文章推荐
- 以WebBrowser.DocumentCompleted 事件为例,说明用C#如何完成事件的订阅处理。
- .Net 以WebBrowser.DocumentCompleted 事件为例,说明用C#如何完成事件的订阅处理
- c# webBrowser 获取js中的alert事件
- 以WebBrowser.DocumentCompleted 事件为例,说明用C#如何完成事件的订阅处理。
- c# webBrowser 获取js中的alert事件
- VB.NET扩展WebBrowser,拥有跳转前获取URL的能力 ,扩展事件
- C#学习基本概念之事件(二)---订阅和取消
- c# webBrowser控件与js的交互
- 那些高级的js原生事件
- webbrowser 里的js调用C#窗体的函数
- asp.net(C#)中给控件添加客户端js事件的方法
- C#调用js && js调用C#事件
- 史上最全的CSS hack方式一览 jQuery 图片轮播的代码分离 JQuery中的动画 C#中Trim()、TrimStart()、TrimEnd()的用法 marquee 标签的使用详情 js鼠标事件 js添加遮罩层 页面上通过地址栏传值时出现乱码的两种解决方法 ref和out的区别在c#中 总结
- c#扩展方法奇思妙用高级篇二:Aggregate扩展其改进
- C#学习笔记(三)—–C#高级特性中的委托与事件:关于事件
- C# ,webBrowser,登录,数据填充,模拟点击链接,运行JS函数
- C# 通过扩展WebBrowser捕获网络连接错误信息
- C#扩展方法奇思妙用高级篇一:改进 Scottgu 的 "In" 扩展
- C# WebBrowser高级应用
- 蛙蛙推荐:WebBrowser控件的高级用法,c#和Javascript交互及光标位置恢复