using System; using System.Diagnostics; using System.Windows.Forms; using UAS_Web.tool; using CefSharp; using CefSharp.WinForms; using System.Text.RegularExpressions; using System.Collections.Generic; using System.Net; using System.Text; using System.IO; using System.Web; using System.Threading; namespace UAS_Web { public partial class Browser : Form { private ChromiumWebBrowser webBrowser; public Browser() { InitializeComponent(); Text = "供应商条码打印"; string path = "http://113.108.109.210:8099/ERP/vendbarcode/relogin.action"; webBrowser = new ChromiumWebBrowser(path) { Dock = DockStyle.Fill }; CheckForIllegalCrossThreadCalls = false; webBrowser.MenuHandler = new MenuHandler(); webBrowser.RequestHandler = new RequestHandler(); webBrowser.DownloadHandler = new DownLoadFile(); Controls.Add(webBrowser); webBrowser.LoadError += WebBrowser_LoadError; webBrowser.FrameLoadEnd += FrameLoadEnd; webBrowser.FrameLoadStart += WebBrowser_FrameLoadStart; webBrowser.KeyboardHandler = new CEFKeyBoardHander(); } private void WebBrowser_FrameLoadStart(object sender, FrameLoadStartEventArgs e) { } void FrameLoadEnd(object sender, FrameLoadEndEventArgs e) { Thread.Sleep(500); var cookiemanager = CefSharp.Cef.GetGlobalCookieManager(); CookieVisitor visitor = new CookieVisitor(); visitor.SendCookie += visitor_SendCookie; cookiemanager.VisitAllCookies(visitor); if (e.Url.Contains("vendbarcode/login.jsp") || e.Url.Contains("relogin.action")) { e.Browser.FocusedFrame.EvaluateScriptAsync("document.getElementById('username').value = '" + BaseUtil.GetCacheData("username") + "';document.getElementById('password').value = '" + BaseUtil.GetCacheData("password") + "';document.getElementById('master').innerHTML = '" + BaseUtil.GetCacheData("master_fun") + "';document.getElementById('master1').value = '" + BaseUtil.GetCacheData("master_name") + "';", new TimeSpan(600000)); return; } } private void visitor_SendCookie(CefSharp.Cookie obj) { if (obj.Path.Contains("vendbarcode")) { if (obj.Name == "username") BaseUtil.SetCacheData("username", HttpUtility.UrlDecode(obj.Value)); if (obj.Name == "password") BaseUtil.SetCacheData("password", HttpUtility.UrlDecode(obj.Value)); if (obj.Name == "master_name") BaseUtil.SetCacheData("master_name", HttpUtility.UrlDecode(obj.Value)); if (obj.Name == "master_fun") BaseUtil.SetCacheData("master_fun", HttpUtility.UrlDecode(obj.Value)); } } private void WebBrowser_LoadError(object sender, LoadErrorEventArgs e) { ////指定放大镜 //if (e.FailedUrl.Contains("vendbarcode/batchdelivery.jsp?whoami=Delivery!Deal#")) // return; //if (e.FailedUrl.Contains("common/excel")) // return; //if (e.FailedUrl.Contains("common/downloadbyId.action")) // return; //MessageBox.Show("当前网络无法访问" + e.ErrorText); } private void Browser_FormClosing(object sender, FormClosingEventArgs e) { webBrowser.GetBrowser().CloseBrowser(true); Process.GetCurrentProcess().Kill(); } private void Browser_Load(object sender, EventArgs e) { Process[] pro = Process.GetProcessesByName("UAS_Web"); for (int i = 0; i < pro.Length; i++) { if (pro[i].Id != Process.GetCurrentProcess().Id) { pro[i].Kill(); } } textBox1.Text = webBrowser.Address; } private void textBox1_KeyDown(object sender, KeyEventArgs e) { if (e.KeyCode == Keys.Enter) { webBrowser.Load(textBox1.Text); } } } public class CEFKeyBoardHander : IKeyboardHandler { public bool OnKeyEvent(IWebBrowser browserControl, IBrowser browser, KeyType type, int windowsKeyCode, int nativeKeyCode, CefEventFlags modifiers, bool isSystemKey) { if (type == KeyType.KeyUp && Enum.IsDefined(typeof(Keys), windowsKeyCode)) { var key = (Keys)windowsKeyCode; switch (key) { case Keys.F12: browser.ShowDevTools(); break; } } return false; } public bool OnPreKeyEvent(IWebBrowser browserControl, IBrowser browser, KeyType type, int windowsKeyCode, int nativeKeyCode, CefEventFlags modifiers, bool isSystemKey, ref bool isKeyboardShortcut) { return false; } } }