C# 窗體中 webbrowser 中 javascript 調用窗體方法 實例

//窗體文件

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;

using System.IO;

 

namespace MyApplication

{

    [System.Runtime.InteropServices.ComVisibleAttribute(true)] //必須加上

    public partial class MyForm : Form

    {

        public MyForm()

        {

            InitializeComponent();

            MyBrowser.ObjectForScripting = this;//必須加上

        }

 

        public void Add(int a, int b)

        {

            MessageBox.Show(a.ToString() + "+" + b.ToString() + "=" + (a + b).ToString() + "!");

        }

 

        private void MyForm_Load(object sender, EventArgs e)

        {

            string path = System.Windows.Forms.Application.ExecutablePath;

            FileInfo exeInfo = new FileInfo(path);

            path = exeInfo.DirectoryName + "\\";   //程序根目錄

            string file_name = path + "html_file\\myscript.html"; //html帶路徑文件名 html文件放在debug目錄下得html_file文件夾中

            MyBrowser.Url = new Uri(file_name);

        }

    }

}

//////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////頁面文件

<html>

<head>

<script type="text/javascript">

window.external.Add(2,3);

</script>

</head>

</html>

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章