private bool OnNavigation(IntPtr webView, IntPtr param, wkeNavigationType navigationType, IntPtr urlPtr) { if (Navigation != null) { var c = (int)navigationType; var url = ExtApi.GetWkeString(urlPtr); var res = Navigation(this, (NavigationType)c, url); if (res) { List <Control> controls = new List <Control>(); foreach (Control cs in Controls) { controls.Add(cs); } Controls.Clear(); foreach (var item in controls) { //释放通过js创建的控件 item.Dispose(); } Browser.Current.ClearViewObject(_webView); } return(res); } return(true); }
private bool OnConfirmMessage(IntPtr webView, IntPtr param, IntPtr msg) { string message = ExtApi.GetWkeString(msg); var res = MessageBox.Show(message, "提示", MessageBoxButtons.OKCancel); return(DialogResult.OK == res ? true : false); }
public static string GetWkeString(IntPtr wkePtr) { var strPtr = MBApi.wkeGetString(wkePtr); var strInfo = ExtApi.GetStringInfo(strPtr); byte[] data = new byte[strInfo.strLen]; Marshal.Copy(strInfo.strPtr, data, 0, data.Length); return(Encoding.UTF8.GetString(data)); }
private void OnTitleChange(IntPtr webView, IntPtr param, IntPtr titlePtr) { string title = ExtApi.GetWkeString(titlePtr); if (TitleChanged != null) { TitleChanged(this, title); } }
private long OnChangeControl(IntPtr es, long obj, IntPtr args, int argCount) { if (argCount != 5) { return(MBApi.jsUndefined()); } var vPtr = MBApi.jsArg(es, 0); var vX = MBApi.jsArg(es, 1); var vY = MBApi.jsArg(es, 2); var vWidth = MBApi.jsArg(es, 3); var vHeight = MBApi.jsArg(es, 4); IntPtr controlPtr = IntPtr.Zero; if (MBApi.jsIsString(vPtr)) { var strPtr = ExtApi.GetJsString(es, vPtr); int n = 0; if (!int.TryParse(strPtr, out n)) { return(MBApi.jsUndefined()); } controlPtr = new IntPtr(n); } else if (MBApi.jsIsNumber(vPtr)) { var ptr = MBApi.jsToInt(es, vPtr); controlPtr = new IntPtr(ptr); } else { return(MBApi.jsUndefined()); } NativeControl control = NativeControl.FromHandle(controlPtr) as NativeControl; if (control == null) { return(MBApi.jsUndefined()); } int x, y, width, height = 0; if (!MBApi.jsIsNumber(vX) || !MBApi.jsIsNumber(vY) || !MBApi.jsIsNumber(vWidth) || !MBApi.jsIsNumber(vHeight)) { return(MBApi.jsUndefined()); } x = MBApi.jsToInt(es, vX); y = MBApi.jsToInt(es, vY); width = MBApi.jsToInt(es, vWidth); height = MBApi.jsToInt(es, vHeight); control.SetBounds(x, y, width, height); return(MBApi.jsTrue()); }
private void OnLoadingFinish(IntPtr webView, IntPtr param, IntPtr urlPtr, wkeLoadingResult result, IntPtr failedReasonPtr) { string url = ExtApi.GetWkeString(urlPtr); string failedReason = ExtApi.GetWkeString(failedReasonPtr); if (LoadingComplete != null) { LoadingComplete(this, url, failedReason, (UrlLoadResult)result); } }
private void OnConsel(IntPtr webView, IntPtr param, wkeConsoleLevel level, IntPtr messagePtr, IntPtr sourceNamePtr, uint sourceLine, IntPtr stackTracePtr) { string message = ExtApi.GetWkeString(messagePtr); string sourceName = ExtApi.GetWkeString(sourceNamePtr); string stackTrace = ExtApi.GetWkeString(stackTracePtr); if (ConselMessage != null) { ConselMessage(this, message, sourceName, (int)sourceLine, stackTrace); } }
private long OnCreateObject(IntPtr es, long obj, IntPtr args, int argCount) { try { var typeName_Val = MBApi.jsArg(es, 0); if (!MBApi.jsIsString(typeName_Val)) { return(MBApi.jsUndefined()); } string typeName = ExtApi.GetJsString(es, typeName_Val); Type localType = null; foreach (var ass in AppDomain.CurrentDomain.GetAssemblies()) { foreach (var type in ass.GetTypes()) { if (type.FullName == typeName) { localType = type; break; } } } if (localType == null) { return(MBApi.jsUndefined()); } object[] obj_args = new object[argCount - 1]; for (int i = 1; i < obj_args.Length; i++) { obj_args[i] = JsConvert.ConvertJSToObject(es, MBApi.jsArg(es, i), typeof(object)); } var localObj = Activator.CreateInstance(localType, obj_args); var webView = MBApi.jsGetWebView(es); CSharpObject local_obj = new CSharpObject(webView, localObj); lock (sharpObjects) { List <CSharpObject> list = null; if (!sharpObjects.ContainsKey(webView)) { list = new List <CSharpObject>(); } else { list = sharpObjects[webView]; } list.Add(local_obj); } return(local_obj.ScriptValue); } finally { } }
private long OnLoadLib(IntPtr es, long obj, IntPtr args, int argCount) { try { var p1 = MBApi.jsArg(es, 0); if (!MBApi.jsIsString(p1)) { return(MBApi.jsFalse()); } string libFile = ExtApi.GetJsString(es, p1); lock (_libSet) { if (_libSet.Contains(libFile)) { return(MBApi.jsTrue()); } } if (string.IsNullOrEmpty(libFile)) { return(MBApi.jsFalse()); } libFile = Path.Combine(Path.GetDirectoryName(Assembly.GetExecutingAssembly().Location), libFile); bool find = false; foreach (var ass in AppDomain.CurrentDomain.GetAssemblies()) { if (ass.Location == libFile) { find = true; break; } } if (!find) { //若该程序集尚未加载,则执行加载 Assembly.LoadFile(libFile); lock (_libSet) { _libSet.Add(libFile); } return(MBApi.jsTrue()); } return(MBApi.jsTrue()); } catch (Exception ex) { MessageBox.Show(ex.Message); return(MBApi.jsUndefined()); } finally { } }
private long OnCreateControl(IntPtr es, long obj, IntPtr args, int argCount) { if (argCount < 4) { return(MBApi.jsUndefined()); } var vX = MBApi.jsArg(es, 0); var vY = MBApi.jsArg(es, 1); var vWidth = MBApi.jsArg(es, 2); var vHeight = MBApi.jsArg(es, 3); var disposeFun = MBApi.jsArg(es, 4); int x, y, width, height = 0; if (!MBApi.jsIsNumber(vX) || !MBApi.jsIsNumber(vY) || !MBApi.jsIsNumber(vWidth) || !MBApi.jsIsNumber(vHeight)) { return(MBApi.jsUndefined()); } x = MBApi.jsToInt(es, vX); y = MBApi.jsToInt(es, vY); width = MBApi.jsToInt(es, vWidth); height = MBApi.jsToInt(es, vHeight); string jsData = null; if (!MBApi.jsIsUndefined(disposeFun)) { jsData = ExtApi.GetJsString(es, disposeFun); } var mHandle = MBApi.jsGetWebView(es); NativeControl control = new NativeControl(); control.WebView = mHandle; control.Location = new Point(x, y); control.Width = width; control.Height = height; control.DisposeFun = jsData; WebView parentView = GetParent(mHandle); if (parentView == null) { return(MBApi.jsUndefined()); } parentView.Controls.Add(control); control.BringToFront(); var handle = control.Handle.ToInt32(); return(MBApi.jsToInt(es, handle)); }
private IntPtr CreateCore() { var window = MBApi.wkeCreateWebWindow(_wkeWindowType.WKE_WINDOW_TYPE_CONTROL, Handle, 0, 0, Width, Height); MBApi.wkeSetCookieEnabled(window, true); MBApi.wkeSetNpapiPluginsEnabled(window, true); MBApi.wkeSetMemoryCacheEnable(window, true); MBApi.wkeSetNavigationToNewWindowEnable(window, false); MBApi.wkeSetCspCheckEnable(window, false); MBApi.wkeResize(window, Width, Height); MBApi.wkeEnableWindow(window, true); MBApi.wkeShowWindow(window, true); MBApi.wkeSetUserAgent(window, ExtApi.SetWkeString(BlinkCommon.BlinkUserAgent)); MBApi.wkeSetDebugConfig(window, "antiAlias", BlinkCommon.BlinkAntiAlias); MBApi.wkeSetDebugConfig(window, "minimumFontSize", BlinkCommon.BlinkMinimumFontSize); MBApi.wkeSetDebugConfig(window, "minimumLogicalFontSize", BlinkCommon.BlinkMinimumLogicalFontSize); MBApi.wkeSetDebugConfig(window, "defaultFixedFontSize", BlinkCommon.BlinkDefaultFixedFontSize); MBApi.wkeSetContextMenuItemShow(window, wkeMenuItemId.kWkeMenuCopyImageId | wkeMenuItemId.kWkeMenuCutId | wkeMenuItemId.kWkeMenuGoBackId | wkeMenuItemId.kWkeMenuGoForwardId | wkeMenuItemId.kWkeMenuInspectElementAtId | wkeMenuItemId.kWkeMenuPasteId | wkeMenuItemId.kWkeMenuPrintId | wkeMenuItemId.kWkeMenuReloadId | wkeMenuItemId.kWkeMenuSelectedAllId | wkeMenuItemId.kWkeMenuSelectedTextId | wkeMenuItemId.kWkeMenuUndoId, true); MBApi.wkeOnNavigation(window, _wkeNavCallBack, IntPtr.Zero); MBApi.wkeOnLoadUrlBegin(window, _loadUrlBeginCallBack, IntPtr.Zero); MBApi.wkeOnLoadUrlEnd(window, _loadUrlEndCallBack, IntPtr.Zero); MBApi.wkeOnConsole(window, _consoleCallback, IntPtr.Zero); MBApi.wkeOnDownload(window, _downLoadCallBack, IntPtr.Zero); MBApi.wkeOnDocumentReady(window, _documentReadyCallBack, IntPtr.Zero); MBApi.wkeOnTitleChanged(window, _titleChangeCallBack, IntPtr.Zero); MBApi.wkeOnLoadingFinish(window, _loadingFinishCallBack, IntPtr.Zero); MBApi.wkeOnAlertBox(window, _onAlertBoxCallBack, IntPtr.Zero); MBApi.wkeOnConfirmBox(window, _onConfirmBoxCallBack, IntPtr.Zero); MBApi.wkeOnPromptBox(window, _onPromptBoxCallBack, IntPtr.Zero); var cacheDir = new DirectoryInfo(CACHE_ATTR); cacheDir.Refresh(); if (!cacheDir.Exists) { cacheDir.Create(); } return(window); }
private long OnCreateComObj(IntPtr es, long obj, IntPtr args, int argCount) { var typeName_ID = MBApi.jsArg(es, 0); if (!MBApi.jsIsString(typeName_ID)) { return(MBApi.jsUndefined()); } string typeID = ExtApi.GetJsString(es, typeName_ID); Guid clsId = new Guid(typeID); Type localType = Type.GetTypeFromCLSID(clsId); if (localType == null) { return(MBApi.jsUndefined()); } object[] obj_args = new object[argCount - 1]; for (int i = 1; i < obj_args.Length; i++) { obj_args[i] = JsConvert.ConvertJSToObject(es, MBApi.jsArg(es, i), typeof(object)); } var localObj = Activator.CreateInstance(localType, obj_args); var webView = MBApi.jsGetWebView(es); CSharpObject local_obj = new CSharpObject(webView, localObj); lock (sharpObjects) { List <CSharpObject> list = null; if (!sharpObjects.ContainsKey(webView)) { list = new List <CSharpObject>(); } else { list = sharpObjects[webView]; } list.Add(local_obj); } return(local_obj.ScriptValue); }
public static object ConvertJSToObject(IntPtr es, long v, Type objType) { var pt = MBApi.jsTypeOf(v); if (pt == jsType.UNDEFINED) { return(null); } else if (pt == jsType.FUNCTION) { string funData = ExtApi.GetJsString(es, v); //TODO:创建动态委托 return(FunctionCreater.CreateJsFunctionCallBack(MBApi.jsGetWebView(es), funData, objType)); } else if (pt == jsType.BOOLEAN) { return(MBApi.jsToBoolean(es, v)); } else if (pt == jsType.NUMBER) { return(Convert.ChangeType(MBApi.jsToDouble(es, v), objType)); } else if (pt == jsType.STRING) { return(ExtApi.GetJsString(es, v)); } else if (MBApi.jsIsArray(v)) { var eType = objType.GetElementType(); var len = MBApi.jsGetLength(es, v); var args = Array.CreateInstance(eType, len); for (int i = 0; i < len; i++) { var argV = ConvertJSToObject(es, MBApi.jsGetAt(es, v, i), eType); args.SetValue(argV, i); } return(args); } else if (pt == jsType.OBJECT) { var base_obj = Activator.CreateInstance(objType); var pInfos = objType.GetProperties(); var dicFun = MBApi.jsEvalW(es, SCRIPT_FUN); //将object转化成{name,value}的数组 var dv = MBApi.jsCallGlobal(es, (long)dicFun, new long[] { v }, 1); var dLen = MBApi.jsGetLength(es, dv); for (int i = 0; i < dLen; i++) { var item = MBApi.jsGetAt(es, dv, i); var name = ExtApi.GetJsString(es, MBApi.jsGet(es, item, NAME_ATTR)); PropertyInfo pInfo = null; if (pInfos.Length > 0) { foreach (var x in pInfos) { if (x.Name.ToLower() == name.ToLower()) { pInfo = x; break; } } } if (pInfo != null) { var js_obj = MBApi.jsGet(es, item, VALUE_ATTR); var obj = ConvertJSToObject(es, js_obj, pInfo.PropertyType); pInfo.SetValue(base_obj, obj, null); } } return(base_obj); } else { return(null); } }
/// <summary> /// 将Object对象转换为JS对象 /// </summary> /// <param name="es"></param> /// <param name="obj"></param> /// <returns></returns> public static long ConvertObjectToJS(IntPtr es, object obj) { if (obj == null) { return(MBApi.jsNull()); } long resIndex = MBApi.jsUndefined(); var type = obj.GetType(); if (type == typeof(byte[])) { var strData = Convert.ToBase64String((byte[])obj); resIndex = ExtApi.SetJsTempString(es, strData); } else if (type.IsSubclassOf(typeof(Array))) { var arr = obj as Array; resIndex = MBApi.jsEmptyArray(es); MBApi.jsSetLength(es, resIndex, arr.Length); for (int i = 0; i < arr.Length; i++) { MBApi.jsSetAt(es, resIndex, i, ConvertObjectToJS(es, arr.GetValue(i))); } } else if (type == typeof(string)) { resIndex = ExtApi.SetJsTempString(es, obj.ToString()); } else if (type == typeof(DateTime)) { DateTime curTime = (DateTime)obj; resIndex = MBApi.jsDouble((curTime - DateTime.MinValue).TotalMilliseconds); } else if (type == typeof(int) || type == typeof(uint) || type == typeof(short) || type == typeof(ushort) || type == typeof(byte) || type == typeof(sbyte)) { var curValue = (int)obj; resIndex = MBApi.jsInt(curValue); } else if (type == typeof(double) || type == typeof(decimal) || type == typeof(long) || type == typeof(ulong) ) { var curValue = (double)obj; resIndex = MBApi.jsDouble(curValue); } else if (type == typeof(float)) { var curValue = (float)obj; resIndex = MBApi.jsFloat(curValue); } else if (type == typeof(bool)) { var curValue = (bool)obj; resIndex = MBApi.jsBoolean(curValue); } else { resIndex = MBApi.jsEmptyObject(es); var pInfos = type.GetProperties(); //将实例中所有保护值类型属性赋值 foreach (var pInfo in pInfos) { var val = pInfo.GetValue(obj, null); if (val != null && val.GetType().IsValueType) { MBApi.jsSet(es, resIndex, pInfo.Name, ConvertObjectToJS(es, val)); } } } return(resIndex); }
private bool OnAlertMessage(IntPtr webView, IntPtr param, IntPtr msg) { string message = ExtApi.GetWkeString(msg); return(DialogResult.OK == MessageBox.Show(message)); }