using System; using System.Threading; using System.Windows.Forms; namespace CNAS_DBSync { public partial class frmPrBar : Form { /// /// 显示百分比(例:30%)或完成数(例:138/2000) /// private static bool _ShowPercentage; /// /// 构造函数 /// /// 窗体标题 /// 滚动条上方显示的文字提示 /// 总任务数 /// 显示百分比(例:30%)或完成数(例:138/2000) /// 是否显示关闭按钮 /// 是否显示开始/暂停/继续/停止按钮 public frmPrBar(string title, string customText, int taskCount, bool showPercentage, bool showClose, bool showCustomButton) { InitializeComponent(); this.Text = title; SetText(customText); //pictureBoxCancel.Visible = showCustomClose; _ShowPercentage = showPercentage; this.ControlBox = showClose; btnStart.Visible = showCustomButton; btnPause.Visible = showCustomButton; btnGoon.Visible = showCustomButton; btnStop.Visible = showCustomButton; ProgressBarService.Status = ProgressBarRunStatus.None; this.progressBar1.Maximum = taskCount; this.progressBar1.Value = 0; } protected override void WndProc(ref Message m) { const int WM_SYSCOMMAND = 0x0112; const int SC_CLOSE = 0xF060; if (m.Msg == WM_SYSCOMMAND && (int)m.WParam == SC_CLOSE) { //Application.ExitThread(); //ProgressBarService.CloseBarForm(); ProgressBarService.Status = ProgressBarRunStatus.Close; return; } base.WndProc(ref m); } /// /// 委托修改等待窗显示的文字 /// /// private delegate void SetTextHandler(string text); public void SetText(string text) { if (lblMsg.InvokeRequired) this.Invoke(new SetTextHandler(SetText), text); else { if (text.ToUpper() == "CLOSE") { this.Close(); this.Dispose(); } else lblMsg.Text = text; } } private delegate void UpdateUI(int step); /// /// 更新UI /// /// public void UpdataUIStatus(int step) { try { if (this.IsDisposed || !this.IsHandleCreated) return; if (InvokeRequired) { this.Invoke(new UpdateUI(delegate (int s) { this.progressBar1.Value = s; //this.progressBar1.Value += s; if (_ShowPercentage) this.lblCount.Text = (this.progressBar1.Value * 100 / this.progressBar1.Maximum).ToString() + "%"; else this.lblCount.Text = this.progressBar1.Value.ToString() + "/" + this.progressBar1.Maximum.ToString(); if (this.progressBar1.Value == this.progressBar1.Maximum) this.Close(); }), step); } else { if (this.progressBar1.Value >= this.progressBar1.Maximum) this.progressBar1.Value = 0; this.progressBar1.Value = step; //this.progressBar1.Value += step; if (_ShowPercentage) this.lblCount.Text = (this.progressBar1.Value * 100 / this.progressBar1.Maximum).ToString() + "%"; else this.lblCount.Text = this.progressBar1.Value.ToString() + "/" + this.progressBar1.Maximum.ToString(); } } catch (Exception ex) { } } /// /// 开始 /// /// /// private void btnStart_Click(object sender, EventArgs e) { ProgressBarService.Status = ProgressBarRunStatus.Start; btnStart.Enabled = false; btnPause.Enabled = true; btnGoon.Enabled = true; btnStop.Enabled = true; this.ControlBox = false; } /// /// 暂停 /// /// /// private void btnPause_Click(object sender, EventArgs e) { //ProgressBarService.PauseProgress(); ProgressBarService.Status = ProgressBarRunStatus.Pause; btnStart.Enabled = false; btnPause.Enabled = false; btnGoon.Enabled = true; btnStop.Enabled = true; this.ControlBox = false; } /// /// 继续 /// /// /// private void btnGoon_Click(object sender, EventArgs e) { ProgressBarService.Status = ProgressBarRunStatus.GoOn; btnStart.Enabled = false; btnPause.Enabled = true; btnGoon.Enabled = false; btnStop.Enabled = true; this.ControlBox = false; } /// /// 停止 /// /// /// private void btnStop_Click(object sender, EventArgs e) { ProgressBarService.Status = ProgressBarRunStatus.Stop; btnStart.Enabled = true; btnPause.Enabled = false; btnGoon.Enabled = false; btnStop.Enabled = false; this.ControlBox = true; } } public class ProgressBarService { private Thread thred = null; private frmPrBar bar = null; public ProgressBarService() { } private static ProgressBarService _instance; private static readonly Object syncLock = new Object(); public static ProgressBarService Instance { get { if (ProgressBarService._instance == null) { lock (syncLock) { if (ProgressBarService._instance == null) { ProgressBarService._instance = new ProgressBarService(); } } } return ProgressBarService._instance; } } #region 创建等待窗口 /// /// 创建等待窗口并写入提示文字 /// /// 标题 /// 提示文字 /// 步长 /// 显示百分比(例:30%)或完成数(例:138/2000).默认false显示完成数 /// 是否显示关闭窗口按钮(true显示;false隐藏),不传值默认fasle /// 是否显示开始/暂停/继续/停止按钮(true显示;false隐藏),不传值默认fasle public static void CreateBarForm(string title, string text, int Count, bool showPercentage = false, bool showClose = false, bool showCustomButton = false) { ProgressBarService.Instance.CreateForm(title, text, Count, showPercentage, showClose, showCustomButton); } int tid1 = 0; private void CreateForm(string title, string text, int Count, bool showPercentage, bool showClose, bool showCustomButton) { int tid2 = Thread.CurrentThread.ManagedThreadId; if (tid1 == tid2) return; tid1 = tid2; if (thred != null) { try { thred = null; bar = null; } catch (Exception) { } } thred = new Thread(new ThreadStart(delegate () { bar = new frmPrBar(title, text, Count, showPercentage, showClose, showCustomButton); System.Windows.Forms.Application.Run(bar); })); thred.Start(); } #endregion #region 关闭等待窗口 /// /// 关闭窗口 /// public static void CloseBarForm() { ProgressBarService.Instance.CloseForm(); } private void CloseForm() { tid1 = 0; if (thred != null) { try { bar.SetText("CLOSE"); thred = null; bar = null; } catch (Exception) { } } } #endregion #region 修改等待窗体的提示文字 /// /// 修改提示文字 /// /// 提示文字 public static void SetBarFormCaption(string text) { ProgressBarService.Instance.SetFormCaption(text); } private void SetFormCaption(string text) { if (bar != null) { try { bar.SetText(text); } catch (Exception) { } } } #endregion #region 更新进度条 /// /// 更新进度 /// /// public static void UpdateProgress(int step) { ProgressBarService.Instance.SetProgress(step); } private void SetProgress(int step) { if (bar != null) { try { bar.UpdataUIStatus(step); } catch (Exception) { } } } #endregion #region 进度条运行状态 public static ProgressBarRunStatus Status { get; set; } #endregion } public enum ProgressBarRunStatus { /// /// 无操作 /// None, /// /// 开始 /// Start, /// /// 暂停 /// Pause, /// /// 继续 /// GoOn, /// /// 停止 /// Stop, /// /// 关闭 /// Close } }