123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293 |
- using System.Drawing;
- using System.Drawing.Drawing2D;
- using System.Drawing.Text;
- using System.Windows.Forms;
- using Microsoft.Windows.Forms;
- using Update.Config;
- using Update.Controls;
- using Update.Core;
- using Update.Properties;
- using Update.Util;
- namespace Update
- {
- /// <summary>
- /// 主窗口
- /// </summary>
- public partial class FrmUpdate : BaseForm
- {
- private const int INTERVAL_DELAY = 1000; //延迟关闭间隔
- private const int INTERVAL_REFRESH = 100; //进度刷新间隔
- private int m_Percentage; //进度百分比
- private Timer m_DelayTimer; //延迟关闭定时器
- private Timer m_RefreshTimer = new Timer(); //进度刷新定时器
- private Updater m_Updater = new Updater(); //更新器
- //控件
- protected UIImage imgBackground;
- protected UILabel lblHeader;
- protected UILine lnHeader;
- protected UILabel lblFooter;
- protected UILine lnFooter;
- protected UILabel lblLog;
- protected UIProgress prgPercentage;
- protected UIButton btnRetry;
- protected UIButton btnCancel;
- //属性
- /// <summary>
- /// 获取或设置是否检查模式
- /// </summary>
- public bool IsCheckMode { get; set; }
- /// <summary>
- /// 获取是否检查完成
- /// </summary>
- public bool IsCheckCompleted { get; protected set; }
- /// <summary>
- /// 获取是否最新版本
- /// </summary>
- public bool Uptodate { get; protected set; }
- /// <summary>
- /// 构造函数
- /// </summary>
- public FrmUpdate()
- {
- InitializeComponent();
- this.InitUI();
- this.InitLogic();
- this.Start();
- }
- /// <summary>
- /// 初始化界面
- /// </summary>
- protected virtual void InitUI()
- {
- this.SuspendLayout();
- //
- this.imgBackground = new UIImage();
- this.imgBackground.Name = "imgBackground";
- this.imgBackground.Dock = DockStyle.Fill;
- this.imgBackground.AnimationRandom = true;
- this.imgBackground.AddFrame(Resources.background0);
- this.imgBackground.AddFrame(Resources.background1);
- this.imgBackground.AddFrame(Resources.background2);
- this.imgBackground.AddFrame(Resources.background3);
- this.imgBackground.AddFrame(Resources.background4);
- this.imgBackground.AddFrame(Resources.background5);
- this.imgBackground.AddFrame(Resources.background6);
- this.imgBackground.AddFrame(Resources.background7);
- //
- this.lblHeader = new UILabel();
- this.lblHeader.Name = "lblHeader";
- this.lblHeader.Dock = DockStyle.Top;
- this.lblHeader.Height = 75;
- this.lblHeader.Padding = new Padding(60, 0, 0, 10);
- this.lblHeader.Font = new Font("微软雅黑", 23F);
- this.lblHeader.ForeColor = Color.FromArgb(200, 255, 255, 255);
- this.lblHeader.Text = this.Text;
- this.lblHeader.TextAlign = ContentAlignment.BottomLeft;
- this.lblHeader.TextRenderingHint = TextRenderingHint.AntiAlias;
- //
- this.lnHeader = new UILine();
- this.lnHeader.Name = "lnHeader";
- this.lnHeader.Dock = DockStyle.Top;
- this.lnHeader.Height = 2;
- this.lnHeader.LineWidth = 2;
- this.lnHeader.LineColor = Color.FromArgb(40, 255, 255, 255);
- this.lnHeader.LineDashStyle = DashStyle.Dash;
- this.lnHeader.Padding = new Padding(1, 0, 1, 0);
- //
- this.lblFooter = new UILabel();
- this.lblFooter.Name = "lblFooter";
- this.lblFooter.Dock = DockStyle.Bottom;
- this.lblFooter.Height = 75;
- //
- this.lnFooter = new UILine();
- this.lnFooter.Name = "lnFooter";
- this.lnFooter.Dock = DockStyle.Bottom;
- this.lnFooter.Height = 2;
- this.lnFooter.LineWidth = 2;
- this.lnFooter.LineColor = Color.FromArgb(40, 255, 255, 255);
- this.lnFooter.LineDashStyle = DashStyle.Dash;
- this.lnFooter.Padding = new Padding(1, 0, 1, 0);
- //
- this.lblLog = new UILabel();
- this.lblLog.Name = "lblLog";
- this.lblLog.Anchor = AnchorStyles.Left | AnchorStyles.Right;
- this.lblLog.Location = new Point(85, 232);
- this.lblLog.Size = new Size(830, 40);
- this.lblLog.Padding = new Padding(4);
- this.lblLog.Font = new Font("微软雅黑", 15F);
- this.lblLog.TextAlign = ContentAlignment.MiddleLeft;
- this.lblLog.ForeColor = Color.FromArgb(200, 255, 255, 255);
- this.lblLog.TextRenderingHint = TextRenderingHint.AntiAlias;
- //
- this.prgPercentage = new UIProgress();
- this.prgPercentage.Name = "prgPercentage";
- this.prgPercentage.Anchor = AnchorStyles.Left | AnchorStyles.Right;
- this.prgPercentage.Location = new Point(85, 278);
- this.prgPercentage.Size = new Size(830, 10);
- this.prgPercentage.BackColor = Color.FromArgb(20, 255, 255, 255);
- this.prgPercentage.BorderColor = Color.FromArgb(200, 255, 255, 255);
- this.prgPercentage.ProgressColor = Color.FromArgb(150, 255, 255, 255);
- //
- this.btnRetry = new UIButton();
- this.btnRetry.Name = "btnRetry";
- this.btnRetry.Anchor = AnchorStyles.Bottom | AnchorStyles.Right;
- this.btnRetry.Location = new Point(804, 455);
- this.btnRetry.Size = new Size(70, 30);
- this.btnRetry.BackColor = Color.Transparent;
- this.btnRetry.HoveredBackColor = Color.FromArgb(20, 255, 255, 255);
- this.btnRetry.PressedBackColor = Color.FromArgb(50, 255, 255, 255);
- this.btnRetry.Font = new Font("微软雅黑", 12F);
- this.btnRetry.ForeColor = Color.FromArgb(200, 255, 255, 255);
- this.btnRetry.Text = "重试";
- this.btnRetry.TextRenderingHint = TextRenderingHint.AntiAlias;
- this.btnRetry.Image = Resources.retry;
- this.btnRetry.ImageSize = new Size(16, 16);
- this.btnRetry.Visible = false;
- //
- this.btnCancel = new UIButton();
- this.btnCancel.Name = "btnCancel";
- this.btnCancel.Anchor = AnchorStyles.Bottom | AnchorStyles.Right;
- this.btnCancel.Location = new Point(880, 455);
- this.btnCancel.Size = new Size(70, 30);
- this.btnCancel.BackColor = Color.Transparent;
- this.btnCancel.HoveredBackColor = Color.FromArgb(20, 255, 255, 255);
- this.btnCancel.PressedBackColor = Color.FromArgb(50, 255, 255, 255);
- this.btnCancel.Font = new Font("微软雅黑", 12F);
- this.btnCancel.ForeColor = Color.FromArgb(200, 255, 255, 255);
- this.btnCancel.Text = "取消";
- this.btnCancel.TextRenderingHint = TextRenderingHint.AntiAlias;
- this.btnCancel.Image = Resources.cancel;
- this.btnCancel.ImageSize = new Size(16, 16);
- //
- this.UIControls.Add(this.imgBackground);
- this.imgBackground.UIControls.Add(this.lblHeader);
- this.imgBackground.UIControls.Add(this.lnHeader);
- this.imgBackground.UIControls.Add(this.lblFooter);
- this.imgBackground.UIControls.Add(this.lnFooter);
- this.imgBackground.UIControls.Add(this.lblLog);
- this.imgBackground.UIControls.Add(this.prgPercentage);
- this.imgBackground.UIControls.Add(this.btnRetry);
- this.imgBackground.UIControls.Add(this.btnCancel);
- //
- this.ResumeLayout(false);
- }
- /// <summary>
- /// 初始化逻辑
- /// </summary>
- protected virtual void InitLogic()
- {
- //定时器
- this.m_RefreshTimer.Interval = INTERVAL_REFRESH;
- this.m_RefreshTimer.Tick += (sender, e) => this.prgPercentage.Percentage = this.m_Percentage;
- //更新器
- this.m_Updater.Notify += (sender, e) => this.lblLog.Text = e.Info;
- this.m_Updater.Progress += (sender, e) => this.m_Percentage = e.ProgressPercentage;
- this.m_Updater.CheckCompleted += (sender, e) =>
- {
- this.IsCheckCompleted = true;
- this.Uptodate = e.Uptodate;
- if (this.IsCheckMode)
- {
- this.CheckCompleted();
- e.Handled = true;
- }
- };
- this.m_Updater.UpdateCompleted += (sender, e) =>
- {
- ProcessUtil.Start(HostConfig.ExecutablePath,"Updater Start");
- this.CloseCore();
- };
- this.m_Updater.Error += (sender, e) =>
- {
- this.lblLog.Text = e.Error.Message;
- this.btnRetry.Visible = true;
- this.m_RefreshTimer.Stop();
- };
- //界面
- this.lblHeader.MouseDown += (sender, e) => Microsoft.Win32.Util.BeginDrag(this.Handle);
- this.lblFooter.MouseDown += (sender, e) => Microsoft.Win32.Util.BeginDrag(this.Handle);
- this.btnRetry.Click += (sender, e) => { IdleEvent(); };
- this.btnCancel.Click += (sender, e) =>
- {
- if (MessageBoxUtil.Confirm("您确定要退出更新?") == DialogResult.OK)
- this.CloseCore();
- };
- }
- /// <summary>
- /// 空闲时开始更新
- /// </summary>
- protected virtual void Start()
- {
- AppUtil.Idle(() =>
- {
- IdleEvent();
- });
- }
- /// <summary>
- /// 检查更新完成
- /// </summary>
- protected virtual void CheckCompleted()
- {
- if (this.m_DelayTimer == null)
- {
- this.m_DelayTimer = new Timer();
- this.m_DelayTimer.Interval = INTERVAL_DELAY;
- this.m_DelayTimer.Tick += (ss, ee) => this.CloseCore();
- }
- this.m_DelayTimer.Start();
- }
- private void IdleEvent()
- {
- if (this.m_Updater == null) return;
- this.btnRetry.Visible = false;
- this.IsCheckCompleted = false;
- this.Uptodate = false;
- this.m_RefreshTimer.Start();
- this.m_Updater.StartUpdate();
- }
- /// <summary>
- /// 检查更新,可指定是否允许取消更新
- /// </summary>
- /// <param name="allowCancel">是否允许取消更新</param>
- /// <returns>true 表示程序应继续运行,否则程序应退出</returns>
- public virtual bool CheckUpdate(bool allowCancel = false)
- {
- return m_Updater.IsClientOverdue();
- }
- /// <summary>
- /// 释放资源
- /// </summary>
- /// <param name="disposing">释放托管资源为true,否则为false</param>
- protected override void Dispose(bool disposing)
- {
- if (this.m_DelayTimer != null)
- {
- this.m_DelayTimer.Dispose();
- this.m_DelayTimer = null;
- }
- if (this.m_RefreshTimer != null)
- {
- this.m_RefreshTimer.Dispose();
- this.m_RefreshTimer = null;
- }
- if (this.m_Updater != null)
- {
- this.m_Updater.Dispose();
- this.m_Updater = null;
- }
- if (disposing && (components != null))
- {
- components.Dispose();
- }
- base.Dispose(disposing);
- }
- }
- }
|