sexta-feira, 19 de fevereiro de 2016

Communication between ActiveX and JavaScript - Edge



                                               Scanner.cs (ClassLibrary)
  


using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Runtime.InteropServices;

namespace Scanner
{
  [ClassInterface(ClassInterfaceType.AutoDispatch)]
  [ComVisible(true), Guid("D857B4F5-8684-453e-82C8-7F493CBE1981")]
  public partial class MyUserControl : UserControl
  {
    private Button btnOK;
    private Label lblVersao;
    private Label lblNome;
    private Button btnChamarJS;
    private Button btnOK2;
    private HtmlDocument htmldoc;
    public MyUserControl()
    {
      InitializeComponent();
      this.lblVersao.Text = System.Reflection.Assembly.GetExecutingAssembly().GetName().Version.ToString();
      this.lblNome.Text = System.Reflection.Assembly.GetExecutingAssembly().GetName().Name;

    }


    private void InitializeComponent()
    {
      this.btnOK = new System.Windows.Forms.Button();
      this.btnOK2 = new System.Windows.Forms.Button();
      this.lblVersao = new System.Windows.Forms.Label();
      this.lblNome = new System.Windows.Forms.Label();
      this.btnChamarJS = new System.Windows.Forms.Button();
      this.SuspendLayout();
      //
      // btnOK
      //
      this.btnOK.Location = new System.Drawing.Point(37, 20);
      this.btnOK.Name = "btnOK";
      this.btnOK.Size = new System.Drawing.Size(75, 23);
      this.btnOK.TabIndex = 0;
      this.btnOK.Text = "Test";
      this.btnOK.UseVisualStyleBackColor = true;
      this.btnOK.Click += new System.EventHandler(this.btnOK_Click);
      //
      // btnOK2
      //
      this.btnOK2.Location = new System.Drawing.Point(37, 95);
      this.btnOK2.Name = "btnOK2";
      this.btnOK2.Size = new System.Drawing.Size(75, 23);
      this.btnOK2.TabIndex = 1;
      this.btnOK2.Text = "Test2";
      this.btnOK2.UseVisualStyleBackColor = true;
      this.btnOK2.Click += new System.EventHandler(this.btnOK2_Click);
      //
      // lblVersao
      //
      this.lblVersao.AutoSize = true;
      this.lblVersao.Location = new System.Drawing.Point(230, 25);
      this.lblVersao.Name = "lblVersao";
      this.lblVersao.Size = new System.Drawing.Size(39, 13);
      this.lblVersao.TabIndex = 2;
      this.lblVersao.Text = "versão";
      //
      // lblNome
      //
      this.lblNome.AutoSize = true;
      this.lblNome.Location = new System.Drawing.Point(170, 25);
      this.lblNome.Name = "lblNome";
      this.lblNome.Size = new System.Drawing.Size(33, 13);
      this.lblNome.TabIndex = 3;
      this.lblNome.Text = "nome";
      //
      // btnChamarJS
      //
      this.btnChamarJS.Location = new System.Drawing.Point(37, 163);
      this.btnChamarJS.Name = "btnChamarJS";
      this.btnChamarJS.Size = new System.Drawing.Size(75, 23);
      this.btnChamarJS.TabIndex = 4;
      this.btnChamarJS.Text = "Chamar JS";
      this.btnChamarJS.UseVisualStyleBackColor = true;
      this.btnChamarJS.Click += new System.EventHandler(this.btnChamarJS_Click);
      //
      // MyUserControl
      //
      this.Controls.Add(this.btnChamarJS);
      this.Controls.Add(this.lblNome);
      this.Controls.Add(this.lblVersao);
      this.Controls.Add(this.btnOK2);
      this.Controls.Add(this.btnOK);
      this.Name = "MyUserControl";
      this.Size = new System.Drawing.Size(687, 310);
      this.ResumeLayout(false);
      this.PerformLayout();

    }

    private void btnOK_Click(object sender, EventArgs e)
    {
      MessageBox.Show("Teste 1");
    }

    private void btnOK2_Click(object sender, EventArgs e)
    {
      MessageBox.Show("Teste 2");
    }

    [ComVisible(true)]
    public void RetornaValorPaginaWeb()
    {
      MessageBox.Show("Teste 3");
    }

    [ComVisible(true)]
    public void MostraValor(String Valor)
    {
      MessageBox.Show("Teste 4: Valor=" + Valor);
    }

    private void btnChamarJS_Click(object sender, EventArgs e)
    {

      try
      {
        if (this.Site != null)
        {
          htmldoc = (HtmlDocument)this.Site.GetService(typeof(HtmlDocument));

        }
      }
      catch (System.Exception ex)
      {
        MessageBox.Show("try0");
      }


      MessageBox.Show("htmldoc =  {"+Convert.ToString(htmldoc) +"} { this.Site = " + Convert.ToString(this.Site) + " }");
      try
      {
        HtmlElement setCLIButton = htmldoc.GetElementById("searchButton");
        setCLIButton.InvokeMember("onClick");
      }
      catch (System.Exception ex) {
        MessageBox.Show("try1");
      }
      try
      {
        htmldoc.InvokeScript("alert");
      }
      catch (System.Exception ex)
      {

        MessageBox.Show("try2");
      }

      try
      {
        object[] args = { "teste" };
        htmldoc.InvokeScript("funcao01", args);
      }
      catch (System.Exception ex) {

        MessageBox.Show("try3");
      }
    }

  }
}

SAMPLE.HTML

<html>
    <HEAD>
        <TITLE>Invoke Script Sample</TITLE>

       <script type="text/javascript">
          function MyObject() {
            this.Data = "Data for my private object.";
          }
          // Return a string.
          function test() {
            return ("This is a test.");
          }
          // Return a JScript object.
          function testJScriptObject() {
            return (new (MyObject));
          }
          // Return a DOM element.
          function testElement() {
            return (div1);
          }
        </script>
    </HEAD>
      <DIV id="div1">
        </DIV>

<object id="MyActiveX" classid="clsid:D857B4F5-8684-453e-82C8-7F493CBE1981"  viewastext="" height="480px" width="640px">

    <p>
        Este texto ser exibido se seu browser não conseguir carregar o activeX. 
        Isto acontece, se voc~e estiver abrindo no Firefox ou Chrome.
        Execute no Internet Explorer.
    </p>
</object>


<button onclick="myFunction()">Click me</button>
<button id="searchButton" onclick="searchButton()">click searchButton()</button>

<p id="demo"></p>

<script type="text/javascript">
  function myFunction() {
    var obj = document.getElementById("MyActiveX");
    obj.RetornaValorPaginaWeb();
    obj.MostraValor('Teste');

  }

  function searchButton() {
    alert('test searchButton JS');
  }
  function funcao01(valor) {
    alert(valor);
  }
</script>

</html>

Postar um comentário