WebBrowser 高级扩展 js扩展 js订阅C#事件


/*
    /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>
*/

posted @ 2008-10-15 12:17  于斯人也  阅读(5199)  评论(2编辑  收藏  举报