界面设计如下:
实现类:
public partial class FormProgressInfo : Form
{
public FormProgressInfo(int nMinimum = 0, int nMaximum = 100)
{
InitializeComponent();
if (nMinimum >= 0 && nMaximum > nMinimum)
{
progressBarInfo.Maximum = nMaximum;
progressBarInfo.Minimum = nMinimum;
progressBarInfo.Value = nMinimum;
}
}
public void SetPos(int nPos)
{
if (nPos <= progressBarInfo.Maximum)
{
progressBarInfo.Value = nPos;
labelProgressValue.Text = $"{nPos * 100 / progressBarInfo.Maximum}%";
}
Application.DoEvents();
}
public void SetTip(string sTip = "正在执行...")
{
if (!string.IsNullOrEmpty(sTip))
labelProgressTip.Text = sTip;
Application.DoEvents();
}
private void FormProgressInfo_Load(object sender, EventArgs e)
{
if (null != Owner)
Owner.Enabled = false;
}
private void FormProgressInfo_FormClosed(object sender, FormClosedEventArgs e)
{
if (null != Owner)
Owner.Enabled = true;
}
}
调用:
var formProgress = new FormProgressInfo(0, 1024);
formProgress.Show(this); //this用于指定父窗体
写进度、写提示应用SetPos、SetTip就好了。