WinForm与Web的进程内交互

WinForm程序嵌入一个WebBrowser控件,发现WinForm中与Web中方法居然是可以相互调用的。

找到的原来的一个例子中,有System.Security.PemissionSet提示 不是特性 类,直接去掉,测试也能成功。

namespace WindowsFormsApplication3
{
    [System.Runtime.InteropServices.ComVisibleAttribute(true)]
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
        }

        private void Form1_Load(object sender, EventArgs e)
        {
            webBrowser1.AllowWebBrowserDrop = false;//将 WebBrowser 控件的 AllowWebBrowserDrop 属性设置为 false,以防止 WebBrowser 控件打开拖放到其上的文件。
            webBrowser1.IsWebBrowserContextMenuEnabled = false;//将该控件的 IsWebBrowserContextMenuEnabled 属性设置为 false,以防止 WebBrowser 控件在用户右击它时显示其快捷菜单.
            webBrowser1.WebBrowserShortcutsEnabled = false;//将该控件的 WebBrowserShortcutsEnabled 属性设置为 false,以防止 WebBrowser 控件响应快捷键。
            webBrowser1.ScriptErrorsSuppressed = true;//将该控件的 ScriptErrorsSuppressed 属性设置为 true,以防止 WebBrowser 控件显示脚本代码问题的错误信息。
            webBrowser1.ObjectForScripting = this;
            string url = AppDomain.CurrentDomain.SetupInformation.ApplicationBase.Replace("\\bin\\Debug", "") + "HTMLPage1.htm";
            webBrowser1.Navigate(url);
        }

        public string InvokeFormMethod(string message)
        {
            MessageBox.Show("这是从WinForm弹出的提示!", "WinForm");
            return "Charles2008";
        }

        private void button1_Click(object sender, EventArgs e)
        {
            this.webBrowser1.Document.InvokeScript("msgalert", new string[] { "Called Javascript code" });
        }
    }
}


<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN">
<html>
	<head>
		<title></title>
        <script>
            function msgalert(msg) {
                alert(msg);
            }
</script>
	</head>
	<body>
	<input type="button" value="在Web页面中,调用WinForm函数" οnclick="alert(window.external.InvokeFormMethod('从Web发起的调用'))" />
	</body>
</html>



  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值