コード例 #1
0
        protected override void OnContextCreated(CefBrowser browser, CefFrame frame, CefV8Context context)
        {
            /*缓存数据库*/
            string extensionCode =
                    "var cachedb;" +
                    "if(!cachedb)" +
                    "	cachedb={};" +
                    "(function() {" +
                    " cachedb.Connect = function(dbName) {" +
                    "	native function Connect(dbName);" +
                    "	return Connect(dbName);" +
                    " };" +

                    " cachedb.Execute = function(commandText) {" +
                    "	native function Execute(commandText);" +
                    "	return Execute(commandText);" +
                    " };" +

                    " cachedb.Query = function(commandText) {" +
                    "	native function Query(commandText);" +
                    "	return Query(commandText);" +
                    " };" +

                    " cachedb.Close = function() {" +
                    "	native function Close();" +
                    "	return Close();" +
                    " };"+

					"})();";
            CefV8Handler ExtendsionHandler = new CwbJsExtendHandler(browser);
            CefRuntime.RegisterExtension("v8/cachedb", extensionCode, ExtendsionHandler);

            /*屏幕分辨率设置*/
            int w = webBrowser.screenWidth;
            int h = webBrowser.screenHeight;
            if (w > 0 && h > 0)
		    {
			    string jscode =
				     "Object.defineProperty(window.screen, 'height', {" +
				     "    get: function() {"+
				      "        return "+h+";"+
				      "    }"+
				      "});"+
				     "Object.defineProperty(window.screen, 'width', {"+
				     "    get: function() {"+
				     "        return "+w+";"+
				     "    }"+
				     "});";
			    frame.ExecuteJavaScript(jscode,frame.Url,0);
		    }
            /*注册执行C#方法*/
            CefV8Value globalValue = context.GetGlobal();
            CefV8Handler callHandler = new CwbJsExtendHandler(browser);
            CefV8Value callMethod = CefV8Value.CreateFunction("CallCSharpMethod", callHandler);
            globalValue.SetValue("CallCSharpMethod", callMethod, CefV8PropertyAttribute.None);
            base.OnContextCreated(browser, frame, context);
            
        }