C calls the method of WinForm through html

  • 2021-09-24 23:30:06
  • OfStack

This article illustrates how C # calls WinForm through html. Share it for your reference, as follows:

Complete test code:

Form1.cs:


using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
namespace test
{
  [System.Runtime.InteropServices.ComVisibleAttribute(true)]
  public partial class Form1 : Form
  {
    public Form1()
    {
      InitializeComponent();
    }
    private void Form1_Load(object sender, EventArgs e)
    {
      System.IO.FileInfo file = new System.IO.FileInfo(Application.StartupPath+@"\test1.htm");
      webBrowser1.Url = new Uri(file.FullName);
      webBrowser1.ObjectForScripting = this;
    }
    private void button1_Click(object sender, EventArgs e)
    {
      object[] objects = new object[1];
      objects[0]="C# Visit javascript Script ";
      webBrowser1.Document.InvokeScript("messageBox", objects);
    }
    public void MyMessageBox(string message)
    {
      MessageBox.Show(message);
    }
  }
}

Class WinOper:


[System.Runtime.InteropServices.ComVisibleAttribute(true)]
  public class WinOperationClass
  {
    public void MyMessageBox1()
    {
      MessageBox.Show(message);
    }
    public void ShowForm()
    {
      Form2 f2 = new Form2();
      f2.WindowState = FormWindowState.Normal;
      f2.Show();
    }
  }

Webpage:


<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN">
<html>
  <head>
    <title></title>
    <script language="javascript" type="text/javascript">
      function messageBox(message)
      {
        alert(message);
      }
    </script>
  </head>
  <body>
    <button onclick="window.external.MyMessageBox('javascript Visit C# Code ')">javascript Visit C# Code </button>
      <a href="javascript:window.external.MyMessageBox1()">javascript Visit C# Code </a>
    <a href="javascript:window.external.ShowForm()">javascript Visit C# Code </a>
  </body>
</html>

Additional:


webBrowser1.ObjectForScripting = this;

This sentence means that the Com binding method executed by webBrowser1 script comes from Form1, while MyMessageBox1 and ShowForm are in WinOperationClass class, which is definitely not possible.

The first one can be because there is MyMessageBox in form1. You can move MyMessageBox1 and ShowForm to form1 or move MyMessageBox to WinOperationClass, and then put


webBrowser1.ObjectForScripting = this;

This sentence is changed to read


WinOperationClass w=new WinOperationClass();
webBrowser1.ObjectForScripting = w;

That's enough

Recommended 2 … Put all Com visible methods in one class for easy maintenance

For more readers interested in C # related content, please check out the topics on this site: "WinForm Control Usage Summary", "C # Data Structure and Algorithm Tutorial", "C # Common Control Usage Tutorial", "C # Object-Oriented Programming Introduction Tutorial" and "C # Programming Thread Use Skills Summary"

I hope this article is helpful to everyone's C # programming.


Related articles: