TransferProgressDialog.cs 9.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282
  1. using System;
  2. using System.Threading;
  3. using System.Threading.Tasks;
  4. using System.Windows.Forms;
  5. namespace PaintDotNet
  6. {
  7. public class TransferProgressDialog : PdnBaseForm
  8. {
  9. private ProgressBar progressBar;
  10. private Button btnCancel;
  11. private Label lblTitle;
  12. private Label lblProgress;
  13. /// <summary>
  14. /// Required designer variable.
  15. /// </summary>
  16. private System.ComponentModel.IContainer components = null;
  17. /// <summary>
  18. /// 执行异步方法的ProgressDialog,本Dialog需要调用者打开关闭
  19. /// </summary>
  20. /// <param name="title">标题</param>
  21. /// <param name="msg">处理信息</param>
  22. /// <param name="proc">同步方法</param>
  23. /// <param name="onCancel">取消事件</param>
  24. /// <param name="canceltext">取消按钮文本</param>
  25. /// <returns>Dialog实例</returns>
  26. public static TransferProgressDialog CreatDialog(string title, string msg, EventHandler onCancel, string canceltext = null)
  27. {
  28. var dialog = new TransferProgressDialog();
  29. dialog.Text = title;
  30. dialog.ProcessMsg = msg;
  31. dialog.CancelClicked += onCancel;
  32. dialog.ProgressBar.Style = ProgressBarStyle.Marquee;
  33. if (string.IsNullOrEmpty(canceltext))
  34. dialog.btnCancel.Text = canceltext;
  35. return dialog;
  36. }
  37. /// <summary>
  38. /// 执行同步方法的ProgressDialog,本Dialog自动执行打开关闭
  39. /// </summary>
  40. /// <param name="title">标题</param>
  41. /// <param name="msg">处理信息</param>
  42. /// <param name="proc">同步方法</param>
  43. /// <param name="onCancel">取消事件</param>
  44. /// <param name="canceltext">取消按钮文本</param>
  45. public static void ShowDialog(string title, string msg, Action proc, EventHandler onCancel = null, string canceltext = null)
  46. {
  47. var dialog = new TransferProgressDialog();
  48. dialog.Text = title;
  49. dialog.ProcessMsg = String.IsNullOrWhiteSpace(msg) ? "Please waiting..." : msg;
  50. dialog.ProgressBar.Style = ProgressBarStyle.Marquee;
  51. if (onCancel != null)
  52. { dialog.CancelClicked += onCancel; }
  53. if (string.IsNullOrEmpty(canceltext))
  54. dialog.btnCancel.Text = canceltext;
  55. new Task(() =>
  56. {
  57. proc();
  58. try//防止取消后关闭引发异常
  59. {
  60. dialog.Invoke(new Action(dialog.Close));
  61. }
  62. catch { }
  63. }).Start();
  64. dialog.ShowDialog();
  65. }
  66. public System.Drawing.Color lblTitleBackColor
  67. {
  68. set
  69. {
  70. this.lblTitle.BackColor = value;
  71. }
  72. }
  73. public string Title
  74. {
  75. get
  76. {
  77. return this.Text;
  78. }
  79. set
  80. {
  81. Text = value;
  82. }
  83. }
  84. public string ProcessMsg
  85. {
  86. get
  87. {
  88. return this.lblProgress.Text;
  89. }
  90. set
  91. {
  92. this.lblProgress.Text = value;
  93. }
  94. }
  95. public event EventHandler CancelClicked;
  96. protected virtual void OnCancelClicked()
  97. {
  98. if (CancelClicked != null)
  99. {
  100. CancelClicked(this, EventArgs.Empty);
  101. }
  102. }
  103. public ProgressBar ProgressBar
  104. {
  105. get
  106. {
  107. return this.progressBar;
  108. }
  109. }
  110. public bool CancelEnabled
  111. {
  112. get
  113. {
  114. return this.btnCancel.Enabled;
  115. }
  116. set
  117. {
  118. this.btnCancel.Enabled = value;
  119. }
  120. }
  121. public bool CancelVisible
  122. {
  123. get
  124. {
  125. return this.btnCancel.Visible;
  126. }
  127. set
  128. {
  129. this.btnCancel.Visible = value;
  130. }
  131. }
  132. public TransferProgressDialog()
  133. {
  134. PdnBaseForm.RegisterFormHotKey(
  135. Keys.Escape,
  136. delegate (Keys keys)
  137. {
  138. OnCancelClicked();
  139. return true;
  140. });
  141. InitializeComponent();
  142. }
  143. protected override void OnLoad(EventArgs e)
  144. {
  145. SystemLayer.UI.DisableCloseBox(this);
  146. base.OnLoad(e);
  147. }
  148. public override void LoadResources()
  149. {
  150. this.btnCancel.Text = PdnResources.GetString("Form.CancelButton.Text");
  151. base.LoadResources();
  152. }
  153. /// <summary>
  154. /// Clean up any resources being used.
  155. /// </summary>
  156. /// <param name="disposing">true if managed resources should be disposed; otherwise, false.</param>
  157. protected override void Dispose(bool disposing)
  158. {
  159. if (disposing && (components != null))
  160. {
  161. components.Dispose();
  162. }
  163. base.Dispose(disposing);
  164. }
  165. #region Windows Form Designer generated code
  166. /// <summary>
  167. /// Required method for Designer support - do not modify
  168. /// the contents of this method with the code editor.
  169. /// </summary>
  170. private void InitializeComponent()
  171. {
  172. this.progressBar = new System.Windows.Forms.ProgressBar();
  173. this.btnCancel = new System.Windows.Forms.Button();
  174. this.lblTitle = new System.Windows.Forms.Label();
  175. this.lblProgress = new System.Windows.Forms.Label();
  176. this.SuspendLayout();
  177. //
  178. // progressBar
  179. //
  180. this.progressBar.Anchor = ((System.Windows.Forms.AnchorStyles)((System.Windows.Forms.AnchorStyles.Bottom | System.Windows.Forms.AnchorStyles.Left)));
  181. this.progressBar.Location = new System.Drawing.Point(10, 34);
  182. this.progressBar.Name = "progressBar";
  183. this.progressBar.Size = new System.Drawing.Size(355, 22);
  184. this.progressBar.TabIndex = 0;
  185. //
  186. // btnCancel
  187. //
  188. this.btnCancel.Anchor = ((System.Windows.Forms.AnchorStyles)((System.Windows.Forms.AnchorStyles.Bottom | System.Windows.Forms.AnchorStyles.Left)));
  189. this.btnCancel.DialogResult = System.Windows.Forms.DialogResult.Cancel;
  190. this.btnCancel.Location = new System.Drawing.Point(281, 63);
  191. this.btnCancel.Name = "btnCancel";
  192. this.btnCancel.Size = new System.Drawing.Size(84, 26);
  193. this.btnCancel.TabIndex = 1;
  194. this.btnCancel.Text = "cancelButton";
  195. this.btnCancel.UseVisualStyleBackColor = true;
  196. this.btnCancel.Click += new System.EventHandler(this.CancelButton_Click);
  197. //
  198. // lblTitle
  199. //
  200. this.lblTitle.AutoEllipsis = true;
  201. this.lblTitle.BackColor = System.Drawing.Color.White;
  202. this.lblTitle.Dock = System.Windows.Forms.DockStyle.Top;
  203. this.lblTitle.Font = new System.Drawing.Font("宋体", 9F, System.Drawing.FontStyle.Regular, System.Drawing.GraphicsUnit.Point, ((byte)(134)));
  204. this.lblTitle.ForeColor = System.Drawing.Color.Black;
  205. this.lblTitle.Location = new System.Drawing.Point(0, 0);
  206. this.lblTitle.Name = "lblTitle";
  207. this.lblTitle.Padding = new System.Windows.Forms.Padding(3);
  208. this.lblTitle.Size = new System.Drawing.Size(378, 20);
  209. this.lblTitle.TabIndex = 2;
  210. this.lblTitle.Text = "itemText";
  211. this.lblTitle.Visible = false;
  212. //
  213. // lblProgress
  214. //
  215. this.lblProgress.Anchor = ((System.Windows.Forms.AnchorStyles)((System.Windows.Forms.AnchorStyles.Bottom | System.Windows.Forms.AnchorStyles.Left)));
  216. this.lblProgress.AutoEllipsis = true;
  217. this.lblProgress.Font = new System.Drawing.Font("宋体", 9F, System.Drawing.FontStyle.Regular, System.Drawing.GraphicsUnit.Point, ((byte)(134)));
  218. this.lblProgress.Location = new System.Drawing.Point(8, 10);
  219. this.lblProgress.Name = "lblProgress";
  220. this.lblProgress.Size = new System.Drawing.Size(358, 19);
  221. this.lblProgress.TabIndex = 5;
  222. //
  223. // TransferProgressDialog
  224. //
  225. this.AutoScaleMode = System.Windows.Forms.AutoScaleMode.None;
  226. this.BackColor = System.Drawing.SystemColors.Control;
  227. this.CancelButton = this.btnCancel;
  228. this.ClientSize = new System.Drawing.Size(378, 98);
  229. this.Controls.Add(this.lblProgress);
  230. this.Controls.Add(this.progressBar);
  231. this.Controls.Add(this.lblTitle);
  232. this.Controls.Add(this.btnCancel);
  233. this.FormBorderStyle = System.Windows.Forms.FormBorderStyle.FixedSingle;
  234. this.Location = new System.Drawing.Point(0, 0);
  235. this.MaximizeBox = false;
  236. this.MinimizeBox = false;
  237. this.Name = "TransferProgressDialog";
  238. this.ShowIcon = false;
  239. this.StartPosition = System.Windows.Forms.FormStartPosition.CenterParent;
  240. this.Text = "";
  241. this.Controls.SetChildIndex(this.btnCancel, 0);
  242. this.Controls.SetChildIndex(this.lblTitle, 0);
  243. this.Controls.SetChildIndex(this.progressBar, 0);
  244. this.Controls.SetChildIndex(this.lblProgress, 0);
  245. this.ResumeLayout(false);
  246. }
  247. #endregion
  248. private void CancelButton_Click(object sender, EventArgs e)
  249. {
  250. OnCancelClicked();
  251. this.Close();
  252. }
  253. }
  254. }