(七十三)c#Winform自定义控件-资源加载窗体
官网
前提
入行已经7,8年了,一直想做一套漂亮点的自定义控件,于是就有了本系列文章。
GitHub:https://github.com/kwwwvagaa/NetWinformControl
码云:https://gitee.com/kwwwvagaa/net_winform_custom_control.git
如果觉得写的还行,请点个 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();
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>(, "正在初始化配置...");
Thread.Sleep();
frmLoading.CurrentMsg = new KeyValuePair<int, string>(, "正在加载第一个资源...");
Thread.Sleep();
frmLoading.CurrentMsg = new KeyValuePair<int, string>(, "正在加载第二个资源...");
Thread.Sleep();
frmLoading.CurrentMsg = new KeyValuePair<int, string>(, "正在加载第三个资源...");
Thread.Sleep();
frmLoading.CurrentMsg = new KeyValuePair<int, string>(, "正在加载第四个资源...");
Thread.Sleep();
frmLoading.CurrentMsg = new KeyValuePair<int, string>(, "正在加载第五个资源...");
Thread.Sleep();
frmLoading.CurrentMsg = new KeyValuePair<int, string>(, "正在加载第六个资源...");
Thread.Sleep();
frmLoading.CurrentMsg = new KeyValuePair<int, string>(, "正在加载第七个资源...");
Thread.Sleep();
frmLoading.CurrentMsg = new KeyValuePair<int, string>(, "正在加载第八个资源...");
Thread.Sleep();
frmLoading.CurrentMsg = new KeyValuePair<int, string>(, "正在加载第九个资源...");
Thread.Sleep();
frmLoading.CurrentMsg = new KeyValuePair<int, string>(, "数据加载完成...");
Thread.Sleep();
}
catch (Exception ex)
{
MessageBox.Show("加载资源时出现错误");
}
};
frmLoading.ShowDialog();
最后的话
如果你喜欢的话,请到 https://gitee.com/kwwwvagaa/net_winform_custom_control 点个星星吧
(七十三)c#Winform自定义控件-资源加载窗体的更多相关文章
- 手撸Spring框架,设计与实现资源加载器,从Spring.xml解析和注册Bean对象
作者:小傅哥 博客:https://bugstack.cn 沉淀.分享.成长,让自己和他人都能有所收获! 一.前言 你写的代码,能接的住产品加需求吗? 接,是能接的,接几次也行,哪怕就一个类一片的 i ...
- Direct2D开发:从资源加载位图
转载请注明出处:http://www.cnblogs.com/Ray1024 一.概述 Direct2D使用Windows图像处理组件 (WIC) 来加载位图.从文件加载位图的方法很简单,而且网上的教 ...
- prelaod场景,用来显示资源加载进度
phaser.js的源码可以到它在github上的托管里去下载,游戏要用到的图片声音等素材资源请点击这里下载.Phaser的使用非常简单,只需要引入它的主文件,然后在页面中指定一个用来放置canvas ...
- 【Android开发学习笔记】【高级】【随笔】插件化——资源加载
前言 上一节我们针对插件最基本的原理进行了一个简单的demo实现,但是由于插件的Context对象被宿主所接管,因此无法加载插件程序的资源.那么如何解决这个问题捏? 有人提出这样的方案:将apk中的资 ...
- Duilib学习笔记《07》— 资源加载
Duilib的界面表现力能如此丰富,很大程度上得益于贴图描述的简单强大.通过之前的学习及参看相关例子,我们可以发现,在XML布局文件中,不管是窗体背景还是控件,都添加了对应的图片资源以此来美化界面.而 ...
- WinForm的延时加载控件概述
这篇文章主要介绍了WinForm的延时加载控件,很实用的技巧,在C#程序设计中有着比较广泛的应用,需要的朋友可以参考下 本文主要针对WinForm的延迟加载在常用控件的实现做简单的描述.在进行C# ...
- Unity3D之Mecanim动画系统学习笔记(十):Mecanim动画的资源加载相关
资源加载是必备的知识点,这里就说说Mecanim动画的资源如何打包及加载. 注意,Unity4.x和Unity5.x的AssetBundle打包策略不一样,本笔记是基于Unity4.x的AssetBu ...
- cocos2d-x Loading界面实现资源加载
有时候场景中的资源加载过多的话就会引起游戏进入的时候很卡,因为那是边加载边显示.在tests例子里面有一个很好的例子叫做TextureCacheTest,里面讲解了如何写loading. #inclu ...
- 【原】从一个bug浅谈YUI3组件的资源加载
篇前声明:为了不涉及业务细节,篇内信息统一以某游戏,某功能代替 前不久,某游戏准备内测客户端,开发人员测试过程中发现某功能突然不灵了,之前的测试一切ok,没有发现任何异常,第一反应是,游戏内浏览器都是 ...
随机推荐
- 《Java 8 in Action》Chapter 3:Lambda表达式
1. Lambda简介 可以把Lambda表达式理解为简洁地表示可传递的匿名函数的一种方式:它没有名称,但它有参数列表.函数主体.返回类型,可能还有一个可以抛出的异常列表. 匿名--我们说匿名,是因为 ...
- Spring源码剖析4:其余方式获取Bean的过程分析
原型Bean加载过程 之前的文章,分析了非懒加载的单例Bean整个加载过程,除了非懒加载的单例Bean之外,Spring中还有一种Bean就是原型(Prototype)的Bean,看一下定义方式: 1 ...
- Mybatis框架(9)---Mybatis自定义插件生成雪花ID做为表主键项目
Mybatis自定义插件生成雪花ID做为主键项目 先附上项目项目GitHub地址 spring-boot-mybatis-interceptor 有关Mybatis雪花ID主键插件前面写了两篇博客作为 ...
- 剑指Offer(二十二):从上往下打印二叉树
剑指Offer(二十二):从上往下打印二叉树 搜索微信公众号:'AI-ming3526'或者'计算机视觉这件小事' 获取更多算法.机器学习干货 csdn:https://blog.csdn.net/b ...
- HTML连载33-背景定位
一.背景定位 同一个标签可以同时设置背景颜色和背景图片,如果颜色和图片同时存在,那么图片会覆盖颜色 1.在CSS中有一个叫做background-position:属性,就是专门用来控制背景图片的位置 ...
- Codeforces 976D
题意略. 思路:构造题. 我们把全部的d[n]+1个点分作3部分来构造. 首先我们把原问题归约成构造d1.dn.和{d2 - d1,d3 - d1,.....,d[n-1] - d1}这样的问题,其中 ...
- Go 面试每天一篇(第 2 天)
下面这段代码输出什么,说明原因. func main() { slice := []int{0,1,2,3} m := make(map[int]*int) for key,val := range ...
- Delphi - cxGrid添加Footer显示
cxGrid - 添加footer显示 1:添加Footer Items 单击cxGrid Customize... ,Summary,Add: 2:添加Footer items数据绑定 选中一条需要 ...
- 一文读懂HashMap
推荐 转载:https://www.jianshu.com/p/ee0de4c99f87
- P2762 太空飞行计划问题 最大权闭合子图
link:https://www.luogu.org/problemnew/show/P2762 题意 承担实验赚钱,但是要花去对应仪器的费用,仪器可能共用.求最大的收益和对应的选择方案. 思路 这道 ...