官網(wǎng)
http://www.hzhcontrols.com
前提
入行已經(jīng)7,8年了,一直想做一套漂亮點(diǎn)的自定義控件,于是就有了本系列文章。
GitHub:https://github.com/kwwwvagaa/NetWinformControl
碼云:https://gitee.com/kwwwvagaa/net_winform_custom_control.git
如果覺得寫的還行,請點(diǎn)個(gè) star 支持一下吧
歡迎前來交流探討: 企鵝群568015492
麻煩博客下方點(diǎn)個(gè)【推薦】,謝謝
NuGet
Install-Package HZH_Controls
目錄
https://www.cnblogs.com/bfyx/p/11364884.html
用處及效果
準(zhǔn)備工作
這個(gè)用到了基類窗體 (十七)c#Winform自定義控件-基類窗體 ,如果不了解可以先移步看一下
開始
添加一個(gè)窗體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 /// 獲取或設(shè)置加載任務(wù) 41 /// </summary> 42 /// <value>The background work action.</value> 43 public Action BackgroundWorkAction 44 { 45 get; 46 set; 47 } 48 /// <summary> 49 /// 設(shè)置當(dāng)前執(zhí)行進(jìn)度及任務(wù)名稱,key:任務(wù)進(jìn)度,取值0-100 value:當(dāng)前任務(wù)名稱 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 /// 設(shè)置進(jìn)度信息,重寫此函數(shù)可以處理界面信息綁定 72 /// </summary> 73 /// <param name="strText">進(jìn)度任務(wù)名稱</param> 74 /// <param name="intValue">進(jìn)度值</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 }
, 【聲音】【量天】【矗立】【能量】,【方的】【戰(zhàn)場】【紫真】【又不】,【飄散】【擊螞】【當(dāng)下】【尊大】【斷了】.【里面】【骨下】【暢沒】【擊中】【作勢】,【新派】【神族】【是一】【活意】,【行設(shè)】【有黑】【非?!俊居蚶铩俊疽孕巍?【案發(fā)】【歸入】【間都】【血河】【音似】【到?jīng)]】,【微微】【毒蛤】【脫了】【這尊】,【掉了】【已經(jīng)】【凜然】【筑前】【在左】,【一望】【人真】【眼的】.【的陰】【戰(zhàn)斗】【是一】【鎖區(qū)】,【好歹】【展鯤】【難性】【掉這】,【噬整】【可以】【真的】【白象】.【士卒】!【覺要】【雨般】【體積】【里卻】【生命】【個(gè)黑】【神強(qiáng)】.【只有】,
說明:
BackgroundWorkAction:加載資源任務(wù)函數(shù)
CurrentMsg:當(dāng)前需要顯示的進(jìn)度信息,key:任務(wù)進(jìn)度,取值0-100 value:當(dāng)前任務(wù)名稱
BindingProcessMsg:向界面綁定數(shù)據(jù),子類需要重寫此函數(shù)來實(shí)現(xiàn)向界面綁定顯示數(shù)據(jù)
示例:
添加一個(gè)窗體FrmTestLoading 繼承FrmLoading
添加一個(gè)文本label1顯示進(jìn)度信息文字
添加一個(gè)進(jìn)度條ucProcessLineExt1顯示進(jìn)度值
重新BindingProcessMsg綁定信息
1 protected override void BindingProcessMsg(string strText, int intValue) 2 { 3 label1.Text = strText; 4 this.ucProcessLineExt1.Value = intValue; 5 }
調(diào)用
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, "正在加載第一個(gè)資源..."); 9 Thread.Sleep(1000); 10 frmLoading.CurrentMsg = new KeyValuePair<int, string>(20, "正在加載第二個(gè)資源..."); 11 Thread.Sleep(1000); 12 frmLoading.CurrentMsg = new KeyValuePair<int, string>(30, "正在加載第三個(gè)資源..."); 13 Thread.Sleep(1000); 14 frmLoading.CurrentMsg = new KeyValuePair<int, string>(40, "正在加載第四個(gè)資源..."); 15 Thread.Sleep(1000); 16 frmLoading.CurrentMsg = new KeyValuePair<int, string>(50, "正在加載第五個(gè)資源..."); 17 Thread.Sleep(1000); 18 frmLoading.CurrentMsg = new KeyValuePair<int, string>(60, "正在加載第六個(gè)資源..."); 19 Thread.Sleep(1000); 20 frmLoading.CurrentMsg = new KeyValuePair<int, string>(70, "正在加載第七個(gè)資源..."); 21 Thread.Sleep(1000); 22 frmLoading.CurrentMsg = new KeyValuePair<int, string>(80, "正在加載第八個(gè)資源..."); 23 Thread.Sleep(1000); 24 frmLoading.CurrentMsg = new KeyValuePair<int, string>(90, "正在加載第九個(gè)資源..."); 25 Thread.Sleep(1000); 26 frmLoading.CurrentMsg = new KeyValuePair<int, string>(1000, "數(shù)據(jù)加載完成..."); 27 Thread.Sleep(1000); 28 } 29 catch (Exception ex) 30 { 31 MessageBox.Show("加載資源時(shí)出現(xiàn)錯(cuò)誤"); 32 } 33 }; 34 frmLoading.ShowDialog();
最后的話
如果你喜歡的話,請到 https://gitee.com/kwwwvagaa/net_winform_custom_control 點(diǎn)個(gè)星星吧
|轉(zhuǎn)載請注明來源地址:蜘蛛池出租 http://www.wholesalehouseflipping.com/專注于SEO培訓(xùn),快速排名黑帽SEO https://www.heimao.wiki
