The example in this article describes how C # calls WinForm through HTML. Share to everyone for your reference, specific 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 # access 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 ();
}
Web page:
<! DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 transitional//en" >
Add:
Webbrowser1.objectforscripting = this;
The meaning of this sentence is that the WebBrowser1 script executes COM binding method from Form1, and MyMessageBox1 and ShowForm are in the Winoperationclass class, certainly not.
The first one can be because the Form1 inside has mymessagebox this method, you MyMessageBox1 and ShowForm move to Form1 or move Mymessagebox inside Winoperationclass, then
Webbrowser1.objectforscripting = this;
Change this sentence into
Winoperationclass w=new Winoperationclass ();
webbrowser1.objectforscripting = W;
It's okay.
Recommend the second kind of ... Put all the COM visible methods inside a class to maintain
Read more about C # Interested readers can view the site topics: "WinForm Control Usage Summary", "C # Data structure and algorithm tutorial", "C # Common control usage Tutorial", "C # object-oriented Program design Introductory Course" and "C # Programming Thread Usage Skills Summary"
I hope this article will help you with C # programming.