123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294 |
- using ComponentFactory.Krypton.Toolkit;
- using System;
- using System.Collections.Generic;
- using System.ComponentModel;
- using System.Data;
- using System.Drawing;
- using System.Linq;
- using System.Runtime.InteropServices;
- using System.Text;
- using System.Threading;
- using System.Threading.Tasks;
- using System.Windows.Forms;
- namespace NXWMS.Forms
- {
- public partial class frmLoading : KryptonForm
- {
- /// <summary>
- /// 加载最大.
- /// </summary>
- public int _LoadMax { get; set; } = 1;
- /// <summary>
- /// 当前加载.
- /// </summary>
- private int _CurrenLoad = 0;
- /// <summary>
- /// 加载轮次数
- /// </summary>
- public int _LoadNumber { get; set; } = -1;
- /// <summary>
- /// 当前轮次数
- /// </summary>
- private int _CurrentNumber = 0;
- /// <summary>
- /// 加载最后的消息
- /// </summary>
- private string _Message = "请耐心等待.正在加载中";
- /// <summary>
- ///
- /// </summary>
- /// <param name="num1"></param>
- /// <param name="num2"></param>
- /// <returns></returns>
- public delegate string ExecAction();
- /// <summary>
- ///
- /// </summary>
- /// <param name="pageSize"></param>
- /// <param name="pageIndex"></param>
- /// <returns></returns>
- public delegate string ExecActionPage(int pageSize, int pageIndex);
-
- /// <summary>
- /// 文件加载至数据视图
- /// </summary>
- /// <param name="dataGrid"></param>
- /// <param name="filePath"></param>
- /// <returns></returns>
- public delegate string ExecActionGridLoad(DataGridView dataGrid,string filePath);
- #region 20200907 孙亚龙添加
- /// <summary>
- /// 定义可以传单个主表主键ID的委托。
- /// 用于加载明细表时,也使用当前的加载控件。
- /// </summary>
- /// <param name="mainTableId">某个业务表的主表主键ID</param>
- /// <returns></returns>
- public delegate string ExecActionExt1(int mainTableId);
- /// <summary>
- /// 执行具体的获取明细表信息函数。然后关闭加载控件。
- /// </summary>
- /// <param name="cal">具体的获取明细表信息函数对象名</param>
- /// <param name="mainTableId">某个业务表的主表主键ID</param>
- /// <returns></returns>
- public string EventCalExecExt1(ExecActionExt1 cal, int mainTableId)
- {
- Application.DoEvents();
- string result;
- try
- {
- result = cal(mainTableId);
- }
- catch (Exception ex)
- {
- Close();
- return ex.Message;
- }
- Close();
- return result;
- }
- #endregion
- /// <summary>
- /// 执行回调函数
- /// </summary>
- /// <param name="cal"></param>
- /// <returns></returns>
- public string EventCalExec(ExecAction cal)
- {
- Application.DoEvents();
- string result;
- try
- {
- result = cal();
- }
- catch (Exception ex)
- {
- Close();
- return ex.Message;
- }
- Close();
- return result;
- }
- /// <summary>
- /// 文件加载数据视图 执行回调函数
- /// </summary>
- /// <param name="cal"></param>
- /// <param name="dataGrid"></param>
- /// <param name="filePath"></param>
- /// <returns></returns>
- public string EventCalExec(ExecActionGridLoad cal, DataGridView dataGrid, string filePath)
- {
- Application.DoEvents();
- string result;
- try
- {
- result = cal(dataGrid, filePath);
- }
- catch (Exception ex)
- {
- Close();
- return ex.Message;
- }
- Close();
- return result;
- }
- public static string ExecFun(Func<string> func)
- {
- var frm = new frmLoading();
- frm.Show();
- return frm.Exec(func);
- }
- public string Exec(Func<string> func)
- {
- Application.DoEvents();
- string result;
- try
- {
- result = func();
- }
- catch (Exception ex)
- {
- Close();
- return ex.Message;
- }
- Close();
- return result;
- }
- /// <summary>
- /// 执行带分页回调函数
- /// </summary>
- /// <param name="cal"></param>
- /// <param name="pageSize"></param>
- /// <param name="pageIndex"></param>
- /// <returns></returns>
- public string EventCalExec(ExecActionPage cal, int pageSize, int pageIndex)
- {
- Application.DoEvents();
- string result;
- try
- {
- result = cal(pageSize, pageIndex);
- }
- catch (Exception ex)
- {
- Close();
- return ex.Message;
- }
- Close();
- return result;
- }
- /// <summary>
- ///
- /// </summary>
- /// <param name="cal"></param>
- /// <returns></returns>
- public bool EventCalExecTask(ExecAction cal)
- {
- var task = new Task(() =>
- {
- cal();
- });
- task.Start();
- Task cwt = task.ContinueWith(t =>
- {
- Close();
- });
- task.Wait();
- cwt.Wait();
- return true;
- }
- public string _message
- {
- get
- {
- return _Message;
- }
- set
- {
- lbMessage.Text = value;
- _Message = value;
- }
- }
- public int _loadIntervel
- {
- get
- {
- return _loadIntervel;
- }
- set
- {
- if (value > 0)
- {
- timer.Interval = value;
- }
- else
- {
- timer.Interval = 50;
- }
- }
- }
-
- public frmLoading()
- {
- InitializeComponent();
- Application.DoEvents();
- lbMessage.Text = _Message;
- }
- public frmLoading(Action action)
- {
- InitializeComponent();
- lbMessage.Text = _Message;
- }
- private void timer_Tick(object sender, EventArgs e)
- {
- if (_CurrentNumber >= _LoadNumber && _LoadNumber != -1)
- {
- Close();
- return;
- }
- if (_CurrenLoad < _LoadMax)
- {
- lbMessage.Text = _Message + string.Empty.PadRight(_CurrenLoad, '.');
- }
- else if (_CurrenLoad == _LoadMax)
- {
- lbMessage.Text = _Message + string.Empty.PadRight(_CurrenLoad, '.');
- _CurrentNumber++;
- }
- else
- {
- _CurrenLoad = 1;
- lbMessage.Text = _Message + string.Empty.PadRight(1, '.');
- }
- _CurrenLoad++;
- }
- }
- }
|