FormMain.cs 6.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226
  1. using DaJiaoYan.Services;
  2. using DaJiaoYan.Utils;
  3. using DaJiaoYan.Variables;
  4. using System;
  5. using System.Drawing;
  6. using System.IO;
  7. using System.Threading.Tasks;
  8. using System.Windows.Forms;
  9. namespace DaJiaoYan
  10. {
  11. public partial class FormMain : Form
  12. {
  13. private readonly NotifyIcon notifyIcon;
  14. private static readonly Icon icon = new Icon(Path.Combine(Const.APP_PATH, Const.ICON_FILE));
  15. public FormMain()
  16. {
  17. InitializeComponent();
  18. notifyIcon = new NotifyIcon
  19. {
  20. Icon = icon,
  21. Text = Const.APP_SHOW_NAME,
  22. Visible = true,
  23. };
  24. notifyIcon.Click += NotifyIcon_Click;
  25. ControlExts.MessageBoxShowForm = this;
  26. Scan.OwnerFormHandle = this.Handle;
  27. Task.Run(() =>
  28. {
  29. Init();
  30. });
  31. //Scan.OwnerFormHandle = this.Handle;
  32. }
  33. private void Init()
  34. {
  35. ControlExts.FormInvoke(this, () =>
  36. {
  37. #if DEBUG
  38. Text = $"{Const.APP_FORM_NAME} (debug {Const.APP_DEBUG_BUILD_VERSION} {Const.HTTP_HOST})";
  39. #else
  40. Text = $"{Const.APP_FORM_NAME} (release {Const.APP_DEBUG_BUILD_VERSION})";
  41. #endif
  42. this.Icon = icon;
  43. });
  44. #if !DEBUG
  45. CheckForUpdate();
  46. #endif
  47. Scan.DelegateLoadDriver = ChangeScannerDriverLabel;
  48. Scan.DelegateChangeScannerName = ChangeScannerNameLabel;
  49. Scan.DelegateChangeScannerStatus = ChangeScannerStatusLabel;
  50. Variables.Vars.DelegateCounterChange = ChangeUploadedCount;
  51. Task.Factory.StartNew(() =>
  52. {
  53. Services.Instance.Run();
  54. Models.ScannerList scannerList = Scan.GetScannerList();
  55. Variables.Vars.Scanners = scannerList.Resources;
  56. Variables.Vars.ScannerSelected = scannerList.Selected;
  57. //Instance.Init();
  58. ShowFreeSpace();
  59. });
  60. }
  61. private void NotifyIcon_Click(object sender, EventArgs e)
  62. {
  63. ShowMainForm();
  64. }
  65. private void ShowMainForm()
  66. {
  67. if (WindowState == FormWindowState.Minimized || !ShowInTaskbar)
  68. {
  69. Show();
  70. Activate();
  71. WindowState = FormWindowState.Normal;
  72. TopMost = true;
  73. Task.Run(() =>
  74. {
  75. Task.Delay(100);
  76. ControlExts.FormInvoke(this, () =>
  77. {
  78. TopMost = false;
  79. });
  80. });
  81. }
  82. else
  83. {
  84. WindowState = FormWindowState.Minimized;
  85. //ShowInTaskbar = false;
  86. Hide();
  87. }
  88. }
  89. private void ChangeLabel(Label label, string txt, Color color)
  90. {
  91. ControlExts.FormInvoke(this, () =>
  92. {
  93. label.Text = txt;
  94. label.ForeColor = color;
  95. });
  96. }
  97. private void ChangeLabel(Label label, string txt)
  98. {
  99. ChangeLabel(label, txt, Color.Black);
  100. }
  101. private void ChangeScannerDriverLabel(bool ok)
  102. {
  103. string txt = ok ? "载入成功" : "载入失败";
  104. Color color = ok ? Color.Green : Color.Red;
  105. ChangeLabel(labelScannerDriver, txt, color);
  106. }
  107. private void ChangeScannerNameLabel(string txt)
  108. {
  109. ChangeLabel(labelScannerName, txt);
  110. }
  111. private async void ChangeScannerStatusLabel(string txt, Color color)
  112. {
  113. ChangeLabel(labelScannerStatus, txt, color);
  114. await ShowFreeSpace();
  115. }
  116. private void ChangeScannerDirectory(string txt)
  117. {
  118. ChangeLabel(labelScannerDirectory, txt);
  119. }
  120. private Task ShowFreeSpace()
  121. {
  122. return Task.Run(() =>
  123. {
  124. string FreeSpace = FileExts.GetHardDiskFreeSpace(Const.TMP_PATH.Substring(0, 1), FileExts.FileSizeUnit.GB);
  125. Utils.FileExts.CheckDirectory(Const.TMP_PATH, true);
  126. ChangeScannerDirectory($"{Const.TMP_PATH}\n[ 可用空间:{FreeSpace} ]");
  127. });
  128. }
  129. private void CheckForUpdate()
  130. {
  131. Task.Run(async () =>
  132. {
  133. var info = await Api.GetUpdateInfo();
  134. if (info.Version > Const.APP_BUILD_VERSION)
  135. {
  136. try
  137. {
  138. ProcessExts.RunUpdate();
  139. }
  140. catch
  141. {
  142. ShowMessageBox("启动升级程序失败!", Const.MSG_CAPTION_ERROR);
  143. }
  144. Close();
  145. }
  146. });
  147. }
  148. /// <summary>
  149. /// 显示消息提示框
  150. /// </summary>
  151. /// <param name="text"></param>
  152. /// <param name="caption"></param>
  153. /// <param name="buttons"></param>
  154. /// <param name="icon"></param>
  155. /// <param name="defaultButton"></param>
  156. /// <returns></returns>
  157. private DialogResult ShowMessageBox(string text, string caption = "提示", MessageBoxButtons buttons = MessageBoxButtons.OK, MessageBoxIcon icon = MessageBoxIcon.Information, MessageBoxDefaultButton defaultButton = MessageBoxDefaultButton.Button1)
  158. {
  159. return ControlExts.ShowMessageBox(text, caption, buttons, icon, defaultButton);
  160. }
  161. private void LabelScannerDirectory_DoubleClick(object sender, EventArgs e)
  162. {
  163. FileExts.OpenFileInExplore(Const.TMP_PATH, () =>
  164. {
  165. ShowMessageBox("打开文件夹失败!", Const.MSG_CAPTION_ERROR);
  166. });
  167. }
  168. private void ChangeUploadedCount(int count)
  169. {
  170. ChangeLabel(labelUploadedCount, count.ToString());
  171. }
  172. private void FormMain_FormClosing(object sender, FormClosingEventArgs e)
  173. {
  174. void exitFunc()
  175. {
  176. notifyIcon.Visible = false;
  177. notifyIcon.Icon.Dispose();
  178. notifyIcon.Dispose();
  179. }
  180. if (Vars.AllTaskFinished())
  181. {
  182. exitFunc();
  183. }
  184. else
  185. {
  186. if (ShowMessageBox("正在上传图片,如果强制退出将导致上传的批次图片不完整!\n确定退出吗?", Const.MSG_CAPTION_WARN, MessageBoxButtons.OKCancel) == DialogResult.OK)
  187. {
  188. exitFunc();
  189. }
  190. else
  191. {
  192. e.Cancel = true;
  193. }
  194. }
  195. }
  196. private void FormMain_SizeChanged(object sender, EventArgs e)
  197. {
  198. ShowInTaskbar = Visible = WindowState != FormWindowState.Minimized;
  199. }
  200. }
  201. }