winform集成cefSharp,与页面进行交互

人走茶凉 提交于 2019-12-03 11:15:37
    /// <summary>
    /// 为了使网页能够与winForm交互 将 com 的可访问性设置为 true
    /// </summary>
    [System.Runtime.InteropServices.ComVisible(true)]
    public partial class Form1 : Form
    {
        /// <summary>
        /// 声明变量
        /// </summary>
        private CefSharp.CefSettings _settings;
        ChromiumWebBrowser _browser;

        public Form1()
        {
            try
            {
                InitializeComponent();

                //初始化cefSharp
                AppLog.Info("cefSharp init ...");
                _settings = new CefSharp.CefSettings();
                CefSharp.Cef.Initialize(_settings);

                //下面设置,减少白屏的发生,此为cefSharp的bug
                if (!_settings.MultiThreadedMessageLoop)
                {
                    Application.Idle += (sender, e) => { Cef.DoMessageLoopWork(); };
                }
                AppLog.Info("cefSharp init OK");

                //设置窗口最大化,最顶端显示
                this.FormBorderStyle = FormBorderStyle.None;
                this.WindowState = FormWindowState.Maximized;
                this.TopMost = true;

            }
            catch (Exception ex)
            {
                AppLog.Error(ex.Message);
            }
        }

        private void Form1_Load(object sender, EventArgs e)
        {
            try
            {
                _browser = new ChromiumWebBrowser("../index.html");
                _browser.RegisterJsObject("cefSharpBrower", new ScriptCallbackManager(), false);

                this.Controls.Add(_browser);

            }
            catch (Exception ex)
            {
                AppLog.Error(ex.Message);
            }
        }

        /// <summary>
        /// js调用方法类
        /// </summary>
        [System.Runtime.InteropServices.ComVisible(true)]
        class ScriptCallbackManager
        {
            /// <summary>
            /// 关闭窗体
            /// </summary>
            public void CloseWindow()
            {
                //退出程序,结束进程
                Process.GetCurrentProcess().Kill();
            }

            //方法1
            public void Func1()
            {
                  //具体实现  
                  ...
            }    }    

添加对cefSharp的引用

using CefSharp;
using CefSharp.WinForms;

页面的调用和交互

<!DOCTYPE html>

<html lang="en" xmlns="http://www.w3.org/1999/xhtml">
<head>
    <meta charset="utf-8" />
    <title></title>
    <style>
        body {
            background-color: cadetblue
        }
    </style>
</head>
<body>
<div>
    <input value="关闭winform" type="button" id="btn_close" />
    <input value="Func1" type="button" id="btn_calc" />
</div>
<script type="text/javascript" src="https://code.jquery.com/jquery-1.10.2.min.js"></script>
<script type="text/javascript">

    //关闭程序
    $(function () {
        $("#btn_close").click(function () {
            cefSharpBrower.CloseWindow();
        });
    });

    $(function () {
        $("#btn_calc").click(function () {
            cefSharpBrower.Func1();
        });
    });

</script>
</body>
</html>

 

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!