zoukankan      html  css  js  c++  java
  • (七十三)c#Winform自定义控件-资源加载窗体-HZHControls

    官网

    http://www.hzhcontrols.com

    前提

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

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

    码云:https://gitee.com/kwwwvagaa/net_winform_custom_control.git

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

    欢迎前来交流探讨: 企鹅群568015492 企鹅群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: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 点个星星吧

  • 相关阅读:
    转 W P 代码淆混
    Flash网页游戏辅助工具制作简析
    Flash Player安全沙漏规则
    用loader.loadbytes直接load bitmapdata.getpixels产生的bytearray
    游戏及开发人员的认识
    一犯人在执行死刑前三天供出祖传治癌奇方(转)
    AIR教程列表
    养生名言 (春、夏、秋、冬。)
    十天学会单片机和C语言编程
    UI设计规范收藏
  • 原文地址:https://www.cnblogs.com/bfyx/p/11589145.html
Copyright © 2011-2022 走看看