コード例 #1
0
ファイル: frmWelcome.cs プロジェクト: wsf1990/NanUI
        public frmWelcome()
            : base("embedded://www/index.html")             //设定启示页面,scheme是embedded就是我们在Main里注册的当前程序集资源
        {
            InitializeComponent();

            //在js中注册一个方法来打开About窗口
            GlobalObject.AddFunction("showAboutForm").Execute += (sender, args) =>
            {
                ShowAboutNanUI();
                //ShowAboutWindow();
            };

            GlobalObject.AddFunction("showDevTools").Execute += (sender, args) =>
            {
                ShowDevTools();
            };


            //网页加载完成时触发事件
            LoadHandler.OnLoadEnd += (sender, args) =>
            {
                //判断下触发的事件是不是主框架的
                if (args.Frame.IsMain)
                {
                    //执行JS,将当前的CEF运行版本等信息通过JS加载到网页上
                    var js = $"$client.setRuntimeInfo({{ api: ['{CfxRuntime.ApiHash(0)}', '{CfxRuntime.ApiHash(1)}'], cef:'{CfxRuntime.GetCefVersion()}', chrome:'{CfxRuntime.GetChromeVersion()}',os:'{CfxRuntime.PlatformOS}', arch:'{CfxRuntime.PlatformArch}'}});";
                    ExecuteJavascript(js);
                }
            };
        }
コード例 #2
0
        public MainForm() : base("embedded://www/index3.html")
        {
            InitializeComponent();
            //在js中注册一个方法来打开About窗口
            GlobalObject.AddFunction("showAboutForm").Execute += (sender, args) =>
            {
                ShowAboutNanUI();
                //ShowAboutWindow();
            };

            GlobalObject.AddFunction("showDevTools").Execute += (sender, args) =>
            {
                ShowDevTools();
            };

            LifeSpanHandler.OnBeforePopup += (sender, args) =>
            {
            };

            LifeSpanHandler.OnAfterCreated += (sender, args) =>
            {
            };


            //网页加载完成时触发事件
            LoadHandler.OnLoadEnd += (sender, args) =>
            {
                //判断下触发的事件是不是主框架的
                if (args.Frame.IsMain)
                {
                    //执行JS,将当前的CEF运行版本等信息通过JS加载到网页上
                    //var js = string.Format("$client.setRuntimeInfo({{ api: ['{CfxRuntime.ApiHash(0)}', '{CfxRuntime.ApiHash(1)}'], cef:'{CfxRuntime.GetCefVersion()}', chrome:'{CfxRuntime.GetChromeVersion()}',os:'{CfxRuntime.PlatformOS}', arch:'{CfxRuntime.PlatformArch}'}});");

                    var    js = string.Format("alert('{0}')", CfxRuntime.ApiHash(1));
                    object a  = CfxRuntime.GetCefVersion();
                    //ExecuteJavascript(js);



                    this.UpdateUI(() =>
                    {
                        //显示字窗体的过程,不解释
                        SubForm su = new SubForm();
                        su.Show();
                    });
                }
            };
        }
コード例 #3
0
        public BrowserForm()
        {
            InitializeComponent();

            LogWriteLine("CfxRuntime.ApiHash(0): " + CfxRuntime.ApiHash(0));
            LogWriteLine("CfxRuntime.ApiHash(1): " + CfxRuntime.ApiHash(1));
            LogWriteLine("CfxRuntime.GetCefVersion(): " + CfxRuntime.GetCefVersion());
            LogWriteLine("CfxRuntime.GetChromeVersion(): " + CfxRuntime.GetChromeVersion());
            LogWriteLine("Platform OS: {0}; Arch: {1}", CfxRuntime.PlatformOS, CfxRuntime.PlatformArch);
            LogWriteLine();

            ChromiumWebBrowser.RemoteProcessCreated += (e) => {
                LogWriteLine("Remote render process created with process id {0}", CfxRemoteCallContext.CurrentContext.ProcessId, CfxRemoteCallContext.CurrentContext.ThreadId);
                e.RenderProcessHandler.OnRenderThreadCreated += (s, e1) => {
                    LogWriteLine("RenderProcessHandler.OnRenderThreadCreated, process id = {0}", CfxRemoteCallContext.CurrentContext.ProcessId);
                };
                e.RenderProcessHandler.OnBrowserDestroyed += (s, e1) => {
                    // this is never reached.
                    LogWriteLine("RenderProcessHandler.OnBrowserDestroyed, process id = {0}, browser id = {1}", CfxRemoteCallContext.CurrentContext.ProcessId, e1.Browser.Identifier);
                };
                e.RenderProcessHandler.OnBrowserCreated += (s, e1) => {
                    LogWriteLine("RenderProcessHandler.OnBrowserCreated, process id = {0}, browser id = {1}", CfxRemoteCallContext.CurrentContext.ProcessId, e1.Browser.Identifier);
                };
                e.RenderProcessHandler.OnFocusedNodeChanged += (s, e1) => {
                    LogWriteLine("RenderProcessHandler.OnFocusedNodeChanged, process id = {0}, browser id = {1}, node = {2}", CfxRemoteCallContext.CurrentContext.ProcessId, e1.Browser.Identifier, e1.Node?.Name);
                };
            };

            LoadUrlButton.Click += new EventHandler(LoadUrlButton_Click);
            UrlTextBox.KeyDown  += new KeyEventHandler(UrlTextBox_KeyDown);

            JSHelloWorldButton.Click += new EventHandler(JSHelloWorldButton_Click);
            JSTestPageButton.Click   += new EventHandler(TestButton_Click);
            VisitDomButton.Click     += new EventHandler(VisitDomButton_Click);

            WebBrowser.GlobalObject.AddFunction("CfxHelloWorld").Execute      += CfxHelloWorld_Execute;
            WebBrowser.GlobalObject.AddFunction("testDoubleCallback").Execute += TestDoubleCallback_Execute;

            // related to issue #65
            WebBrowser.GlobalObject.AddFunction("ArrayTestCallback").Execute += (s, e1) => {
                var array = e1.Arguments[0];
                var v0    = array.GetValue(0);
                var v1    = array.GetValue(1);
                if (v0 != null)
                {
                    LogWriteLine("Array test function works: v0 = {0}, v1 = {1}", v0.IntValue, v1.IntValue);
                }
                else
                {
                    LogWriteLine("Array test function: array is broken.");
                }
            };

            // related to pull request #1
            WebBrowser.GlobalObject.AddFunction("ListKeysInDocumentObject").Execute += (s, e1) => {
                var           doc  = e1.Arguments[0];
                List <string> keys = new List <string>();
                if (doc.GetKeys(keys))
                {
                    LogWriteLine("document has {0} keys:", keys.Count);
                    keys.ForEach(k => LogWriteLine(k));
                }
                else
                {
                    LogWriteLine("GetKeys returned false.");
                }
            };

            WebBrowser.GlobalObject.Add("TestObject", new JsTestObject(this));


            var sleepFunction = new JSFunction(JSInvokeMode.DontInvoke);

            sleepFunction.Execute += (s, e) => {
                LogWriteLine("Sleep function: sleep 5 seconds...");
                Thread.Sleep(5000);
                try {
                    var x = e.Arguments[0].IntValue;
                    LogWriteLine("Sleep function: Event args accessed sucessfully.");
                } catch (Exception ex) {
                    LogWriteLine("Sleep function: Error accessing event args:");
                    LogWriteLine(ex.ToString());
                }
            };

            WebBrowser.GlobalObject.Add("SleepFunction", sleepFunction);

            var html = @"

                <html>
                <head><script>
                    function testlog(text) {
                        document.getElementById('testfunc_result').innerHTML += '<br>' + text;
                    }
                </script>
                <script>
                    function doubleCallback(arg1, arg2) {
                        testlog('Function doubleCallback() called. Arguments:');
                        testlog(arg1);
                        testlog(arg2);
                        return 'This text is returned from doubleCallback()';
                    }
                    function clearTestLog() {
                        document.getElementById('testfunc_result').innerHTML = '';
                    }
                </script>
                </head>
                <body>
                    <br><br><b>Local resource/javascript integration test page.</b>
                    <hr><br><br>
                    Local resource image:<br>
                    <img src='http://localresource/image'><br><br>
                    <a href='http://www.google.com/' onclick=""window.open('http://www.google.com/', 'Popup test', 'width=800,height=600,scrollbars=yes'); return false;"">open popup window.open</a>
                    <a href='http://www.google.com/' target=blank>open popup target=blank</a>
                    <br><br>
                    <button id='testbutton1' onclick=""document.getElementById('testfunc_result').innerHTML += '<br>' + CfxHelloWorld('this is the hello world function');"">Execute CfxHelloWorld()</button>
                    <button id='testbutton2' onclick=""
                        testlog('TestObject = ' + TestObject);
                        testlog('TestObject.testFunction = ' + TestObject.testFunction);
                        TestObject.testFunction('this is the test function');
                    "">Execute TestObject.testFunction()</button>
                    <button id='testbutton3' onclick=""
                        testlog('TestObject = ' + TestObject);
                        testlog('TestObject.anotherObject = ' + TestObject.anotherObject);
                        testlog('TestObject.anotherObject.anotherTestFunction = ' + TestObject.anotherObject.anotherTestFunction);
                        testlog(TestObject.anotherObject.anotherTestFunction('this is the other test function'));
                    "">Execute TestObject.anotherObject.anotherTestFunction()</button>
                    <button id='testbutton4' onclick=""
                        testlog('TestObject.dynamicProperty = ' + TestObject.dynamicProperty);
                        testlog('(define TestObject.dynamicProperty += 100)');
                        TestObject.dynamicProperty += 100;
                        testlog('TestObject.dynamicProperty = ' + TestObject.dynamicProperty);
                    "">Defined TestObject properties</button>
                    <button id='testbutton5' onclick=""
                        testlog('TestObject.foo = ' + TestObject.foo);
                        testlog('(define TestObject.foo = 100)');
                        TestObject.foo = 100;
                        testlog('TestObject.foo = ' + TestObject.foo);
                    "">Undefined TestObject properties</button>
                    <button id='testbutton6' onclick=""
                        testlog('Call native function testDoubleCallback(doubleCallback). Return value:');
                        testlog('Return value: ' + testDoubleCallback(doubleCallback));
                    "">Double Callback</button>
                    <br><br><div id='testfunc_result'></div>
            ";

            WebBrowser.SetWebResource("http://localresource/text.html", new Chromium.WebBrowser.WebResource(html));

            var bm = new System.Drawing.Bitmap(100, 100);

            using (var g = System.Drawing.Graphics.FromImage(bm)) {
                g.FillRectangle(System.Drawing.Brushes.Yellow, 0, 0, 99, 99);
                g.DrawRectangle(System.Drawing.Pens.Black, 0, 0, 99, 99);
                g.DrawLine(System.Drawing.Pens.Black, 0, 0, 99, 99);
            }
            WebBrowser.SetWebResource("http://localresource/image", new Chromium.WebBrowser.WebResource(bm));

            WebBrowser.DisplayHandler.OnConsoleMessage += (s, e) => LogCallback(s, e);
            WebBrowser.DisplayHandler.OnTitleChange    += (s, e) => LogCallback(s, e);
            WebBrowser.DisplayHandler.OnStatusMessage  += (s, e) => LogCallback(s, e);

            WebBrowser.DisplayHandler.OnTitleChange += (s, e) => {
                var wb = ChromiumWebBrowser.FromCfxBrowser(e.Browser);
                LogWriteLine("ChromiumWebBrowser.FromCfxBrowser(e.Browser) == WebBrowser: {0}", wb == WebBrowser);
                var title = e.Title;
                BeginInvoke((MethodInvoker)(() => Text = "ChromiumWebBrowser - " + title));
            };

            WebBrowser.LifeSpanHandler.OnBeforePopup += (s, e) => {
                LogCallback(s, e);
            };

            WebBrowser.LoadHandler.OnLoadingStateChange += (s, e) => {
                if (e.IsLoading)
                {
                    return;
                }
                BeginInvoke((MethodInvoker)(() => {
                    UrlTextBox.Text = WebBrowser.Url.ToString();
                }));
            };

            WebBrowser.LoadUrl("http://localresource/text.html");

            WebBrowser.FindToolbar.Visible = true;

            WebBrowser.OnV8ContextCreated += (s, e) => {
                if (e.Frame.IsMain)
                {
                    CfrV8Value     retval;
                    CfrV8Exception exception;
                    if (e.Context.Eval("CfxHelloWorld()", null, 0, out retval, out exception))
                    {
                        LogWriteLine("OnV8ContextCreated: Eval succeeded, retval is '{0}'", retval.StringValue);
                    }
                    else
                    {
                        LogWriteLine("OnV8ContextCreated: Eval failed, exception is '{0}'", exception.Message);
                    }
                }
            };

            WebBrowser.GlobalObject.AddFunction("SubmitAsyncTestFunction").Execute += JS_SubmitAsyncTestFunction;
            WebBrowser.GlobalObject.AddFunction("bigStringFunction").Execute       += JS_bigStringFunction;
        }