1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 | using System; using System.Collections.Generic; using System.ComponentModel; using System.Data; using System.Drawing; using System.Linq; using System.Text; using System.Windows.Forms; using CefSharp; using System.Security.Permissions; using System.Runtime.InteropServices; using System.Configuration; using System.IO; namespace WindowsFormsApplication1 { //[PermissionSet(SecurityAction.Demand, Name = "FullTrust")] //[ComVisible(true)]//COM+组件可见 public partial class FrmWebCef : Form { public static string url= "" ; public FrmWebCef( string _url) { InitializeComponent(); try { url = ConfigurationManager.AppSettings[ "url" ].ToString(); } catch { } } private void Form1_Load( object sender, EventArgs e) { CefSettings settings = new CefSettings(); settings.Locale = "zh-CN" ; settings.CachePath = Directory.GetCurrentDirectory() + @"\Cache" ; //settings.AcceptLanguageList="zh-CN"; settings.CefCommandLineArgs.Add( "disable-application-cache" , "1" ); settings.CefCommandLineArgs.Add( "disable-session-storage" , "1" ); if (!Cef.IsInitialized) Cef.Initialize(settings); // Cef.Initialize(settings); CefSharp.WinForms.ChromiumWebBrowser wb = new CefSharp.WinForms.ChromiumWebBrowser(url); wb.Dock = DockStyle.Fill; // CefSharp.CefSettings settings = new CefSharp.CefSettings(); //// settings. = "zh-CN"; // settings.Locale = "zh-CN"; //// <html><head><meta http-equiv="content-type" content="text/html;charset=GBK"> ////</head><body><input type="text" value="send" id="input"> ////<input type="button" value="send" id="button" onclick="Cl();"> ////<script type="text/javascript"> //// document.getElementById('button').onclick = function () { //// debugger; //// alert('测试1'); //// // window.extend.Close('测试'); //// window.parent.postMessage('this is test!index1', '*'); //// }; //// function Cl() { //// debugger; //// alert('测试'); //// parent.postMessage('this is test!index', '*'); //// } ////</script></body></html> // <html><head><meta http-equiv="x-ua-compatible" content="IE=10,chrome=1"> //</head><body style="margin:0;padding:0;overflow:hidden" scroll="no"><iframe width="100%" height="100%" marginwidth="0" marginheight="0" border="0" src="http://zhenggc.cc/indext.html"></iframe> //<script type="text/javascript"> var mess=''; function receiveMessage(e) { alert('csharp _html:' +e.data);mess=e.data; csh.CloseMe(e.data);} window.addEventListener("message", receiveMessage, false);function go(){ window.external.Close(); } </script> </body></html> // Cef.Initialize(settings); // settings.l // CefSharpSettings.LegacyJavascriptBindingEnabled = true; string html = "<html><head><meta http-equiv=\"x-ua-compatible\" content=\"IE=10,chrome=1\"></head><body style=\"margin:0;padding:0;overflow:hidden\" scroll=\"no\"><iframe width=\"100%\" height=\"100%\" marginwidth=\"0\" marginheight=\"0\" border=\"0\" src=\"" + url + "\"></iframe>" + "<script type=\"text/javascript\"> var mess=''; function receiveMessage(e) { alert('csharp_html:' +e.data);mess=e.data; csh.CloseMe(e.data);} window.addEventListener(\"message\", receiveMessage, false); </script> </body></html>" ; wb.LoadHtml(html, "http://zhenggc.cn/indext.html" ); // wb.JsDialogHandler wb.RegisterJsObject( "csh" , this , false ); // wb.DownloadHandler = new CefSharpDownloadHandler(); this .Controls.Add(wb); } public void CloseMe( string mess) { MessageBox.Show( "cSHarp" + mess); if (mess.Equals( "kclose" )) { this .Close(); } } } } |