using System; using System.Collections.Generic; using System.IO; using System.Windows.Forms; using System.Data; using System.Text; using System.ComponentModel; using Microsoft.Win32; using Newtonsoft.Json; using System.Net; using System.Runtime.Serialization; using System.Runtime.Serialization.Formatters.Binary; using CefSharp; using System.Resources; using System.Drawing; using CefSharp.WinForms; using System.Reflection; namespace felixyin { public class JsEvent { /* private BackgroundWorker backgroundWorker1; public JsEvent() { this.backgroundWorker1 = new System.ComponentModel.BackgroundWorker(); } */ public string MessageText { get; set; } public void ShowTest() { MessageBox.Show("this in C#.\n\r" + MessageText); } public void ShowTestArg(string ss) { MessageBox.Show("收到JS带参数调用\n\r" + ss); } public void domReady() { Program.showMainForm(); //this.backgroundWorker1.RunWorkerAsync(); } public void toggleDevTools() { Program.toggleDevTools(); } /* private void backgroundWorker1_RunWorkerCompleted( object sender, RunWorkerCompletedEventArgs e) { Program.showMainForm(); } */ } public partial class MainForm : Form { private UsbScanerHook listener = new UsbScanerHook(); private NotifyIcon nIcon; private ContextMenu cm; public MainForm() { Control.CheckForIllegalCrossThreadCalls = false; var settings = new CefSettings(); // Set BrowserSubProcessPath based on app bitness at runtime settings.BrowserSubprocessPath = Path.Combine(AppDomain.CurrentDomain.SetupInformation.ApplicationBase, Environment.Is64BitProcess ? "x64" : "x86", "CefSharp.BrowserSubprocess.exe"); // Make sure you set performDependencyCheck false // var osVersion = Environment.OSVersion; // Disable GPU in WPF and Offscreen examples until #1634 has been resolved6 // settings.CefCommandLineArgs.Add("disable-gpu", "1"); Cef.Initialize(settings, performDependencyCheck: false, browserProcessHandler: null); // Cef.EnableHighDPISupport(); string url = "http://gongchang.qdbak.com/"; InitializeComponent(url); // FIXME 生产环境,需要修改为4404 // this.webBrowser1.Load(url); // 已废弃 //this.webBrowser1.ContextMenu = new ContextMenu(); // js 调用 .net CefSharpSettings.LegacyJavascriptBindingEnabled = true; this.webBrowser1.RegisterJsObject("jsObj", new JsEvent(), new CefSharp.BindingOptions() { CamelCaseJavascriptNames = false }); //交互数据 showNotifyIcon(); verifyStartSuccess(url); } private void verifyStartSuccess(string url) { bool isOk = ResponseOk(url); if (!isOk) { DialogResult result = MessageBox.Show("程序发生未知错误,请联系管理员!", "佰安客科技提示您", MessageBoxButtons.OK, MessageBoxIcon.Information); if (result == DialogResult.OK) { System.Environment.Exit(0); } } this.listener.ScanerEvent += Listener_ScanerEvent; listener.Start(); } private void showNotifyIcon() { this.nIcon = new NotifyIcon(); ResourceManager manager = new ResourceManager(typeof(felixyin.Properties.Resources)); Icon icon = (Icon)manager.GetObject("logo"); this.nIcon.Text = "佰安客工厂管理系统";//鼠标悬停系统托盘上显示 this.nIcon.Icon = icon;//图标 this.nIcon.Visible = true;//设置图标可见 cm = new ContextMenu(); MenuItem guanYu = new MenuItem("关于"); guanYu.Click += new EventHandler(this.guanYu_Click); cm.MenuItems.Add(guanYu);//添加托盘菜单项 MenuItem addAutoStart = new MenuItem("设置随系统自动启动"); addAutoStart.Click += new EventHandler(this.addAutoStart_Click); cm.MenuItems.Add(addAutoStart); MenuItem delAutoStart = new MenuItem("取消随系统自动启动"); delAutoStart.Click += new EventHandler(this.delAutoStart_Click); cm.MenuItems.Add(delAutoStart); MenuItem clearCache = new MenuItem("强制刷新(无缓存)"); clearCache.Click += new EventHandler(this.delClearCache_Click); cm.MenuItems.Add(clearCache); MenuItem exit = new MenuItem("退出"); exit.Click += new EventHandler(this.exit_Click); cm.MenuItems.Add(exit); nIcon.ContextMenu = cm;//托盘菜单项在图标右键时显示 this.nIcon.ShowBalloonTip(0x2710, "佰安客科技提示您", "程序正在初始化,大约需要10秒。右键点击图标可显示菜单,左键双击可显示或隐藏程序。", ToolTipIcon.Info);//托盘图标加载时提示 nIcon.Click += new EventHandler(this.nIcon_Click);//委托点击事 nIcon.MouseDoubleClick += new MouseEventHandler(this.nIcon_MouseDoubleClick); } private void exit_Click(object sender, EventArgs e) { Application.Exit(); } private void delAutoStart_Click(object sender, EventArgs e) { Program.AutoStart(false); MessageBox.Show("取消随机自动启动会导致无法接收扫码枪信号!!!\n随系统自动启动已取消", "佰安客科技提示您"); } private void addAutoStart_Click(object sender, EventArgs e) { Program.AutoStart(true); MessageBox.Show("已开启随系统自动启动", "佰安客科技提示您"); } private void delClearCache_Click(object sender, EventArgs e) { this.webBrowser1.Reload(true); MessageBox.Show("强制刷新需要10秒以上的执行时间,请耐心等待!", "佰安客科技提示您"); } private void guanYu_Click(object sender, EventArgs e) { System.Diagnostics.Process.Start("http://www.qdqtrj.com"); } private void nIcon_Click(object sender, EventArgs e) { // System.Diagnostics.Process.Start("http://www.qdqtrj.com"); } Boolean isShow = true; private void nIcon_MouseDoubleClick(object sender, MouseEventArgs e) { // nIcon.Visible = false; if (isShow) { this.Hide(); this.isShow = false; } else { this.isShow = true; this.Show(); WindowState = FormWindowState.Maximized; this.Focus(); } } private void Listener_ScanerEvent(UsbScanerHook.ScanerCodes codes) { // Console.WriteLine(codes.Result); // .net 调用 js this.webBrowser1.GetBrowser().MainFrame.EvaluateScriptAsync("scanningGunListener( '" + codes.Result + "')");//运行页面上js的testArg带参数的方法 } /// /// Get方式获取url地址输出内容 /// /// url public static bool ResponseOk(String url) { try { HttpWebRequest webRequest = (HttpWebRequest)WebRequest.Create(url); webRequest.Method = "GET"; HttpWebResponse webResponse = (HttpWebResponse)webRequest.GetResponse(); webResponse.Close(); Console.WriteLine(webResponse.StatusCode); return true; } catch { return false; } } private Boolean isCls = false; public void closeWin() { isCls = true; this.Close(); } private void Form1_FormClosing_1(object sender, FormClosingEventArgs e) { if (isCls) { e.Cancel = false; } else { // this.webBrowser1.Document.InvokeScript("beforeClose"); DialogResult result = MessageBox.Show("关闭后扫码枪无法与系统连接,您确定要关闭吗!!!", "佰安客科技提示您", MessageBoxButtons.OKCancel, MessageBoxIcon.Information); if (result == DialogResult.OK) { this.nIcon.Visible = false; e.Cancel = false; //点击OK this.listener.Stop(); } else { e.Cancel = true; } } } } }