(七十三)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,没有发现任何异常,第一反应是,游戏内浏览器都是 ...
随机推荐
- ID转名称到手方案01
> 好久没有写技术文章了,那就重新捡起来,从今天开始,分享这段时间的收获吧 ------------ > ## 其实很多时候,我们只需要鱼,而不是渔,呐,给你鱼. ### 这次的分享主题是 ...
- 使用Sigar做后台服务器管理时,遇到的linux上的问题
首先是线下猛如虎,线上惨不忍赌........ 问题的出处是: function change() { /*获取cpu*/ $.ajax({ url: "http://localhost:8 ...
- 持续集成高级篇之Jekins脚本参数化构建
系列目录 本系列已经很久没有更新了,接前面基础篇,本系统主要介绍jenkins构建里的一些高级特性.包括脚本参数化,Jenkins Pipeline与及在PipeLine模式下如何执行常见的传统构建任 ...
- spring、spring mvc与spring boot的区别是什么?
Spring 的功能 Spring 框架就像一个家族,有众多衍生产品例如 boot.security.jpa等等.但他们的基础都是Spring 的 ioc和 aop ioc 提供了依赖注入的容器 ao ...
- Linux之Shell编程(14)
变量: 定义变量的规则: 1)变量名可以由字母.数字和下划线组成,但不能以数字开头 2)等号两侧不能有空格 3)变量名一般习惯大写 将命令的返回值赋值给变量: 1)使用``将命令括起来 2)使用$() ...
- MyBatis 封装Map,返回不同实体的集合对象
现在有一个需求,就是从100个表中获得任意表中的数据,按照正常的思维模式和处理方式, 我们首先会创建100个实体类(累死!),然后通过resultType一一对应实体类,这种方式简直... 那么我们 ...
- python小白短期基础入门
一.编程语言介绍与分类 1.什么是编程语言 编程语言本质就是人类的语言,主要用于沟通交流.我们通过编程语言与计算机进行互动交流,从而使计算机来帮助我们实现一些特定的功能和一些复杂的工作. 2.编程语言 ...
- java实现截取PDF指定页并进行图片格式转换
1.引入依赖 <dependency> <groupId>org.apache.pdfbox</groupId> <artifactId>pdfbox& ...
- 提升RabbitMQ消费速度的一些实践
RabbitMQ是一个开源的消息中间件,自带管理界面友好.开发语言支持广泛.没有对其它中间件的依赖,而且社区非常活跃,特别适合中小型企业拿来就用.这篇文章主要探讨提升RabbitMQ消费速度的一些方法 ...
- spring-cloud-kubernetes与k8s的configmap
本文是<spring-cloud-kubernetes实战系列>的第六篇,主要内容是在kubernetes上部署一个java web应用,该应用使用了spring-cloud-kubern ...