webbrowser 里的js调用C#窗体的函数

    xiaoxiao2026-10-02  7

    1.c#程序里要添加  [System.Runtime.InteropServices.ComVisibleAttribute(true)] 和  webBrowser1.ObjectForScripting = this;

    位置:

            [System.Runtime.InteropServices.ComVisibleAttribute(true)]     public partial class Form1 : Form     {               public Form1()         {             InitializeComponent();             refresh();             string path1 = System.Windows.Forms.Application.StartupPath + @"\" + "PlanMngPage" + @"\home.htm";             webBrowser1.Navigate(new Uri(path1));             webBrowser1.ObjectForScripting = this;         }  public void MyMessageBox(string message)         {             MessageBox.Show(message);         }

    2.在JS使用 window.external.引用的窗体的函数();   来引用C#里的函数

      eg:

      <body  οnlοad="window.external.MyMessageBox('javascript访问C#代码')">

    转载请注明原文地址: https://ju.6miu.com/read-1312537.html
    最新回复(0)