JS in HTML calls C # 's WinForm program

Source: Internet
Author: User

Simply put, load a webBrowser1 in the C # client WinForm and then trigger the C # function response with the Click JS effect on the page in WebBrowser1. The pro-test passed.

The C # code is as follows:

Using system;using system.collections.generic;using system.componentmodel;using system.data;using System.Drawing; Using system.linq;using system.text;using system.windows.forms;namespace testjs{    [ System.Security.Permissions.PermissionSet (System.Security.Permissions.SecurityAction.Demand, Name = "FullTrust")]    [System.Runtime.InteropServices.ComVisibleAttribute (true)] public    partial class Form1:form    {        public void Say ()        {            MessageBox.Show ("OK");        }        Public Form1 ()        {            InitializeComponent ();        }        private void Form1_Load (object sender, EventArgs e)        {            this.webBrowser1.ObjectForScripting = this;            This.webBrowser1.Navigate ("http://localhost/1.html");}}}    

The JS code is as follows:

<script>
function Say ()
{
var tnum=window.external.say ();//getdebugpath () is a C # method
}
</script>
Hi
<input type= "button" value= "OK" onclick= "Say ()" >



Tests can be output


Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.