| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282 | 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;        /// <summary>        /// Required designer variable.        /// </summary>        private System.ComponentModel.IContainer components = null;        /// <summary>        /// 执行异步方法的ProgressDialog,本Dialog需要调用者打开关闭        /// </summary>        /// <param name="title">标题</param>        /// <param name="msg">处理信息</param>        /// <param name="proc">同步方法</param>        /// <param name="onCancel">取消事件</param>        /// <param name="canceltext">取消按钮文本</param>        /// <returns>Dialog实例</returns>        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;        }        /// <summary>        /// 执行同步方法的ProgressDialog,本Dialog自动执行打开关闭        /// </summary>        /// <param name="title">标题</param>        /// <param name="msg">处理信息</param>        /// <param name="proc">同步方法</param>        /// <param name="onCancel">取消事件</param>        /// <param name="canceltext">取消按钮文本</param>        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; }            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();        }        /// <summary>        /// Clean up any resources being used.        /// </summary>        /// <param name="disposing">true if managed resources should be disposed; otherwise, false.</param>        protected override void Dispose(bool disposing)        {            if (disposing && (components != null))            {                components.Dispose();            }            base.Dispose(disposing);        }        #region Windows Form Designer generated code        /// <summary>        /// Required method for Designer support - do not modify        /// the contents of this method with the code editor.        /// </summary>        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.Location = new System.Drawing.Point(0, 0);            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();        }    }}
 |