TransferProgressDialog.cs 9.8 KB

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