MainForm.cs 9.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275
  1. using System;
  2. using System.Collections.Generic;
  3. using System.IO;
  4. using System.Windows.Forms;
  5. using System.Data;
  6. using System.Text;
  7. using System.ComponentModel;
  8. using Microsoft.Win32;
  9. using Newtonsoft.Json;
  10. using System.Net;
  11. using System.Runtime.Serialization;
  12. using System.Runtime.Serialization.Formatters.Binary;
  13. using CefSharp;
  14. using System.Resources;
  15. using System.Drawing;
  16. using CefSharp.WinForms;
  17. using System.Reflection;
  18. namespace felixyin
  19. {
  20. public class JsEvent
  21. {
  22. /* private BackgroundWorker backgroundWorker1;
  23. public JsEvent()
  24. {
  25. this.backgroundWorker1 = new System.ComponentModel.BackgroundWorker();
  26. }
  27. */
  28. public string MessageText { get; set; }
  29. public void ShowTest()
  30. {
  31. MessageBox.Show("this in C#.\n\r" + MessageText);
  32. }
  33. public void ShowTestArg(string ss)
  34. {
  35. MessageBox.Show("收到JS带参数调用\n\r" + ss);
  36. }
  37. public void domReady()
  38. {
  39. Program.showMainForm();
  40. //this.backgroundWorker1.RunWorkerAsync();
  41. }
  42. public void toggleDevTools()
  43. {
  44. Program.toggleDevTools();
  45. }
  46. /*
  47. private void backgroundWorker1_RunWorkerCompleted(
  48. object sender,
  49. RunWorkerCompletedEventArgs e)
  50. {
  51. Program.showMainForm();
  52. }
  53. */
  54. }
  55. public partial class MainForm : Form
  56. {
  57. private UsbScanerHook listener = new UsbScanerHook();
  58. private NotifyIcon nIcon;
  59. private ContextMenu cm;
  60. public MainForm()
  61. {
  62. Control.CheckForIllegalCrossThreadCalls = false;
  63. var settings = new CefSettings();
  64. // Set BrowserSubProcessPath based on app bitness at runtime
  65. settings.BrowserSubprocessPath = Path.Combine(AppDomain.CurrentDomain.SetupInformation.ApplicationBase,
  66. Environment.Is64BitProcess ? "x64" : "x86",
  67. "CefSharp.BrowserSubprocess.exe");
  68. // Make sure you set performDependencyCheck false
  69. // var osVersion = Environment.OSVersion;
  70. // Disable GPU in WPF and Offscreen examples until #1634 has been resolved6
  71. // settings.CefCommandLineArgs.Add("disable-gpu", "1");
  72. Cef.Initialize(settings, performDependencyCheck: false, browserProcessHandler: null);
  73. // Cef.EnableHighDPISupport();
  74. string url = "http://gongchang.qdbak.com/";
  75. InitializeComponent(url);
  76. // FIXME 生产环境,需要修改为4404
  77. // this.webBrowser1.Load(url); // 已废弃
  78. //this.webBrowser1.ContextMenu = new ContextMenu();
  79. // js 调用 .net
  80. CefSharpSettings.LegacyJavascriptBindingEnabled = true;
  81. this.webBrowser1.RegisterJsObject("jsObj", new JsEvent(), new CefSharp.BindingOptions() { CamelCaseJavascriptNames = false }); //交互数据
  82. showNotifyIcon();
  83. verifyStartSuccess(url);
  84. }
  85. private void verifyStartSuccess(string url)
  86. {
  87. bool isOk = ResponseOk(url);
  88. if (!isOk)
  89. {
  90. DialogResult result = MessageBox.Show("程序发生未知错误,请联系管理员!", "佰安客科技提示您", MessageBoxButtons.OK, MessageBoxIcon.Information);
  91. if (result == DialogResult.OK)
  92. {
  93. System.Environment.Exit(0);
  94. }
  95. }
  96. this.listener.ScanerEvent += Listener_ScanerEvent;
  97. listener.Start();
  98. }
  99. private void showNotifyIcon()
  100. {
  101. this.nIcon = new NotifyIcon();
  102. ResourceManager manager = new ResourceManager(typeof(felixyin.Properties.Resources));
  103. Icon icon = (Icon)manager.GetObject("logo");
  104. this.nIcon.Text = "佰安客工厂管理系统";//鼠标悬停系统托盘上显示
  105. this.nIcon.Icon = icon;//图标
  106. this.nIcon.Visible = true;//设置图标可见
  107. cm = new ContextMenu();
  108. MenuItem guanYu = new MenuItem("关于");
  109. guanYu.Click += new EventHandler(this.guanYu_Click);
  110. cm.MenuItems.Add(guanYu);//添加托盘菜单项
  111. MenuItem addAutoStart = new MenuItem("设置随系统自动启动");
  112. addAutoStart.Click += new EventHandler(this.addAutoStart_Click);
  113. cm.MenuItems.Add(addAutoStart);
  114. MenuItem delAutoStart = new MenuItem("取消随系统自动启动");
  115. delAutoStart.Click += new EventHandler(this.delAutoStart_Click);
  116. cm.MenuItems.Add(delAutoStart);
  117. MenuItem clearCache = new MenuItem("强制刷新(无缓存)");
  118. clearCache.Click += new EventHandler(this.delClearCache_Click);
  119. cm.MenuItems.Add(clearCache);
  120. MenuItem exit = new MenuItem("退出");
  121. exit.Click += new EventHandler(this.exit_Click);
  122. cm.MenuItems.Add(exit);
  123. nIcon.ContextMenu = cm;//托盘菜单项在图标右键时显示
  124. this.nIcon.ShowBalloonTip(0x2710, "佰安客科技提示您", "程序正在初始化,大约需要10秒。右键点击图标可显示菜单,左键双击可显示或隐藏程序。", ToolTipIcon.Info);//托盘图标加载时提示
  125. nIcon.Click += new EventHandler(this.nIcon_Click);//委托点击事
  126. nIcon.MouseDoubleClick += new MouseEventHandler(this.nIcon_MouseDoubleClick);
  127. }
  128. private void exit_Click(object sender, EventArgs e)
  129. {
  130. Application.Exit();
  131. }
  132. private void delAutoStart_Click(object sender, EventArgs e)
  133. {
  134. Program.AutoStart(false);
  135. MessageBox.Show("取消随机自动启动会导致无法接收扫码枪信号!!!\n随系统自动启动已取消", "佰安客科技提示您");
  136. }
  137. private void addAutoStart_Click(object sender, EventArgs e)
  138. {
  139. Program.AutoStart(true);
  140. MessageBox.Show("已开启随系统自动启动", "佰安客科技提示您");
  141. }
  142. private void delClearCache_Click(object sender, EventArgs e)
  143. {
  144. this.webBrowser1.Reload(true);
  145. MessageBox.Show("强制刷新需要10秒以上的执行时间,请耐心等待!", "佰安客科技提示您");
  146. }
  147. private void guanYu_Click(object sender, EventArgs e)
  148. {
  149. System.Diagnostics.Process.Start("http://www.qdqtrj.com");
  150. }
  151. private void nIcon_Click(object sender, EventArgs e)
  152. {
  153. // System.Diagnostics.Process.Start("http://www.qdqtrj.com");
  154. }
  155. Boolean isShow = true;
  156. private void nIcon_MouseDoubleClick(object sender, MouseEventArgs e)
  157. {
  158. // nIcon.Visible = false;
  159. if (isShow)
  160. {
  161. this.Hide();
  162. this.isShow = false;
  163. }
  164. else
  165. {
  166. this.isShow = true;
  167. this.Show();
  168. WindowState = FormWindowState.Maximized;
  169. this.Focus();
  170. }
  171. }
  172. private void Listener_ScanerEvent(UsbScanerHook.ScanerCodes codes)
  173. {
  174. // Console.WriteLine(codes.Result);
  175. // .net 调用 js
  176. this.webBrowser1.GetBrowser().MainFrame.EvaluateScriptAsync("scanningGunListener( '" + codes.Result + "')");//运行页面上js的testArg带参数的方法
  177. }
  178. /// <summary>
  179. /// Get方式获取url地址输出内容
  180. /// </summary> /// <param name="url">url</param>
  181. public static bool ResponseOk(String url)
  182. {
  183. try
  184. {
  185. HttpWebRequest webRequest = (HttpWebRequest)WebRequest.Create(url);
  186. webRequest.Method = "GET";
  187. HttpWebResponse webResponse = (HttpWebResponse)webRequest.GetResponse();
  188. webResponse.Close();
  189. Console.WriteLine(webResponse.StatusCode);
  190. return true;
  191. }
  192. catch
  193. {
  194. return false;
  195. }
  196. }
  197. private Boolean isCls = false;
  198. public void closeWin()
  199. {
  200. isCls = true;
  201. this.Close();
  202. }
  203. private void Form1_FormClosing_1(object sender, FormClosingEventArgs e)
  204. {
  205. if (isCls)
  206. {
  207. e.Cancel = false;
  208. }
  209. else
  210. {
  211. // this.webBrowser1.Document.InvokeScript("beforeClose");
  212. DialogResult result = MessageBox.Show("关闭后扫码枪无法与系统连接,您确定要关闭吗!!!", "佰安客科技提示您", MessageBoxButtons.OKCancel, MessageBoxIcon.Information);
  213. if (result == DialogResult.OK)
  214. {
  215. this.nIcon.Visible = false;
  216. e.Cancel = false; //点击OK
  217. this.listener.Stop();
  218. }
  219. else
  220. {
  221. e.Cancel = true;
  222. }
  223. }
  224. }
  225. }
  226. }