| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226 |
- using DaJiaoYan.Services;
- using DaJiaoYan.Utils;
- using DaJiaoYan.Variables;
- using System;
- using System.Drawing;
- using System.IO;
- using System.Threading.Tasks;
- using System.Windows.Forms;
- namespace DaJiaoYan
- {
- public partial class FormMain : Form
- {
- private readonly NotifyIcon notifyIcon;
- private static readonly Icon icon = new Icon(Path.Combine(Const.APP_PATH, Const.ICON_FILE));
- public FormMain()
- {
- InitializeComponent();
- notifyIcon = new NotifyIcon
- {
- Icon = icon,
- Text = Const.APP_SHOW_NAME,
- Visible = true,
- };
- notifyIcon.Click += NotifyIcon_Click;
- ControlExts.MessageBoxShowForm = this;
- Scan.OwnerFormHandle = this.Handle;
- Task.Run(() =>
- {
- Init();
- });
- //Scan.OwnerFormHandle = this.Handle;
- }
- private void Init()
- {
- ControlExts.FormInvoke(this, () =>
- {
- #if DEBUG
- Text = $"{Const.APP_FORM_NAME} (debug {Const.APP_DEBUG_BUILD_VERSION} {Const.HTTP_HOST})";
- #else
- Text = $"{Const.APP_FORM_NAME} (release {Const.APP_DEBUG_BUILD_VERSION})";
- #endif
- this.Icon = icon;
- });
- #if !DEBUG
- CheckForUpdate();
- #endif
- Scan.DelegateLoadDriver = ChangeScannerDriverLabel;
- Scan.DelegateChangeScannerName = ChangeScannerNameLabel;
- Scan.DelegateChangeScannerStatus = ChangeScannerStatusLabel;
- Variables.Vars.DelegateCounterChange = ChangeUploadedCount;
- Task.Factory.StartNew(() =>
- {
- Services.Instance.Run();
- Models.ScannerList scannerList = Scan.GetScannerList();
- Variables.Vars.Scanners = scannerList.Resources;
- Variables.Vars.ScannerSelected = scannerList.Selected;
- //Instance.Init();
- ShowFreeSpace();
- });
- }
- private void NotifyIcon_Click(object sender, EventArgs e)
- {
- ShowMainForm();
- }
- private void ShowMainForm()
- {
- if (WindowState == FormWindowState.Minimized || !ShowInTaskbar)
- {
- Show();
- Activate();
- WindowState = FormWindowState.Normal;
- TopMost = true;
- Task.Run(() =>
- {
- Task.Delay(100);
- ControlExts.FormInvoke(this, () =>
- {
- TopMost = false;
- });
- });
- }
- else
- {
- WindowState = FormWindowState.Minimized;
- //ShowInTaskbar = false;
- Hide();
- }
- }
- private void ChangeLabel(Label label, string txt, Color color)
- {
- ControlExts.FormInvoke(this, () =>
- {
- label.Text = txt;
- label.ForeColor = color;
- });
- }
- private void ChangeLabel(Label label, string txt)
- {
- ChangeLabel(label, txt, Color.Black);
- }
- private void ChangeScannerDriverLabel(bool ok)
- {
- string txt = ok ? "载入成功" : "载入失败";
- Color color = ok ? Color.Green : Color.Red;
- ChangeLabel(labelScannerDriver, txt, color);
- }
- private void ChangeScannerNameLabel(string txt)
- {
- ChangeLabel(labelScannerName, txt);
- }
- private async void ChangeScannerStatusLabel(string txt, Color color)
- {
- ChangeLabel(labelScannerStatus, txt, color);
- await ShowFreeSpace();
- }
- private void ChangeScannerDirectory(string txt)
- {
- ChangeLabel(labelScannerDirectory, txt);
- }
- private Task ShowFreeSpace()
- {
- return Task.Run(() =>
- {
- string FreeSpace = FileExts.GetHardDiskFreeSpace(Const.TMP_PATH.Substring(0, 1), FileExts.FileSizeUnit.GB);
- Utils.FileExts.CheckDirectory(Const.TMP_PATH, true);
- ChangeScannerDirectory($"{Const.TMP_PATH}\n[ 可用空间:{FreeSpace} ]");
- });
- }
- private void CheckForUpdate()
- {
- Task.Run(async () =>
- {
- var info = await Api.GetUpdateInfo();
- if (info.Version > Const.APP_BUILD_VERSION)
- {
- try
- {
- ProcessExts.RunUpdate();
- }
- catch
- {
- ShowMessageBox("启动升级程序失败!", Const.MSG_CAPTION_ERROR);
- }
- Close();
- }
- });
- }
- /// <summary>
- /// 显示消息提示框
- /// </summary>
- /// <param name="text"></param>
- /// <param name="caption"></param>
- /// <param name="buttons"></param>
- /// <param name="icon"></param>
- /// <param name="defaultButton"></param>
- /// <returns></returns>
- private DialogResult ShowMessageBox(string text, string caption = "提示", MessageBoxButtons buttons = MessageBoxButtons.OK, MessageBoxIcon icon = MessageBoxIcon.Information, MessageBoxDefaultButton defaultButton = MessageBoxDefaultButton.Button1)
- {
- return ControlExts.ShowMessageBox(text, caption, buttons, icon, defaultButton);
- }
- private void LabelScannerDirectory_DoubleClick(object sender, EventArgs e)
- {
- FileExts.OpenFileInExplore(Const.TMP_PATH, () =>
- {
- ShowMessageBox("打开文件夹失败!", Const.MSG_CAPTION_ERROR);
- });
- }
- private void ChangeUploadedCount(int count)
- {
- ChangeLabel(labelUploadedCount, count.ToString());
- }
- private void FormMain_FormClosing(object sender, FormClosingEventArgs e)
- {
- void exitFunc()
- {
- notifyIcon.Visible = false;
- notifyIcon.Icon.Dispose();
- notifyIcon.Dispose();
- }
- if (Vars.AllTaskFinished())
- {
- exitFunc();
- }
- else
- {
- if (ShowMessageBox("正在上传图片,如果强制退出将导致上传的批次图片不完整!\n确定退出吗?", Const.MSG_CAPTION_WARN, MessageBoxButtons.OKCancel) == DialogResult.OK)
- {
- exitFunc();
- }
- else
- {
- e.Cancel = true;
- }
- }
- }
- private void FormMain_SizeChanged(object sender, EventArgs e)
- {
- ShowInTaskbar = Visible = WindowState != FormWindowState.Minimized;
- }
- }
- }
|