MainForm.cs 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507
  1. using DevExpress.XtraSplashScreen;
  2. using GCAS.Actions;
  3. using GCAS.Code;
  4. using GCAS.Dto;
  5. using GCAS.Localization;
  6. using GCAS.Model;
  7. using GCAS.Reports;
  8. using System;
  9. using System.Collections.Generic;
  10. using System.ComponentModel;
  11. using System.Linq;
  12. using System.Threading;
  13. using System.Windows.Forms;
  14. namespace GCAS
  15. {
  16. public partial class MainForm : DevExpress.XtraEditors.XtraForm
  17. {
  18. private List<EntranceModel> entranceModels;
  19. private EntranceRepository EntranceRepository;
  20. private List<DeviceModel> deviceModels;
  21. private DeviceRepository deviceRepository;
  22. private List<NameValue> L;
  23. public OperatorModel Operator;
  24. public MainForm()
  25. {
  26. InitializeComponent();
  27. L = LocalizationHelper.GetSource(Thread.CurrentThread.CurrentUICulture);
  28. mdiManager.ClosePageButtonShowMode = DevExpress.XtraTab.ClosePageButtonShowMode.InAllTabPagesAndTabControlHeader;
  29. helpProvider1.HelpNamespace = Application.StartupPath + $"/Sources/{Thread.CurrentThread.CurrentUICulture.Name}.chm";
  30. helpProvider1.SetShowHelp(this, true);
  31. deviceRepository = new DeviceRepository();
  32. EntranceRepository = new EntranceRepository();
  33. }
  34. private void MainForm_Load(object sender, EventArgs e)
  35. {
  36. bar_operator.Caption = L.GetString("operator") + Operator?.Name;
  37. mdiManager.MdiParent = this;
  38. timer_currentTime.Interval = 1000;
  39. timer_currentTime.Start();
  40. if (Operator.Role == 1)
  41. {
  42. nbg_user.Visible = false;
  43. nbg_actions.Visible = false;
  44. }
  45. ShowMessage();
  46. entranceModels = EntranceRepository.GetList();
  47. deviceModels = deviceRepository.GetList();
  48. if (deviceModels.Count > 1)
  49. {
  50. if (entranceModels.Count > 2)
  51. {
  52. RealTimeForm form = new RealTimeForm
  53. {
  54. currentUser = Operator,
  55. MdiParent = this
  56. };
  57. form.Show();
  58. }
  59. else
  60. {
  61. RealTimeTwoCarForm form = new RealTimeTwoCarForm
  62. {
  63. currentUser = Operator,
  64. MdiParent = this
  65. };
  66. form.Show();
  67. }
  68. }
  69. else
  70. {
  71. if (entranceModels.Count > 1)
  72. {
  73. RealTimeOneCarTwoEntanceForm form = new RealTimeOneCarTwoEntanceForm
  74. {
  75. currentUser = Operator,
  76. MdiParent = this
  77. };
  78. form.Show();
  79. }
  80. else
  81. {
  82. RealTimeOneCarForm form = new RealTimeOneCarForm
  83. {
  84. currentUser = Operator,
  85. MdiParent = this
  86. };
  87. form.Show();
  88. }
  89. }
  90. HideMessage();
  91. }
  92. public void theout(object source, System.Timers.ElapsedEventArgs e)
  93. {
  94. if (IsHandleCreated)
  95. {
  96. var forms = MdiChildren;
  97. if (forms.Length > 0)
  98. {
  99. Invoke(new EventHandler(delegate
  100. {
  101. Form form = forms[new Random().Next(0, forms.Length - 1)];
  102. if (form.Name == "RealTimeForm")
  103. {
  104. var realForm = form as RealTimeForm;
  105. realForm.BindTable(null);
  106. realForm.BindChart();
  107. }
  108. form.Activate();
  109. }));
  110. }
  111. }
  112. }
  113. public void chartRef(object source, System.Timers.ElapsedEventArgs e)
  114. {
  115. if (IsHandleCreated)
  116. {
  117. var form = MdiChildren.FirstOrDefault(c => c.Name == "RealTimeForm");
  118. if (form != null)
  119. {
  120. Invoke(new EventHandler(delegate
  121. {
  122. var realForm = form as RealTimeForm;
  123. realForm.BindChart();
  124. }));
  125. }
  126. }
  127. }
  128. private void timer_currentTime_Tick(object sender, EventArgs e)
  129. {
  130. bar_time.Caption = DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss");
  131. }
  132. private void btn_singout_ItemClick(object sender, DevExpress.XtraBars.ItemClickEventArgs e)
  133. {
  134. DialogResult = DialogResult.Retry;
  135. Close();
  136. }
  137. private void nbi_device_LinkClicked(object sender, DevExpress.XtraNavBar.NavBarLinkEventArgs e)
  138. {
  139. if (!MdiChildren.Any(c => c.Name == "DeviceForm"))
  140. {
  141. DeviceForm form = new DeviceForm
  142. {
  143. currentUser = Operator,
  144. MdiParent = this
  145. };
  146. form.Show();
  147. }
  148. else
  149. {
  150. MdiChildren.First(c => c.Name == "DeviceForm").Activate();
  151. }
  152. }
  153. private void nbi_main_realtime_LinkClicked(object sender, DevExpress.XtraNavBar.NavBarLinkEventArgs e)
  154. {
  155. if (MdiChildren.Any(c => c.Name == "RealTimeForm"))
  156. {
  157. MdiChildren.First(c => c.Name == "RealTimeForm").Activate();
  158. }
  159. else if (MdiChildren.Any(c => c.Name == "RealTimeOneCarForm"))
  160. {
  161. MdiChildren.First(c => c.Name == "RealTimeOneCarForm").Activate();
  162. }
  163. else if (MdiChildren.Any(c => c.Name == "RealTimeTwoCarForm"))
  164. {
  165. MdiChildren.First(c => c.Name == "RealTimeTwoCarForm").Activate();
  166. }
  167. else if (MdiChildren.Any(c => c.Name == "RealTimeOneCarTwoEntanceForm"))
  168. {
  169. MdiChildren.First(c => c.Name == "RealTimeOneCarTwoEntanceForm").Activate();
  170. }
  171. else {
  172. ShowMessage();
  173. if (deviceModels.Count > 1)//两台车用两车的显示页面
  174. {
  175. if (entranceModels.Count>2)//两车两料口
  176. {
  177. RealTimeForm form = new RealTimeForm
  178. {
  179. currentUser = Operator,
  180. MdiParent = this
  181. };
  182. form.Show();
  183. }
  184. else//两车三料口
  185. {
  186. RealTimeOneCarForm form = new RealTimeOneCarForm
  187. {
  188. currentUser = Operator,
  189. MdiParent = this
  190. };
  191. form.Show();
  192. }
  193. }
  194. else
  195. {
  196. if (entranceModels.Count > 1)//一车两料口
  197. {
  198. RealTimeOneCarTwoEntanceForm form = new RealTimeOneCarTwoEntanceForm
  199. {
  200. currentUser = Operator,
  201. MdiParent = this
  202. };
  203. form.Show();
  204. }
  205. else//一车一料口
  206. {
  207. RealTimeOneCarForm form = new RealTimeOneCarForm
  208. {
  209. currentUser = Operator,
  210. MdiParent = this
  211. };
  212. form.Show();
  213. }
  214. }
  215. HideMessage();
  216. }
  217. }
  218. private void nbi_main_record_LinkClicked(object sender, DevExpress.XtraNavBar.NavBarLinkEventArgs e)
  219. {
  220. if (!MdiChildren.Any(c => c.Name == "RecordForm"))
  221. {
  222. RecordForm form = new RecordForm
  223. {
  224. currentUser = Operator,
  225. MdiParent = this
  226. };
  227. form.Show();
  228. }
  229. else
  230. {
  231. MdiChildren.First(c => c.Name == "RecordForm").Activate();
  232. }
  233. }
  234. private void btn_sysset_ItemClick(object sender, DevExpress.XtraBars.ItemClickEventArgs e)
  235. {
  236. SystemSetForm form = new SystemSetForm
  237. {
  238. currentUser = Operator,
  239. };
  240. form.ShowDialog();
  241. }
  242. private void nbi_user_list_LinkClicked(object sender, DevExpress.XtraNavBar.NavBarLinkEventArgs e)
  243. {
  244. if (!MdiChildren.Any(c => c.Name == "OperatorForm"))
  245. {
  246. OperatorForm form = new OperatorForm
  247. {
  248. currentUser = Operator,
  249. MdiParent = this
  250. };
  251. form.Show();
  252. }
  253. else
  254. {
  255. MdiChildren.First(c => c.Name == "OperatorForm").Activate();
  256. }
  257. }
  258. private void btn_serverset_ItemClick(object sender, DevExpress.XtraBars.ItemClickEventArgs e)
  259. {
  260. using (ServerSet form = new ServerSet())
  261. {
  262. form.currentUser = Operator;
  263. if (form.ShowDialog() == DialogResult.OK)
  264. {
  265. Application.Restart();
  266. }
  267. }
  268. }
  269. private void nbi_team_list_LinkClicked(object sender, DevExpress.XtraNavBar.NavBarLinkEventArgs e)
  270. {
  271. if (!MdiChildren.Any(c => c.Name == "TeamForm"))
  272. {
  273. TeamForm form = new TeamForm
  274. {
  275. currentUser = Operator,
  276. MdiParent = this
  277. };
  278. form.Show();
  279. }
  280. else
  281. {
  282. MdiChildren.First(c => c.Name == "TeamForm").Activate();
  283. }
  284. }
  285. private void btn_changePwd_ItemClick(object sender, DevExpress.XtraBars.ItemClickEventArgs e)
  286. {
  287. using (ChangePwd form = new ChangePwd())
  288. {
  289. form.currentOperator = Operator;
  290. if (form.ShowDialog() == DialogResult.OK)
  291. {
  292. }
  293. }
  294. }
  295. private void nbi_actions_LinkClicked(object sender, DevExpress.XtraNavBar.NavBarLinkEventArgs e)
  296. {
  297. if (!MdiChildren.Any(c => c.Name == "ActionsLog"))
  298. {
  299. ActionsLog form = new ActionsLog
  300. {
  301. MdiParent = this
  302. };
  303. form.Show();
  304. }
  305. else
  306. {
  307. MdiChildren.First(c => c.Name == "ActionsLog").Activate();
  308. }
  309. }
  310. private void nbi_chart_LinkClicked(object sender, DevExpress.XtraNavBar.NavBarLinkEventArgs e)
  311. {
  312. if (!MdiChildren.Any(c => c.Name == "ChartForm"))
  313. {
  314. ChartForm form = new ChartForm
  315. {
  316. MdiParent = this
  317. };
  318. form.Show();
  319. }
  320. else
  321. {
  322. MdiChildren.First(c => c.Name == "ChartForm").Activate();
  323. }
  324. }
  325. private void nbi_report_LinkClicked(object sender, DevExpress.XtraNavBar.NavBarLinkEventArgs e)
  326. {
  327. if (!MdiChildren.Any(c => c.Name == "ReportForm"))
  328. {
  329. ReportForm form = new ReportForm
  330. {
  331. MdiParent = this
  332. };
  333. form.Show();
  334. }
  335. else
  336. {
  337. MdiChildren.First(c => c.Name == "ReportForm").Activate();
  338. }
  339. }
  340. private void nbi_entrance_LinkClicked(object sender, DevExpress.XtraNavBar.NavBarLinkEventArgs e)
  341. {
  342. if (!MdiChildren.Any(c => c.Name == "EntranceForm"))
  343. {
  344. EntranceForm form = new EntranceForm
  345. {
  346. MdiParent = this,
  347. currentUser = Operator
  348. };
  349. form.Show();
  350. }
  351. else
  352. {
  353. MdiChildren.First(c => c.Name == "EntranceForm").Activate();
  354. }
  355. }
  356. private SplashScreenManager _loadForm;
  357. /// <summary>
  358. /// 等待窗体管理对象
  359. /// </summary>
  360. protected SplashScreenManager LoadForm
  361. {
  362. get
  363. {
  364. if (_loadForm == null)
  365. {
  366. _loadForm = new SplashScreenManager(this, typeof(FrmWaitForm), true, true)
  367. {
  368. ClosingDelay = 0
  369. };
  370. }
  371. return _loadForm;
  372. }
  373. }
  374. /// <summary>
  375. /// 显示等待窗体
  376. /// </summary>
  377. public void ShowMessage()
  378. {
  379. bool flag = !LoadForm.IsSplashFormVisible;
  380. if (flag)
  381. {
  382. LoadForm.ShowWaitForm();
  383. }
  384. }
  385. /// <summary>
  386. /// 关闭等待窗体
  387. /// </summary>
  388. public void HideMessage()
  389. {
  390. bool isSplashFormVisible = LoadForm.IsSplashFormVisible;
  391. if (isSplashFormVisible)
  392. {
  393. LoadForm.CloseWaitForm();
  394. }
  395. }
  396. private void btn_help_ItemClick(object sender, DevExpress.XtraBars.ItemClickEventArgs e)
  397. {
  398. Help.ShowHelp(this, helpProvider1.HelpNamespace);
  399. }
  400. private void MainForm_Resize(object sender, EventArgs e)
  401. {
  402. if (WindowState != FormWindowState.Minimized)
  403. {
  404. if (Width <= 1024)
  405. {
  406. navBarControl1.Width = 177;
  407. }
  408. else
  409. {
  410. navBarControl1.Width = 241;
  411. }
  412. }
  413. }
  414. private void Exit_ToolStripMenuItem_Click(object sender, EventArgs e)
  415. {
  416. if (MessageBox.Show("退出后将无法计量称重数据,是否确认退出程序?", "退出", MessageBoxButtons.OKCancel, MessageBoxIcon.Question) == DialogResult.OK)
  417. {
  418. DialogResult = DialogResult.Cancel;
  419. // 关闭所有的线程
  420. Dispose();
  421. Close();
  422. }
  423. }
  424. private void Main_ToolStripMenuItem_Click(object sender, EventArgs e)
  425. {
  426. DialogResult = DialogResult.Ignore;
  427. ShowInTaskbar = true;
  428. //还原窗体显示
  429. WindowState = FormWindowState.Maximized;
  430. //激活窗体并给予它焦点
  431. Activate();
  432. }
  433. protected override void OnClosing(CancelEventArgs e)
  434. {
  435. if (DialogResult == DialogResult.Cancel)
  436. {
  437. e.Cancel = true;
  438. WindowState = FormWindowState.Minimized;
  439. ShowInTaskbar = false;
  440. notifyIcon1.Visible = true;
  441. }
  442. else
  443. {
  444. e.Cancel = true;
  445. }
  446. }
  447. private void notifyIcon1_MouseClick(object sender, MouseEventArgs e)
  448. {
  449. if (e.Button == MouseButtons.Left)
  450. {
  451. DialogResult = DialogResult.Ignore;
  452. ShowInTaskbar = true; notifyIcon1.Visible = false;
  453. //还原窗体显示
  454. WindowState = FormWindowState.Maximized;
  455. //激活窗体并给予它焦点
  456. Activate();
  457. }
  458. }
  459. }
  460. }