(七十三)c#Winform自定義控件-資源加載窗體

官網

http://www.hzhcontrols.comhtml

前提

入行已經7,8年了,一直想作一套漂亮點的自定義控件,因而就有了本系列文章。git

GitHub:https://github.com/kwwwvagaa/NetWinformControlgithub

碼雲:https://gitee.com/kwwwvagaa/net_winform_custom_control.gitc#

若是以爲寫的還行,請點個 star 支持一下吧ide

歡迎前來交流探討: 企鵝羣568015492 企鵝羣568015492函數

麻煩博客下方點個【推薦】,謝謝post

NuGet

Install-Package HZH_Controls

目錄

http://www.javashuo.com/article/p-hacmmtru-mw.htmlui

用處及效果

準備工做

這個用到了基類窗體  (十七)c#Winform自定義控件-基類窗體 ,若是不瞭解能夠先移步看一下this

開始

添加一個窗體FrmLoading 繼承 FrmBasespa

東西很少,看所有代碼

  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();

 

最後的話

若是你喜歡的話,請到 https://gitee.com/kwwwvagaa/net_winform_custom_control 點個星星吧

相關文章
相關標籤/搜索