TransferProgressDialog.cs 9.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285
  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. else
  54. {
  55. dialog.CancelVisible = false;
  56. }
  57. if (string.IsNullOrEmpty(canceltext))
  58. dialog.btnCancel.Text = canceltext;
  59. new Task(() =>
  60. {
  61. proc();
  62. try//防止取消后关闭引发异常
  63. {
  64. dialog.Invoke(new Action(dialog.Close));
  65. }
  66. catch { }
  67. }).Start();
  68. dialog.ShowDialog();
  69. }
  70. public System.Drawing.Color lblTitleBackColor
  71. {
  72. set
  73. {
  74. this.lblTitle.BackColor = value;
  75. }
  76. }
  77. public string Title
  78. {
  79. get
  80. {
  81. return this.Text;
  82. }
  83. set
  84. {
  85. Text = value;
  86. }
  87. }
  88. public string ProcessMsg
  89. {
  90. get
  91. {
  92. return this.lblProgress.Text;
  93. }
  94. set
  95. {
  96. this.lblProgress.Text = value;
  97. }
  98. }
  99. public event EventHandler CancelClicked;
  100. protected virtual void OnCancelClicked()
  101. {
  102. if (CancelClicked != null)
  103. {
  104. CancelClicked(this, EventArgs.Empty);
  105. }
  106. }
  107. public ProgressBar ProgressBar
  108. {
  109. get
  110. {
  111. return this.progressBar;
  112. }
  113. }
  114. public bool CancelEnabled
  115. {
  116. get
  117. {
  118. return this.btnCancel.Enabled;
  119. }
  120. set
  121. {
  122. this.btnCancel.Enabled = value;
  123. }
  124. }
  125. public bool CancelVisible
  126. {
  127. get
  128. {
  129. return this.btnCancel.Visible;
  130. }
  131. set
  132. {
  133. this.btnCancel.Visible = value;
  134. }
  135. }
  136. public TransferProgressDialog()
  137. {
  138. PdnBaseForm.RegisterFormHotKey(
  139. Keys.Escape,
  140. delegate (Keys keys)
  141. {
  142. OnCancelClicked();
  143. return true;
  144. });
  145. InitializeComponent();
  146. }
  147. protected override void OnLoad(EventArgs e)
  148. {
  149. SystemLayer.UI.DisableCloseBox(this);
  150. base.OnLoad(e);
  151. }
  152. public override void LoadResources()
  153. {
  154. this.btnCancel.Text = PdnResources.GetString("Form.CancelButton.Text");
  155. base.LoadResources();
  156. }
  157. /// <summary>
  158. /// Clean up any resources being used.
  159. /// </summary>
  160. /// <param name="disposing">true if managed resources should be disposed; otherwise, false.</param>
  161. protected override void Dispose(bool disposing)
  162. {
  163. if (disposing && (components != null))
  164. {
  165. components.Dispose();
  166. }
  167. base.Dispose(disposing);
  168. }
  169. #region Windows Form Designer generated code
  170. /// <summary>
  171. /// Required method for Designer support - do not modify
  172. /// the contents of this method with the code editor.
  173. /// </summary>
  174. private void InitializeComponent()
  175. {
  176. this.progressBar = new System.Windows.Forms.ProgressBar();
  177. this.btnCancel = new System.Windows.Forms.Button();
  178. this.lblTitle = new System.Windows.Forms.Label();
  179. this.lblProgress = new System.Windows.Forms.Label();
  180. this.SuspendLayout();
  181. //
  182. // progressBar
  183. //
  184. this.progressBar.Anchor = ((System.Windows.Forms.AnchorStyles)((System.Windows.Forms.AnchorStyles.Bottom | System.Windows.Forms.AnchorStyles.Left)));
  185. this.progressBar.Location = new System.Drawing.Point(10, 34);
  186. this.progressBar.Name = "progressBar";
  187. this.progressBar.Size = new System.Drawing.Size(355, 22);
  188. this.progressBar.TabIndex = 0;
  189. //
  190. // btnCancel
  191. //
  192. this.btnCancel.Anchor = ((System.Windows.Forms.AnchorStyles)((System.Windows.Forms.AnchorStyles.Bottom | System.Windows.Forms.AnchorStyles.Left)));
  193. this.btnCancel.DialogResult = System.Windows.Forms.DialogResult.Cancel;
  194. this.btnCancel.Location = new System.Drawing.Point(281, 63);
  195. this.btnCancel.Name = "btnCancel";
  196. this.btnCancel.Size = new System.Drawing.Size(84, 26);
  197. this.btnCancel.TabIndex = 1;
  198. this.btnCancel.Text = "cancelButton";
  199. this.btnCancel.UseVisualStyleBackColor = true;
  200. this.btnCancel.Click += new System.EventHandler(this.CancelButton_Click);
  201. //
  202. // lblTitle
  203. //
  204. this.lblTitle.AutoEllipsis = true;
  205. this.lblTitle.BackColor = System.Drawing.Color.White;
  206. this.lblTitle.Dock = System.Windows.Forms.DockStyle.Top;
  207. this.lblTitle.Font = new System.Drawing.Font("宋体", 9F, System.Drawing.FontStyle.Regular, System.Drawing.GraphicsUnit.Point, ((byte)(134)));
  208. this.lblTitle.ForeColor = System.Drawing.Color.Black;
  209. this.lblTitle.Location = new System.Drawing.Point(0, 0);
  210. this.lblTitle.Name = "lblTitle";
  211. this.lblTitle.Padding = new System.Windows.Forms.Padding(3);
  212. this.lblTitle.Size = new System.Drawing.Size(378, 20);
  213. this.lblTitle.TabIndex = 2;
  214. this.lblTitle.Text = "itemText";
  215. this.lblTitle.Visible = false;
  216. //
  217. // lblProgress
  218. //
  219. this.lblProgress.Anchor = ((System.Windows.Forms.AnchorStyles)((System.Windows.Forms.AnchorStyles.Bottom | System.Windows.Forms.AnchorStyles.Left)));
  220. this.lblProgress.AutoEllipsis = true;
  221. this.lblProgress.Font = new System.Drawing.Font("宋体", 9F, System.Drawing.FontStyle.Regular, System.Drawing.GraphicsUnit.Point, ((byte)(134)));
  222. this.lblProgress.Location = new System.Drawing.Point(8, 10);
  223. this.lblProgress.Name = "lblProgress";
  224. this.lblProgress.Size = new System.Drawing.Size(358, 19);
  225. this.lblProgress.TabIndex = 5;
  226. //
  227. // TransferProgressDialog
  228. //
  229. this.AutoScaleMode = System.Windows.Forms.AutoScaleMode.None;
  230. this.BackColor = System.Drawing.SystemColors.Control;
  231. this.CancelButton = this.btnCancel;
  232. this.ClientSize = new System.Drawing.Size(378, 98);
  233. this.Controls.Add(this.lblProgress);
  234. this.Controls.Add(this.progressBar);
  235. this.Controls.Add(this.lblTitle);
  236. this.Controls.Add(this.btnCancel);
  237. this.FormBorderStyle = System.Windows.Forms.FormBorderStyle.FixedSingle;
  238. this.MaximizeBox = false;
  239. this.MinimizeBox = false;
  240. this.Name = "TransferProgressDialog";
  241. this.ShowIcon = false;
  242. this.StartPosition = System.Windows.Forms.FormStartPosition.CenterParent;
  243. this.Text = "";
  244. this.Controls.SetChildIndex(this.btnCancel, 0);
  245. this.Controls.SetChildIndex(this.lblTitle, 0);
  246. this.Controls.SetChildIndex(this.progressBar, 0);
  247. this.Controls.SetChildIndex(this.lblProgress, 0);
  248. this.ResumeLayout(false);
  249. }
  250. #endregion
  251. private void CancelButton_Click(object sender, EventArgs e)
  252. {
  253. OnCancelClicked();
  254. this.Close();
  255. }
  256. }
  257. }