1. 程式人生 > >(七十三)c#Winform自定義控制元件-資源載入窗體

(七十三)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

東西不多,看全部程式碼

  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:[email protected]
  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         }

最後的話

如果你喜歡的話,請到 https://gitee.com/kwwwvagaa/net_winform_custom_control 點個星