文章目录
用处及效果

准备工作
这个用到了基类窗体 (十七)c#Winform自定义控件-基类窗体 ,如果不了解可以先移步看一下
开始
添加一个窗体FrmLoading 继承 FrmBase
东西不多,看全部代码
// ***********************************************************************
// Assembly : HZH_Controls
// Created : 2019-09-26
//
// ***********************************************************************
// <copyright file="FrmLoading.cs">
// Copyright by Huang Zhenghui(黄正辉) All, QQ group:568015492 QQ:623128629 Email:623128629@qq.com
// </copyright>
//
// Blog: https://www.cnblogs.com/bfyx
// GitHub:https://github.com/kwwwvagaa/NetWinformControl
// gitee:https://gitee.com/kwwwvagaa/net_winform_custom_control.git
//
// If you use this code, please keep this note.
// ***********************************************************************
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading;
using System.Windows.Forms;
namespace HZH_Controls.Forms
{
/// <summary>
/// Class FrmLoading.
/// Implements the <see cref="HZH_Controls.Forms.FrmBase" />
/// </summary>
/// <seealso cref="HZH_Controls.Forms.FrmBase" />
public partial class FrmLoading : FrmBase
{
/// <summary>
/// The update database worker
/// </summary>
BackgroundWorker updateDBWorker = new BackgroundWorker();
/// <summary>
/// 获取或设置加载任务
/// </summary>
/// <value>The background work action.</value>
public Action BackgroundWorkAction
{
get;
set;
}
/// <summary>
/// 设置当前执行进度及任务名称,key:任务进度,取值0-100 value:当前任务名称
/// </summary>
/// <value>The current MSG.</value>
public KeyValuePair<int, string> CurrentMsg
{
set
{
this.updateDBWorker.ReportProgress(value.Key, value.Value);
}
}
/// <summary>
/// Initializes a new instance of the <see cref="FrmLoading"/> class.
/// </summary>
public FrmLoading()
{
InitializeComponent();
this.updateDBWorker.WorkerReportsProgress = true;
this.updateDBWorker.WorkerSupportsCancellation = true;
this.updateDBWorker.DoWork += new DoWorkEventHandler(this.backgroundWorker1_DoWork);
this.updateDBWorker.ProgressChanged += new ProgressChangedEventHandler(this.backgroundWorker1_ProgressChanged);
}
/// <summary>
/// 设置进度信息,重写此函数可以处理界面信息绑定
/// </summary>
/// <param name="strText">进度任务名称</param>
/// <param name="intValue">进度值</param>
protected virtual void BindingProcessMsg(string strText, int intValue)
{
}
/// <summary>
/// Sets the message.
/// </summary>
/// <param name="strText">The string text.</param>
/// <param name="intValue">The int value.</param>
private void SetMessage(string strText, int intValue)
{
if (this.InvokeRequired)
{
this.BeginInvoke(new MethodInvoker(delegate() { SetMessage(strText, intValue); }));
}
else
{
BindingProcessMsg(strText, intValue);
}
}
/// <summary>
/// Handles the Load event of the FrmLoading control.
/// </summary>
/// <param name="sender">The source of the event.</param>
/// <param name="e">The <see cref="EventArgs"/> instance containing the event data.</param>
private void FrmLoading_Load(object sender, EventArgs e)
{
if (ControlHelper.IsDesignMode())
return;
this.updateDBWorker.RunWorkerAsync();
}
/// <summary>
/// Handles the DoWork event of the backgroundWorker1 control.
/// </summary>
/// <param name="sender">The source of the event.</param>
/// <param name="e">The <see cref="DoWorkEventArgs"/> instance containing the event data.</param>
private void backgroundWorker1_DoWork(object sender, DoWorkEventArgs e)
{
if (this.BackgroundWorkAction != null)
{
this.BackgroundWorkAction();
}
Thread.Sleep(100);
if (base.InvokeRequired)
{
base.BeginInvoke(new MethodInvoker(delegate
{
base.Close();
}));
}
else
{
base.Close();
}
}
/// <summary>
/// Handles the ProgressChanged event of the backgroundWorker1 control.
/// </summary>
/// <param name="sender">The source of the event.</param>
/// <param name="e">The <see cref="ProgressChangedEventArgs"/> instance containing the event data.</param>
private void backgroundWorker1_ProgressChanged(object sender, ProgressChangedEventArgs e)
{
SetMessage((e.UserState == null) ? "" : e.UserState.ToString(), e.ProgressPercentage);
}
}
}
说明:
BackgroundWorkAction:加载资源任务函数
CurrentMsg:当前需要显示的进度信息,key:任务进度,取值0-100 value:当前任务名称
BindingProcessMsg:向界面绑定数据,子类需要重写此函数来实现向界面绑定显示数据
示例:
添加一个窗体FrmTestLoading 继承FrmLoading
添加一个文本label1显示进度信息文字
添加一个进度条ucProcessLineExt1显示进度值
重新BindingProcessMsg绑定信息
protected override void BindingProcessMsg(string strText, int intValue)
{
label1.Text = strText;
this.ucProcessLineExt1.Value = intValue;
}
调用
FrmTestLoading frmLoading = new FrmTestLoading();
frmLoading.BackgroundWorkAction = delegate()
{
try
{
frmLoading.CurrentMsg = new KeyValuePair<int, string>(1, "正在初始化配置...");
Thread.Sleep(1000);
frmLoading.CurrentMsg = new KeyValuePair<int, string>(10, "正在加载第一个资源...");
Thread.Sleep(1000);
frmLoading.CurrentMsg = new KeyValuePair<int, string>(20, "正在加载第二个资源...");
Thread.Sleep(1000);
frmLoading.CurrentMsg = new KeyValuePair<int, string>(30, "正在加载第三个资源...");
Thread.Sleep(1000);
frmLoading.CurrentMsg = new KeyValuePair<int, string>(40, "正在加载第四个资源...");
Thread.Sleep(1000);
frmLoading.CurrentMsg = new KeyValuePair<int, string>(50, "正在加载第五个资源...");
Thread.Sleep(1000);
frmLoading.CurrentMsg = new KeyValuePair<int, string>(60, "正在加载第六个资源...");
Thread.Sleep(1000);
frmLoading.CurrentMsg = new KeyValuePair<int, string>(70, "正在加载第七个资源...");
Thread.Sleep(1000);
frmLoading.CurrentMsg = new KeyValuePair<int, string>(80, "正在加载第八个资源...");
Thread.Sleep(1000);
frmLoading.CurrentMsg = new KeyValuePair<int, string>(90, "正在加载第九个资源...");
Thread.Sleep(1000);
frmLoading.CurrentMsg = new KeyValuePair<int, string>(1000, "数据加载完成...");
Thread.Sleep(1000);
}
catch (Exception ex)
{
MessageBox.Show("加载资源时出现错误");
}
};
frmLoading.ShowDialog();
最后的话
如果你喜欢的话,请到 https://gitee.com/kwwwvagaa/net_winform_custom_control 点个星星吧
作者:冰封一夏
出处: http://www.hzhcontrols.com/doc.html
HZHControls官网:http://www.hzhcontrols.com
本文版权归作者和博客园共有,欢迎转载,但未经作者同意必须保留此段声明,
且在文章页面明显位置给出原文连接,否则保留追究法律责任的权利。
GitHub:https://github.com/kwwwvagaa/NetWinformControl
码云:https://gitee.com/kwwwvagaa/net_winform_custom_control.git