官网

http://www.hzhcontrols.com

前提

入行已经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自定义控件-资源加载窗体的更多相关文章

  1. 手撸Spring框架,设计与实现资源加载器,从Spring.xml解析和注册Bean对象

    作者:小傅哥 博客:https://bugstack.cn 沉淀.分享.成长,让自己和他人都能有所收获! 一.前言 你写的代码,能接的住产品加需求吗? 接,是能接的,接几次也行,哪怕就一个类一片的 i ...

  2. Direct2D开发:从资源加载位图

    转载请注明出处:http://www.cnblogs.com/Ray1024 一.概述 Direct2D使用Windows图像处理组件 (WIC) 来加载位图.从文件加载位图的方法很简单,而且网上的教 ...

  3. prelaod场景,用来显示资源加载进度

    phaser.js的源码可以到它在github上的托管里去下载,游戏要用到的图片声音等素材资源请点击这里下载.Phaser的使用非常简单,只需要引入它的主文件,然后在页面中指定一个用来放置canvas ...

  4. 【Android开发学习笔记】【高级】【随笔】插件化——资源加载

    前言 上一节我们针对插件最基本的原理进行了一个简单的demo实现,但是由于插件的Context对象被宿主所接管,因此无法加载插件程序的资源.那么如何解决这个问题捏? 有人提出这样的方案:将apk中的资 ...

  5. Duilib学习笔记《07》— 资源加载

    Duilib的界面表现力能如此丰富,很大程度上得益于贴图描述的简单强大.通过之前的学习及参看相关例子,我们可以发现,在XML布局文件中,不管是窗体背景还是控件,都添加了对应的图片资源以此来美化界面.而 ...

  6. WinForm的延时加载控件概述

    这篇文章主要介绍了WinForm的延时加载控件,很实用的技巧,在C#程序设计中有着比较广泛的应用,需要的朋友可以参考下   本文主要针对WinForm的延迟加载在常用控件的实现做简单的描述.在进行C# ...

  7. Unity3D之Mecanim动画系统学习笔记(十):Mecanim动画的资源加载相关

    资源加载是必备的知识点,这里就说说Mecanim动画的资源如何打包及加载. 注意,Unity4.x和Unity5.x的AssetBundle打包策略不一样,本笔记是基于Unity4.x的AssetBu ...

  8. cocos2d-x Loading界面实现资源加载

    有时候场景中的资源加载过多的话就会引起游戏进入的时候很卡,因为那是边加载边显示.在tests例子里面有一个很好的例子叫做TextureCacheTest,里面讲解了如何写loading. #inclu ...

  9. 【原】从一个bug浅谈YUI3组件的资源加载

    篇前声明:为了不涉及业务细节,篇内信息统一以某游戏,某功能代替 前不久,某游戏准备内测客户端,开发人员测试过程中发现某功能突然不灵了,之前的测试一切ok,没有发现任何异常,第一反应是,游戏内浏览器都是 ...

随机推荐

  1. 去掉input框的数字箭头

    input::-webkit-outer-spin-button,input::-webkit-inner-spin-button { -webkit-appearance: none;}input[ ...

  2. ‎CocosBuilder 学习笔记(1) CCBReader 解析.ccbi文件流程

    1. 简介 CocosBuilder是免费开源的Cocos2d UI编辑器. .ccb文件是CCB项目的原始文件. .ccbi文件是CCB项目发布后的生成的二进制文件.CCBReader可以快速通过该 ...

  3. DOM操作(基础版)

    DOM操作(基础版) DOM是document Object Model的缩写,简称文档对象模型.只要记住这是操作文档的就行了. DOM基础选择器 1.getElementById(id); //获取 ...

  4. 04_枚举类型iota

    iota是枚举类型的关键字,使用iota可以方便快捷的给常量赋值,主要体现在以下几个方面:1.iota常量自动生成器,每个一行加12.iota给常量赋值使用3.iota遇到const重置为04.可以写 ...

  5. Oracle批量更改所有表的字段取值_类型_原字段名

    CREATE PROCEDURE 存储过程名称 is cursor c_tab is select * from user_tab_columns t r_tab user_tab_columns%r ...

  6. P2805 [NOI2009]植物大战僵尸 + 最大权闭合子图 X 拓扑排序

    传送门:https://www.luogu.org/problemnew/show/P2805 题意 有一个n * m的地图,你可以操纵僵尸从地图的右边向左边走,走的一些地方是有能量值的,有些地方会被 ...

  7. 牛客网暑期ACM多校训练营(第三场) A PACM Team 01背包 记录路径

    链接:https://www.nowcoder.com/acm/contest/141/A来源:牛客网 Eddy was a contestant participating in ACM ICPC ...

  8. CF 988C Equal Sums 思维 第九题 map

    Equal Sums time limit per test 2 seconds memory limit per test 256 megabytes input standard input ou ...

  9. 试试 IEnumerable 的另外 6 个小例子

    IEnumerable 接口是 C# 开发过程中非常重要的接口,对于其特性和用法的了解是十分必要的.本文将通过6个小例子,来熟悉一下其简单的用法. <!-- more --> 阅读建议 在 ...

  10. mariadb数据库galera下添加新的服务器节点

    昨天经过各种努力,终于完成了两台服务器集成的搭建,今天再新开一台服务器,在想如何加入呢?网上度娘了很久结果没搜到相关文章:哎,索性直接照着前两台服务配置,在第三台(新服务器)上配置完成后重启maria ...