前提
入行已经 7,8 年了, 一直想做一套漂亮点的自定义控件, 于是就有了本系列文章.
GitHub: https://github.com/kwwwvagaa/NetWinformControl
码云:
如果觉得写的还行, 请点个 star 支持一下吧
欢迎前来交流探讨: 企鹅群 568015492
麻烦博客下方点个 [推荐] , 谢谢
- NuGet
- Install-Package HZH_Controls
目录
https://www.cnblogs.com/bfyx/p/11364884.html
用处及效果
准备工作
这个用到了基类窗体 (十七)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://www.cnblogs.com/bfyx/p/11589145.html