You cannot select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

185 lines
5.5 KiB
C#

This file contains ambiguous Unicode characters!

This file contains ambiguous Unicode characters that may be confused with others in your current locale. If your use case is intentional and legitimate, you can safely ignore this warning. Use the Escape button to highlight these characters.

using CefSharp;
using CefSharp.WinForms;
using NLog;
using POSV.Cef.Client.Properties;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows.Forms;
namespace POSV.Cef.Client.ClientComm
{
public delegate void LoadingStateChanged(bool IsLoadStatus,string Message);
public class CefUnifyBrowser
{
public CefUnifyBrowser(object _form)
{
this.InitializedCef();
this.m_webBrowser = new ChromiumWebBrowser("");
this.m_webBrowser.RegisterJsObject("_form", _form , false);
this.m_webBrowser.Dock = DockStyle.Fill;
this.m_webBrowser.LoadingStateChanged += M_webBrowser_LoadingStateChanged;
this.m_webBrowser.LoadError += M_webBrowser_LoadError;
this.m_form = _form as Control;
this.m_form.Controls.Add(m_webBrowser);
this.m_webBrowser.Show();
this.logger = LogManager.GetLogger(this.GetType().FullName + "【CEF操作类】");
}
//================== private fields =====================
#region fields
/// <summary>
/// cef 浏览器组件
/// </summary>
private ChromiumWebBrowser m_webBrowser = null;
/// <summary>
/// 主窗口
/// </summary>
public Control m_form = null;
/// <summary>
/// 日志
/// </summary>
private Logger logger = null;
/// <summary>
/// 加载消息
/// </summary>
public string Message = string.Empty;
public LoadingStateChanged LoadingStateEvent;
#endregion
//================== public function =======================
#region LoadCef 加载网页
/// <summary>
/// 跳转
/// </summary>
/// <param name="Url">地址</param>
/// <param name="function">类</param>
public void LoadCef(string Url)
{
this.m_webBrowser.Load(Url);
}
#endregion
#region CallbackJsfunction 调用JS函数
/// <summary>
/// 调用JS函数
/// </summary>
/// <param name="name">JS方法名</param>
/// <param name="data">方法参数</param>
public void CallbackJsfunction(string name, string data)
{
this.logger.Info("CEF操作" + name + "数据" + data);
var _name = string.Format("{0}('{1}')", name, data);
this.m_webBrowser.GetBrowser().MainFrame.ExecuteJavaScriptAsync(_name);
}
#endregion
#region ShowDevTools 调用窗口
/// <summary>
/// 调用html调用窗口
/// </summary>
public void ShowDevTools()
{
try
{
if (m_webBrowser.IsBrowserInitialized)
{
this.m_webBrowser.ShowDevTools();
}
}
catch
{
}
}
#endregion
#region CloseBrowser 关闭Form
/// <summary>
/// 关闭Form, 直接关闭窗口会到导致线程不能完全退出
/// </summary>
public void CloseBrowser()
{
this.m_webBrowser.GetBrowser().CloseBrowser(true);
this.logger.Info("关闭cef");
}
#endregion
#region Back 后退,保留界面状态
public void BackForm()
{
this.m_webBrowser.Back();
}
#endregion
#region Reload
public void Reload()
{
this.m_webBrowser.Reload();
}
#endregion
#region ForwardForm前进用户表单填写完后不依然保存
public void ForwardForm()
{
this.m_webBrowser.Forward();
}
#endregion
//================== private function =======================
private void M_webBrowser_LoadingStateChanged(object sender, LoadingStateChangedEventArgs e)
{
if (this.LoadingStateEvent != null)
{
this.LoadingStateEvent(e.Browser.IsLoading, Message);
}
}
private void M_webBrowser_LoadError(object sender, LoadErrorEventArgs e)
{
logger.Info(string.Format("界面加载失败!\t\n ErrorCode:" + e.ErrorCode + "\t\n ErrorText:" + e.ErrorText + "\t\n FailedUrl:" + e.FailedUrl));
}
private void InitializedCef()
{
if (!CefSharp.Cef.IsInitialized)
{
var settings = new CefSettings();
// 在配置类中注册自定义的schemeName与其对应的工厂类
settings.RegisterScheme(
new CefCustomScheme
{
SchemeName = CustomSchemeHandlerFactory.SchemeName,
SchemeHandlerFactory = new CustomSchemeHandlerFactory()
});
// 设置是否使用代理服务
settings.CefCommandLineArgs.Add("no-proxy-server", "1");
settings.CefCommandLineArgs.Add("disable-gpu-compositing", "1");
settings.CefCommandLineArgs.Add("enable-begin-frame-scheduling", "1");
settings.CefCommandLineArgs.Add("disable-gpu-vsync", "1");
settings.CefCommandLineArgs.Add("disable-direct-write", "1");
// 初始化
CefSharp.Cef.Initialize(settings);
}
}
}
}