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


/*
 /r:"D:\MyC#\WebBrowser.FSO.OnExternalEvent\Interop.Scripting.dll";"C:\Program Files\Microsoft.NET\Primary Interop Assemblies\Microsoft.mshtml.dll"
*/
namespace Test
{
    using System;
    using System.ComponentModel;
    using System.Windows.Forms;
    using System.Reflection;
    using Microshaoft;
    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;
            s = AppDomain.CurrentDomain.BaseDirectory + @"Noname1.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 Microshaoft = window.external;alert('C# exec JavaScript')", "javascript");
        }
        private void button1_Click(object sender, EventArgs e)
        {
            object[] args = new object[1];
            args[0] = (object) "第二步: C# Invoke Script";
            _dso.Sys.OnExternalEvent1.GetType().InvokeMember
                                                (
                                                    ""
                                                    , BindingFlags.InvokeMethod
                                                    , null
                                                    , _dso.Sys.OnExternalEvent1
                                                    , args
                                                );
        }
    }
}
namespace Microshaoft
{
    using System;
    using System.Security.Permissions;
    using System.Runtime.InteropServices;
    using System.Windows.Forms;
    using System.Collections.Generic;
    using System.IO;
    using Scripting;
    [PermissionSet(SecurityAction.Demand, Name="FullTrust")]
    [ComVisible(true)]
    public class DocumentScriptingObject
    {
        private SysClass _Sys = new SysClass("default");
        public SysClass Sys
        {
            get
            {
                return _Sys;
            }
        }
        
        public class SysClass
        {
            private object _onExternalEvent1;
            
            //private List<SysClass> _a;
            public void ShowMessage()
            {
                MessageBox.Show(_msg);
            
            }
            private string _msg = "default";
            public SysClass(string s)
            {
                _msg = s;
            }
            public FileSystemObject NewFSO()
            {
                return new FileSystemObject();
            
            }
            public object OnExternalEvent1
            {
                get 
                {
                    return _onExternalEvent1; 
                }
                set 
                {
                    //Console.WriteLine(Microsoft.VisualBasic.Information.TypeName(value));
                    _onExternalEvent1 = value; 
                }
            }
        }
    }
}
/*
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<title> new document </title>
<meta name="generator" content="editplus" />
<meta name="author" content="" />
<meta name="keywords" content="" />
<meta name="description" content="" />
<script type="text/javascript">
alert('html Script')
<!--
 function RegisterExternalEvent()
 {
 Microshaoft.Sys.OnExternalEvent1 = onExternalEvent1;
 }
 function onExternalEvent1(x)
 {
 //invoke by c#
 alert(x)
 }
//-->
</script>
</head>

<body>
<input type="button" value= "第一步: HTML Script Register C# External Event" onclick="RegisterExternalEvent();alert('HTML Script Register External Event')" />
</body>
</html>
*/

你可能感兴趣的:(WebBrowser)