欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页  >  IT编程

(七十三)c#Winform自定义控件-资源加载窗体

程序员文章站 2023-01-04 11:56:26
前提 入行已经7,8年了,一直想做一套漂亮点的自定义控件,于是就有了本系列文章。 GitHub:https://github.com/kwwwvagaa/NetWinformControl 码云:https://gitee.com/kwwwvagaa/net_winform_custom_contr ......

前提

入行已经7,8年了,一直想做一套漂亮点的自定义控件,于是就有了本系列文章。

github:https://github.com/kwwwvagaa/netwinformcontrol

码云:

如果觉得写的还行,请点个 star 支持一下吧

欢迎前来交流探讨: 企鹅群568015492 

麻烦博客下方点个【推荐】,谢谢

nuget

install-package hzh_controls

目录

用处及效果

(七十三)c#Winform自定义控件-资源加载窗体

准备工作

这个用到了基类窗体  (十七)c#winform自定义控件-基类窗体 ,如果不了解可以先移步看一下

开始

添加一个窗体frmloading 继承 frmbase

东西不多,看全部代码

  1 // ***********************************************************************
  2 // assembly         : hzh_controls
  3 // created          : 2019-09-26
  4 //
  5 // ***********************************************************************
  6 // <copyright file="frmloading.cs">
  7 //     copyright by huang zhenghui(黄正辉) all, qq group:568015492 qq:623128629 email:623128629@qq.com
  8 // </copyright>
  9 //
 10 // blog: https://www.cnblogs.com/bfyx
 11 // github:https://github.com/kwwwvagaa/netwinformcontrol
 12 // gitee:https://gitee.com/kwwwvagaa/net_winform_custom_control.git
 13 //
 14 // if you use this code, please keep this note.
 15 // ***********************************************************************
 16 using system;
 17 using system.collections.generic;
 18 using system.componentmodel;
 19 using system.data;
 20 using system.drawing;
 21 using system.linq;
 22 using system.text;
 23 using system.threading;
 24 using system.windows.forms;
 25 
 26 namespace hzh_controls.forms
 27 {
 28     /// <summary>
 29     /// class frmloading.
 30     /// implements the <see cref="hzh_controls.forms.frmbase" />
 31     /// </summary>
 32     /// <seealso cref="hzh_controls.forms.frmbase" />
 33     public partial class frmloading : frmbase
 34     {
 35         /// <summary>
 36         /// the update database worker
 37         /// </summary>
 38         backgroundworker updatedbworker = new backgroundworker();
 39         /// <summary>
 40         /// 获取或设置加载任务
 41         /// </summary>
 42         /// <value>the background work action.</value>
 43         public action backgroundworkaction
 44         {
 45             get;
 46             set;
 47         }
 48         /// <summary>
 49         /// 设置当前执行进度及任务名称,key:任务进度,取值0-100  value:当前任务名称
 50         /// </summary>
 51         /// <value>the current msg.</value>
 52         public keyvaluepair<int, string> currentmsg
 53         {
 54             set
 55             {
 56                 this.updatedbworker.reportprogress(value.key, value.value);
 57             }
 58         }
 59         /// <summary>
 60         /// initializes a new instance of the <see cref="frmloading"/> class.
 61         /// </summary>
 62         public frmloading()
 63         {
 64             initializecomponent();
 65             this.updatedbworker.workerreportsprogress = true;
 66             this.updatedbworker.workersupportscancellation = true;
 67             this.updatedbworker.dowork += new doworkeventhandler(this.backgroundworker1_dowork);
 68             this.updatedbworker.progresschanged += new progresschangedeventhandler(this.backgroundworker1_progresschanged);
 69         }
 70         /// <summary>
 71         /// 设置进度信息,重写此函数可以处理界面信息绑定
 72         /// </summary>
 73         /// <param name="strtext">进度任务名称</param>
 74         /// <param name="intvalue">进度值</param>
 75         protected virtual void bindingprocessmsg(string strtext, int intvalue)
 76         {
 77 
 78         }
 79 
 80         /// <summary>
 81         /// sets the message.
 82         /// </summary>
 83         /// <param name="strtext">the string text.</param>
 84         /// <param name="intvalue">the int value.</param>
 85         private void setmessage(string strtext, int intvalue)
 86         {
 87             if (this.invokerequired)
 88             {
 89                 this.begininvoke(new methodinvoker(delegate() { setmessage(strtext, intvalue); }));
 90             }
 91             else
 92             {
 93                 bindingprocessmsg(strtext, intvalue);
 94             }
 95         }
 96 
 97         /// <summary>
 98         /// handles the load event of the frmloading control.
 99         /// </summary>
100         /// <param name="sender">the source of the event.</param>
101         /// <param name="e">the <see cref="eventargs"/> instance containing the event data.</param>
102         private void frmloading_load(object sender, eventargs e)
103         {
104             if (controlhelper.isdesignmode())
105                 return;
106             this.updatedbworker.runworkerasync();
107         }
108 
109         /// <summary>
110         /// handles the dowork event of the backgroundworker1 control.
111         /// </summary>
112         /// <param name="sender">the source of the event.</param>
113         /// <param name="e">the <see cref="doworkeventargs"/> instance containing the event data.</param>
114         private void backgroundworker1_dowork(object sender, doworkeventargs e)
115         {
116             if (this.backgroundworkaction != null)
117             {
118                 this.backgroundworkaction();
119             }
120             thread.sleep(100);
121             if (base.invokerequired)
122             {
123                 base.begininvoke(new methodinvoker(delegate
124                 {
125                     base.close();
126                 }));
127             }
128             else
129             {
130                 base.close();
131             }
132         }
133 
134         /// <summary>
135         /// handles the progresschanged event of the backgroundworker1 control.
136         /// </summary>
137         /// <param name="sender">the source of the event.</param>
138         /// <param name="e">the <see cref="progresschangedeventargs"/> instance containing the event data.</param>
139         private void backgroundworker1_progresschanged(object sender, progresschangedeventargs e)
140         {
141             setmessage((e.userstate == null) ? "" : e.userstate.tostring(), e.progresspercentage);
142         }
143     }
144 }

 

说明:

backgroundworkaction:加载资源任务函数

currentmsg:当前需要显示的进度信息,key:任务进度,取值0-100  value:当前任务名称

bindingprocessmsg:向界面绑定数据,子类需要重写此函数来实现向界面绑定显示数据

 

示例:

添加一个窗体frmtestloading 继承frmloading

添加一个文本label1显示进度信息文字

添加一个进度条ucprocesslineext1显示进度值

重新bindingprocessmsg绑定信息

1 protected override void bindingprocessmsg(string strtext, int intvalue)
2         {
3             label1.text = strtext;
4             this.ucprocesslineext1.value = intvalue;
5         }

调用

 1  frmtestloading frmloading = new frmtestloading();
 2                     frmloading.backgroundworkaction = delegate()
 3                     {
 4                         try
 5                         {
 6                             frmloading.currentmsg = new keyvaluepair<int, string>(1, "正在初始化配置...");
 7                             thread.sleep(1000);
 8                             frmloading.currentmsg = new keyvaluepair<int, string>(10, "正在加载第一个资源...");
 9                             thread.sleep(1000);
10                             frmloading.currentmsg = new keyvaluepair<int, string>(20, "正在加载第二个资源...");
11                             thread.sleep(1000);
12                             frmloading.currentmsg = new keyvaluepair<int, string>(30, "正在加载第三个资源...");
13                             thread.sleep(1000);
14                             frmloading.currentmsg = new keyvaluepair<int, string>(40, "正在加载第四个资源...");
15                             thread.sleep(1000);
16                             frmloading.currentmsg = new keyvaluepair<int, string>(50, "正在加载第五个资源...");
17                             thread.sleep(1000);
18                             frmloading.currentmsg = new keyvaluepair<int, string>(60, "正在加载第六个资源...");
19                             thread.sleep(1000);
20                             frmloading.currentmsg = new keyvaluepair<int, string>(70, "正在加载第七个资源...");
21                             thread.sleep(1000);
22                             frmloading.currentmsg = new keyvaluepair<int, string>(80, "正在加载第八个资源...");
23                             thread.sleep(1000);
24                             frmloading.currentmsg = new keyvaluepair<int, string>(90, "正在加载第九个资源...");
25                             thread.sleep(1000);
26                             frmloading.currentmsg = new keyvaluepair<int, string>(1000, "数据加载完成...");
27                             thread.sleep(1000);
28                         }
29                         catch (exception ex)
30                         {
31                             messagebox.show("加载资源时出现错误");
32                         }
33                     };
34                     frmloading.showdialog();

 

最后的话

如果你喜欢的话,请到  点个星星吧