FrmUpdate.cs 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293
  1. using System.Drawing;
  2. using System.Drawing.Drawing2D;
  3. using System.Drawing.Text;
  4. using System.Windows.Forms;
  5. using Microsoft.Windows.Forms;
  6. using Update.Config;
  7. using Update.Controls;
  8. using Update.Core;
  9. using Update.Properties;
  10. using Update.Util;
  11. namespace Update
  12. {
  13. /// <summary>
  14. /// 主窗口
  15. /// </summary>
  16. public partial class FrmUpdate : BaseForm
  17. {
  18. private const int INTERVAL_DELAY = 1000; //延迟关闭间隔
  19. private const int INTERVAL_REFRESH = 100; //进度刷新间隔
  20. private int m_Percentage; //进度百分比
  21. private Timer m_DelayTimer; //延迟关闭定时器
  22. private Timer m_RefreshTimer = new Timer(); //进度刷新定时器
  23. private Updater m_Updater = new Updater(); //更新器
  24. //控件
  25. protected UIImage imgBackground;
  26. protected UILabel lblHeader;
  27. protected UILine lnHeader;
  28. protected UILabel lblFooter;
  29. protected UILine lnFooter;
  30. protected UILabel lblLog;
  31. protected UIProgress prgPercentage;
  32. protected UIButton btnRetry;
  33. protected UIButton btnCancel;
  34. //属性
  35. /// <summary>
  36. /// 获取或设置是否检查模式
  37. /// </summary>
  38. public bool IsCheckMode { get; set; }
  39. /// <summary>
  40. /// 获取是否检查完成
  41. /// </summary>
  42. public bool IsCheckCompleted { get; protected set; }
  43. /// <summary>
  44. /// 获取是否最新版本
  45. /// </summary>
  46. public bool Uptodate { get; protected set; }
  47. /// <summary>
  48. /// 构造函数
  49. /// </summary>
  50. public FrmUpdate()
  51. {
  52. InitializeComponent();
  53. this.InitUI();
  54. this.InitLogic();
  55. this.Start();
  56. }
  57. /// <summary>
  58. /// 初始化界面
  59. /// </summary>
  60. protected virtual void InitUI()
  61. {
  62. this.SuspendLayout();
  63. //
  64. this.imgBackground = new UIImage();
  65. this.imgBackground.Name = "imgBackground";
  66. this.imgBackground.Dock = DockStyle.Fill;
  67. this.imgBackground.AnimationRandom = true;
  68. this.imgBackground.AddFrame(Resources.background0);
  69. this.imgBackground.AddFrame(Resources.background1);
  70. this.imgBackground.AddFrame(Resources.background2);
  71. this.imgBackground.AddFrame(Resources.background3);
  72. this.imgBackground.AddFrame(Resources.background4);
  73. this.imgBackground.AddFrame(Resources.background5);
  74. this.imgBackground.AddFrame(Resources.background6);
  75. this.imgBackground.AddFrame(Resources.background7);
  76. //
  77. this.lblHeader = new UILabel();
  78. this.lblHeader.Name = "lblHeader";
  79. this.lblHeader.Dock = DockStyle.Top;
  80. this.lblHeader.Height = 75;
  81. this.lblHeader.Padding = new Padding(60, 0, 0, 10);
  82. this.lblHeader.Font = new Font("微软雅黑", 23F);
  83. this.lblHeader.ForeColor = Color.FromArgb(200, 255, 255, 255);
  84. this.lblHeader.Text = this.Text;
  85. this.lblHeader.TextAlign = ContentAlignment.BottomLeft;
  86. this.lblHeader.TextRenderingHint = TextRenderingHint.AntiAlias;
  87. //
  88. this.lnHeader = new UILine();
  89. this.lnHeader.Name = "lnHeader";
  90. this.lnHeader.Dock = DockStyle.Top;
  91. this.lnHeader.Height = 2;
  92. this.lnHeader.LineWidth = 2;
  93. this.lnHeader.LineColor = Color.FromArgb(40, 255, 255, 255);
  94. this.lnHeader.LineDashStyle = DashStyle.Dash;
  95. this.lnHeader.Padding = new Padding(1, 0, 1, 0);
  96. //
  97. this.lblFooter = new UILabel();
  98. this.lblFooter.Name = "lblFooter";
  99. this.lblFooter.Dock = DockStyle.Bottom;
  100. this.lblFooter.Height = 75;
  101. //
  102. this.lnFooter = new UILine();
  103. this.lnFooter.Name = "lnFooter";
  104. this.lnFooter.Dock = DockStyle.Bottom;
  105. this.lnFooter.Height = 2;
  106. this.lnFooter.LineWidth = 2;
  107. this.lnFooter.LineColor = Color.FromArgb(40, 255, 255, 255);
  108. this.lnFooter.LineDashStyle = DashStyle.Dash;
  109. this.lnFooter.Padding = new Padding(1, 0, 1, 0);
  110. //
  111. this.lblLog = new UILabel();
  112. this.lblLog.Name = "lblLog";
  113. this.lblLog.Anchor = AnchorStyles.Left | AnchorStyles.Right;
  114. this.lblLog.Location = new Point(85, 232);
  115. this.lblLog.Size = new Size(830, 40);
  116. this.lblLog.Padding = new Padding(4);
  117. this.lblLog.Font = new Font("微软雅黑", 15F);
  118. this.lblLog.TextAlign = ContentAlignment.MiddleLeft;
  119. this.lblLog.ForeColor = Color.FromArgb(200, 255, 255, 255);
  120. this.lblLog.TextRenderingHint = TextRenderingHint.AntiAlias;
  121. //
  122. this.prgPercentage = new UIProgress();
  123. this.prgPercentage.Name = "prgPercentage";
  124. this.prgPercentage.Anchor = AnchorStyles.Left | AnchorStyles.Right;
  125. this.prgPercentage.Location = new Point(85, 278);
  126. this.prgPercentage.Size = new Size(830, 10);
  127. this.prgPercentage.BackColor = Color.FromArgb(20, 255, 255, 255);
  128. this.prgPercentage.BorderColor = Color.FromArgb(200, 255, 255, 255);
  129. this.prgPercentage.ProgressColor = Color.FromArgb(150, 255, 255, 255);
  130. //
  131. this.btnRetry = new UIButton();
  132. this.btnRetry.Name = "btnRetry";
  133. this.btnRetry.Anchor = AnchorStyles.Bottom | AnchorStyles.Right;
  134. this.btnRetry.Location = new Point(804, 455);
  135. this.btnRetry.Size = new Size(70, 30);
  136. this.btnRetry.BackColor = Color.Transparent;
  137. this.btnRetry.HoveredBackColor = Color.FromArgb(20, 255, 255, 255);
  138. this.btnRetry.PressedBackColor = Color.FromArgb(50, 255, 255, 255);
  139. this.btnRetry.Font = new Font("微软雅黑", 12F);
  140. this.btnRetry.ForeColor = Color.FromArgb(200, 255, 255, 255);
  141. this.btnRetry.Text = "重试";
  142. this.btnRetry.TextRenderingHint = TextRenderingHint.AntiAlias;
  143. this.btnRetry.Image = Resources.retry;
  144. this.btnRetry.ImageSize = new Size(16, 16);
  145. this.btnRetry.Visible = false;
  146. //
  147. this.btnCancel = new UIButton();
  148. this.btnCancel.Name = "btnCancel";
  149. this.btnCancel.Anchor = AnchorStyles.Bottom | AnchorStyles.Right;
  150. this.btnCancel.Location = new Point(880, 455);
  151. this.btnCancel.Size = new Size(70, 30);
  152. this.btnCancel.BackColor = Color.Transparent;
  153. this.btnCancel.HoveredBackColor = Color.FromArgb(20, 255, 255, 255);
  154. this.btnCancel.PressedBackColor = Color.FromArgb(50, 255, 255, 255);
  155. this.btnCancel.Font = new Font("微软雅黑", 12F);
  156. this.btnCancel.ForeColor = Color.FromArgb(200, 255, 255, 255);
  157. this.btnCancel.Text = "取消";
  158. this.btnCancel.TextRenderingHint = TextRenderingHint.AntiAlias;
  159. this.btnCancel.Image = Resources.cancel;
  160. this.btnCancel.ImageSize = new Size(16, 16);
  161. //
  162. this.UIControls.Add(this.imgBackground);
  163. this.imgBackground.UIControls.Add(this.lblHeader);
  164. this.imgBackground.UIControls.Add(this.lnHeader);
  165. this.imgBackground.UIControls.Add(this.lblFooter);
  166. this.imgBackground.UIControls.Add(this.lnFooter);
  167. this.imgBackground.UIControls.Add(this.lblLog);
  168. this.imgBackground.UIControls.Add(this.prgPercentage);
  169. this.imgBackground.UIControls.Add(this.btnRetry);
  170. this.imgBackground.UIControls.Add(this.btnCancel);
  171. //
  172. this.ResumeLayout(false);
  173. }
  174. /// <summary>
  175. /// 初始化逻辑
  176. /// </summary>
  177. protected virtual void InitLogic()
  178. {
  179. //定时器
  180. this.m_RefreshTimer.Interval = INTERVAL_REFRESH;
  181. this.m_RefreshTimer.Tick += (sender, e) => this.prgPercentage.Percentage = this.m_Percentage;
  182. //更新器
  183. this.m_Updater.Notify += (sender, e) => this.lblLog.Text = e.Info;
  184. this.m_Updater.Progress += (sender, e) => this.m_Percentage = e.ProgressPercentage;
  185. this.m_Updater.CheckCompleted += (sender, e) =>
  186. {
  187. this.IsCheckCompleted = true;
  188. this.Uptodate = e.Uptodate;
  189. if (this.IsCheckMode)
  190. {
  191. this.CheckCompleted();
  192. e.Handled = true;
  193. }
  194. };
  195. this.m_Updater.UpdateCompleted += (sender, e) =>
  196. {
  197. ProcessUtil.Start(HostConfig.ExecutablePath,"Updater Start");
  198. this.CloseCore();
  199. };
  200. this.m_Updater.Error += (sender, e) =>
  201. {
  202. this.lblLog.Text = e.Error.Message;
  203. this.btnRetry.Visible = true;
  204. this.m_RefreshTimer.Stop();
  205. };
  206. //界面
  207. this.lblHeader.MouseDown += (sender, e) => Microsoft.Win32.Util.BeginDrag(this.Handle);
  208. this.lblFooter.MouseDown += (sender, e) => Microsoft.Win32.Util.BeginDrag(this.Handle);
  209. this.btnRetry.Click += (sender, e) => { IdleEvent(); };
  210. this.btnCancel.Click += (sender, e) =>
  211. {
  212. if (MessageBoxUtil.Confirm("您确定要退出更新?") == DialogResult.OK)
  213. this.CloseCore();
  214. };
  215. }
  216. /// <summary>
  217. /// 空闲时开始更新
  218. /// </summary>
  219. protected virtual void Start()
  220. {
  221. AppUtil.Idle(() =>
  222. {
  223. IdleEvent();
  224. });
  225. }
  226. /// <summary>
  227. /// 检查更新完成
  228. /// </summary>
  229. protected virtual void CheckCompleted()
  230. {
  231. if (this.m_DelayTimer == null)
  232. {
  233. this.m_DelayTimer = new Timer();
  234. this.m_DelayTimer.Interval = INTERVAL_DELAY;
  235. this.m_DelayTimer.Tick += (ss, ee) => this.CloseCore();
  236. }
  237. this.m_DelayTimer.Start();
  238. }
  239. private void IdleEvent()
  240. {
  241. if (this.m_Updater == null) return;
  242. this.btnRetry.Visible = false;
  243. this.IsCheckCompleted = false;
  244. this.Uptodate = false;
  245. this.m_RefreshTimer.Start();
  246. this.m_Updater.StartUpdate();
  247. }
  248. /// <summary>
  249. /// 检查更新,可指定是否允许取消更新
  250. /// </summary>
  251. /// <param name="allowCancel">是否允许取消更新</param>
  252. /// <returns>true 表示程序应继续运行,否则程序应退出</returns>
  253. public virtual bool CheckUpdate(bool allowCancel = false)
  254. {
  255. return m_Updater.IsClientOverdue();
  256. }
  257. /// <summary>
  258. /// 释放资源
  259. /// </summary>
  260. /// <param name="disposing">释放托管资源为true,否则为false</param>
  261. protected override void Dispose(bool disposing)
  262. {
  263. if (this.m_DelayTimer != null)
  264. {
  265. this.m_DelayTimer.Dispose();
  266. this.m_DelayTimer = null;
  267. }
  268. if (this.m_RefreshTimer != null)
  269. {
  270. this.m_RefreshTimer.Dispose();
  271. this.m_RefreshTimer = null;
  272. }
  273. if (this.m_Updater != null)
  274. {
  275. this.m_Updater.Dispose();
  276. this.m_Updater = null;
  277. }
  278. if (disposing && (components != null))
  279. {
  280. components.Dispose();
  281. }
  282. base.Dispose(disposing);
  283. }
  284. }
  285. }