using System;
using System.Threading;
using System.Threading.Tasks;
using System.Windows.Forms;
namespace PaintDotNet
{
public class TransferProgressDialog : PdnBaseForm
{
private ProgressBar progressBar;
private Button btnCancel;
private Label lblTitle;
private Label lblProgress;
///
/// Required designer variable.
///
private System.ComponentModel.IContainer components = null;
///
/// 执行异步方法的ProgressDialog,本Dialog需要调用者打开关闭
///
/// 标题
/// 处理信息
/// 同步方法
/// 取消事件
/// 取消按钮文本
/// Dialog实例
public static TransferProgressDialog CreatDialog(string title, string msg, EventHandler onCancel, string canceltext = null)
{
var dialog = new TransferProgressDialog();
dialog.Text = title;
dialog.ProcessMsg = msg;
dialog.CancelClicked += onCancel;
dialog.ProgressBar.Style = ProgressBarStyle.Marquee;
if (string.IsNullOrEmpty(canceltext))
dialog.btnCancel.Text = canceltext;
return dialog;
}
///
/// 执行同步方法的ProgressDialog,本Dialog自动执行打开关闭
///
/// 标题
/// 处理信息
/// 同步方法
/// 取消事件
/// 取消按钮文本
public static void ShowDialog(string title, string msg, Action proc, EventHandler onCancel = null, string canceltext = null)
{
var dialog = new TransferProgressDialog();
dialog.Text = title;
dialog.ProcessMsg = String.IsNullOrWhiteSpace(msg) ? "Please waiting..." : msg;
dialog.ProgressBar.Style = ProgressBarStyle.Marquee;
if (onCancel != null)
{ dialog.CancelClicked += onCancel; }
else
{
dialog.CancelVisible = false;
}
if (string.IsNullOrEmpty(canceltext))
dialog.btnCancel.Text = canceltext;
new Task(() =>
{
proc();
try//防止取消后关闭引发异常
{
dialog.Invoke(new Action(dialog.Close));
}
catch { }
}).Start();
dialog.ShowDialog();
}
public System.Drawing.Color lblTitleBackColor
{
set
{
this.lblTitle.BackColor = value;
}
}
public string Title
{
get
{
return this.Text;
}
set
{
Text = value;
}
}
public string ProcessMsg
{
get
{
return this.lblProgress.Text;
}
set
{
this.lblProgress.Text = value;
}
}
public event EventHandler CancelClicked;
protected virtual void OnCancelClicked()
{
if (CancelClicked != null)
{
CancelClicked(this, EventArgs.Empty);
}
}
public ProgressBar ProgressBar
{
get
{
return this.progressBar;
}
}
public bool CancelEnabled
{
get
{
return this.btnCancel.Enabled;
}
set
{
this.btnCancel.Enabled = value;
}
}
public bool CancelVisible
{
get
{
return this.btnCancel.Visible;
}
set
{
this.btnCancel.Visible = value;
}
}
public TransferProgressDialog()
{
PdnBaseForm.RegisterFormHotKey(
Keys.Escape,
delegate (Keys keys)
{
OnCancelClicked();
return true;
});
InitializeComponent();
}
protected override void OnLoad(EventArgs e)
{
SystemLayer.UI.DisableCloseBox(this);
base.OnLoad(e);
}
public override void LoadResources()
{
this.btnCancel.Text = PdnResources.GetString("Form.CancelButton.Text");
base.LoadResources();
}
///
/// Clean up any resources being used.
///
/// true if managed resources should be disposed; otherwise, false.
protected override void Dispose(bool disposing)
{
if (disposing && (components != null))
{
components.Dispose();
}
base.Dispose(disposing);
}
#region Windows Form Designer generated code
///
/// Required method for Designer support - do not modify
/// the contents of this method with the code editor.
///
private void InitializeComponent()
{
this.progressBar = new System.Windows.Forms.ProgressBar();
this.btnCancel = new System.Windows.Forms.Button();
this.lblTitle = new System.Windows.Forms.Label();
this.lblProgress = new System.Windows.Forms.Label();
this.SuspendLayout();
//
// progressBar
//
this.progressBar.Anchor = ((System.Windows.Forms.AnchorStyles)((System.Windows.Forms.AnchorStyles.Bottom | System.Windows.Forms.AnchorStyles.Left)));
this.progressBar.Location = new System.Drawing.Point(10, 34);
this.progressBar.Name = "progressBar";
this.progressBar.Size = new System.Drawing.Size(355, 22);
this.progressBar.TabIndex = 0;
//
// btnCancel
//
this.btnCancel.Anchor = ((System.Windows.Forms.AnchorStyles)((System.Windows.Forms.AnchorStyles.Bottom | System.Windows.Forms.AnchorStyles.Left)));
this.btnCancel.DialogResult = System.Windows.Forms.DialogResult.Cancel;
this.btnCancel.Location = new System.Drawing.Point(281, 63);
this.btnCancel.Name = "btnCancel";
this.btnCancel.Size = new System.Drawing.Size(84, 26);
this.btnCancel.TabIndex = 1;
this.btnCancel.Text = "cancelButton";
this.btnCancel.UseVisualStyleBackColor = true;
this.btnCancel.Click += new System.EventHandler(this.CancelButton_Click);
//
// lblTitle
//
this.lblTitle.AutoEllipsis = true;
this.lblTitle.BackColor = System.Drawing.Color.White;
this.lblTitle.Dock = System.Windows.Forms.DockStyle.Top;
this.lblTitle.Font = new System.Drawing.Font("宋体", 9F, System.Drawing.FontStyle.Regular, System.Drawing.GraphicsUnit.Point, ((byte)(134)));
this.lblTitle.ForeColor = System.Drawing.Color.Black;
this.lblTitle.Location = new System.Drawing.Point(0, 0);
this.lblTitle.Name = "lblTitle";
this.lblTitle.Padding = new System.Windows.Forms.Padding(3);
this.lblTitle.Size = new System.Drawing.Size(378, 20);
this.lblTitle.TabIndex = 2;
this.lblTitle.Text = "itemText";
this.lblTitle.Visible = false;
//
// lblProgress
//
this.lblProgress.Anchor = ((System.Windows.Forms.AnchorStyles)((System.Windows.Forms.AnchorStyles.Bottom | System.Windows.Forms.AnchorStyles.Left)));
this.lblProgress.AutoEllipsis = true;
this.lblProgress.Font = new System.Drawing.Font("宋体", 9F, System.Drawing.FontStyle.Regular, System.Drawing.GraphicsUnit.Point, ((byte)(134)));
this.lblProgress.Location = new System.Drawing.Point(8, 10);
this.lblProgress.Name = "lblProgress";
this.lblProgress.Size = new System.Drawing.Size(358, 19);
this.lblProgress.TabIndex = 5;
//
// TransferProgressDialog
//
this.AutoScaleMode = System.Windows.Forms.AutoScaleMode.None;
this.BackColor = System.Drawing.SystemColors.Control;
this.CancelButton = this.btnCancel;
this.ClientSize = new System.Drawing.Size(378, 98);
this.Controls.Add(this.lblProgress);
this.Controls.Add(this.progressBar);
this.Controls.Add(this.lblTitle);
this.Controls.Add(this.btnCancel);
this.FormBorderStyle = System.Windows.Forms.FormBorderStyle.FixedSingle;
this.MaximizeBox = false;
this.MinimizeBox = false;
this.Name = "TransferProgressDialog";
this.ShowIcon = false;
this.StartPosition = System.Windows.Forms.FormStartPosition.CenterParent;
this.Text = "";
this.Controls.SetChildIndex(this.btnCancel, 0);
this.Controls.SetChildIndex(this.lblTitle, 0);
this.Controls.SetChildIndex(this.progressBar, 0);
this.Controls.SetChildIndex(this.lblProgress, 0);
this.ResumeLayout(false);
}
#endregion
private void CancelButton_Click(object sender, EventArgs e)
{
OnCancelClicked();
this.Close();
}
}
}