265 lines
9.0 KiB
C#
265 lines
9.0 KiB
C#
using Lskj.Business;
|
|
using Lskj.Model;
|
|
using Lskj.Util;
|
|
using System;
|
|
using System.Collections.Generic;
|
|
using System.Linq;
|
|
using System.Text;
|
|
using System.Threading.Tasks;
|
|
using System.Windows.Forms;
|
|
using Xilium.CefGlue;
|
|
|
|
namespace Lskj.Control.BrowserSetting
|
|
{
|
|
public class BsClient : CefClient
|
|
{
|
|
public FrmWebBrowser frmWebBrowser;
|
|
public int rightKeyFlag;
|
|
public event EventHandler OnCreated;
|
|
private readonly CefLifeSpanHandler lifeSpanHandler;
|
|
private readonly CefDownloadHandler downloadHandler;
|
|
private readonly CefKeyboardHandler keyboardHandler;
|
|
private readonly CefFocusHandler focusHandler;
|
|
private readonly CefRequestHandler requestHandler;
|
|
private readonly CefContextMenuHandler contextMenuHandler;
|
|
public BsClient(int RightKeyFlag = 0)
|
|
{
|
|
if (SystemInfo.Instance.BrowserRight) RightKeyFlag = 1;
|
|
lifeSpanHandler = new BsLifeSpanHandler(this);
|
|
downloadHandler = new BsDownloadHandler(this);
|
|
keyboardHandler = new BsKeyboardHandler();
|
|
focusHandler = new BsFocusHandler();
|
|
requestHandler = new BsRequestHandler();
|
|
contextMenuHandler = new BsContextMenuHandler(RightKeyFlag);
|
|
}
|
|
protected override CefLifeSpanHandler GetLifeSpanHandler()
|
|
{
|
|
return lifeSpanHandler;
|
|
}
|
|
protected override CefDownloadHandler GetDownloadHandler()
|
|
{
|
|
return downloadHandler;
|
|
}
|
|
protected override CefKeyboardHandler GetKeyboardHandler()
|
|
{
|
|
return keyboardHandler;
|
|
}
|
|
protected override CefFocusHandler GetFocusHandler()
|
|
{
|
|
return focusHandler;
|
|
}
|
|
protected override CefRequestHandler GetRequestHandler()
|
|
{
|
|
return requestHandler;
|
|
}
|
|
protected override CefContextMenuHandler GetContextMenuHandler()
|
|
{
|
|
return contextMenuHandler;
|
|
}
|
|
protected override bool OnProcessMessageReceived(CefBrowser browser, CefFrame frame, CefProcessId sourceProcess, CefProcessMessage message)
|
|
{
|
|
try
|
|
{
|
|
return OnProcessMessageReceivedCore(browser, frame, sourceProcess, message);
|
|
}
|
|
catch (Exception exception)
|
|
{
|
|
ReportProcessMessageFailure(exception);
|
|
return true;
|
|
}
|
|
}
|
|
|
|
private bool OnProcessMessageReceivedCore(CefBrowser browser, CefFrame frame, CefProcessId sourceProcess, CefProcessMessage message)
|
|
{
|
|
if (message.Name.Equals("OpenModule"))
|
|
{
|
|
string arg0 = message.Arguments.GetString(0);
|
|
string arg1 = message.Arguments.GetString(1);
|
|
string arg2 = message.Arguments.GetString(2);
|
|
string arg3 = message.Arguments.GetString(3);
|
|
string arg4 = message.Arguments.GetString(4);
|
|
PostToUiThread(
|
|
ERPInfo.Instance.MainControl,
|
|
delegate
|
|
{
|
|
BsOpenModuleHandler.OpenModule(
|
|
arg0,
|
|
arg1,
|
|
arg2,
|
|
arg3,
|
|
arg4);
|
|
});
|
|
}
|
|
else if (message.Name.Equals("OpenOnRightClick"))
|
|
{
|
|
string arg0 = message.Arguments.GetString(0);
|
|
string arg1 = message.Arguments.GetString(1);
|
|
string arg2 = message.Arguments.GetString(2);
|
|
string arg3 = message.Arguments.GetString(3);
|
|
PostToUiThread(
|
|
ERPInfo.Instance.MainControl,
|
|
delegate
|
|
{
|
|
BsOpenModuleHandler.OpenRightModule(
|
|
arg0,
|
|
arg1,
|
|
arg2,
|
|
arg3);
|
|
});
|
|
}
|
|
else if (message.Name.Equals("CloseWin"))
|
|
{
|
|
var browserControl = frmWebBrowser;
|
|
PostToUiThread(
|
|
browserControl,
|
|
delegate
|
|
{
|
|
// Parent/Parent is a WinForms control-tree read and must
|
|
// not run on CEF's UI thread.
|
|
var parent = browserControl.Parent;
|
|
var baseForm = parent == null
|
|
? null
|
|
: parent.Parent as BaseForm;
|
|
if (baseForm == null || baseForm.IsDisposed)
|
|
return;
|
|
|
|
baseForm.DialogResult = DialogResult.OK;
|
|
baseForm.Close();
|
|
});
|
|
}
|
|
else if (message.Name.Equals("SelectPro"))
|
|
{
|
|
string arg0 = message.Arguments.GetString(0);
|
|
string arg1 = message.Arguments.GetString(1);
|
|
bool isclose = arg1!=null&&!string.IsNullOrWhiteSpace(arg1) && arg1.Equals("1") ? true : false;
|
|
var browserControl = frmWebBrowser;
|
|
PostToUiThread(
|
|
browserControl,
|
|
delegate
|
|
{
|
|
var parent = browserControl.Parent;
|
|
var baseForm = parent == null
|
|
? null
|
|
: parent.Parent as BaseForm;
|
|
if (baseForm == null || baseForm.IsDisposed)
|
|
return;
|
|
|
|
BsOpenModuleHandler.AddReturnRow(arg0);
|
|
if (isclose)
|
|
{
|
|
baseForm.DialogResult = DialogResult.OK;
|
|
baseForm.Close();
|
|
}
|
|
});
|
|
}
|
|
|
|
return base.OnProcessMessageReceived(browser, frame, sourceProcess, message);
|
|
}
|
|
|
|
private static void PostToUiThread(
|
|
System.Windows.Forms.Control control,
|
|
Action action)
|
|
{
|
|
if (control == null || action == null || control.IsDisposed ||
|
|
!control.IsHandleCreated)
|
|
return;
|
|
|
|
Action guardedAction = delegate
|
|
{
|
|
if (control.IsDisposed)
|
|
return;
|
|
|
|
try
|
|
{
|
|
action();
|
|
}
|
|
catch (Exception exception)
|
|
{
|
|
ReportProcessMessageFailure(exception);
|
|
}
|
|
};
|
|
|
|
try
|
|
{
|
|
// CEF invokes OnProcessMessageReceived on its UI thread. A
|
|
// synchronous Control.Invoke can block that thread inside a
|
|
// modal dialog while the dialog waits for CEF to create its
|
|
// browser. Always queue the operation and return to CEF first.
|
|
control.BeginInvoke(guardedAction);
|
|
}
|
|
catch (Exception exception)
|
|
{
|
|
ReportProcessMessageFailure(exception);
|
|
}
|
|
}
|
|
|
|
private static void ReportProcessMessageFailure(Exception exception)
|
|
{
|
|
try
|
|
{
|
|
LogHelper.Instance.WriteError(exception);
|
|
}
|
|
catch
|
|
{
|
|
// Never propagate logging failures into a native CEF callback.
|
|
}
|
|
|
|
Action showError = delegate
|
|
{
|
|
try
|
|
{
|
|
MessageUtil.Show(exception);
|
|
}
|
|
catch
|
|
{
|
|
}
|
|
};
|
|
|
|
try
|
|
{
|
|
System.Windows.Forms.Control mainControl = ERPInfo.Instance.MainControl;
|
|
if (mainControl == null || mainControl.IsDisposed ||
|
|
!mainControl.IsHandleCreated)
|
|
return;
|
|
|
|
if (mainControl.InvokeRequired)
|
|
mainControl.BeginInvoke(showError);
|
|
else
|
|
showError();
|
|
}
|
|
catch
|
|
{
|
|
// The main window may close while the callback is reporting.
|
|
}
|
|
}
|
|
|
|
public void Created(CefBrowser cefBrowser)
|
|
{
|
|
if (frmWebBrowser != null && OnCreated != null)
|
|
{
|
|
OnCreated(cefBrowser, EventArgs.Empty);
|
|
}
|
|
}
|
|
|
|
public bool Closing(CefBrowser browser)
|
|
{
|
|
var control = frmWebBrowser;
|
|
return control != null && control.BrowserClosing(browser);
|
|
}
|
|
|
|
public void Closed(CefBrowser browser)
|
|
{
|
|
var control = frmWebBrowser;
|
|
if (control != null)
|
|
{
|
|
control.BrowserClosed(browser);
|
|
if (control.cefBrowserSettings == null)
|
|
{
|
|
frmWebBrowser = null;
|
|
OnCreated = null;
|
|
}
|
|
}
|
|
}
|
|
}
|
|
}
|