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
{
///
/// 加载最大.
///
public int _LoadMax { get; set; } = 1;
///
/// 当前加载.
///
private int _CurrenLoad = 0;
///
/// 加载轮次数
///
public int _LoadNumber { get; set; } = -1;
///
/// 当前轮次数
///
private int _CurrentNumber = 0;
///
/// 加载最后的消息
///
private string _Message = "请耐心等待.正在加载中";
///
///
///
///
///
///
public delegate string ExecAction();
///
///
///
///
///
///
public delegate string ExecActionPage(int pageSize, int pageIndex);
///
/// 文件加载至数据视图
///
///
///
///
public delegate string ExecActionGridLoad(DataGridView dataGrid,string filePath);
#region 20200907 孙亚龙添加
///
/// 定义可以传单个主表主键ID的委托。
/// 用于加载明细表时,也使用当前的加载控件。
///
/// 某个业务表的主表主键ID
///
public delegate string ExecActionExt1(int mainTableId);
///
/// 执行具体的获取明细表信息函数。然后关闭加载控件。
///
/// 具体的获取明细表信息函数对象名
/// 某个业务表的主表主键ID
///
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
///
/// 执行回调函数
///
///
///
public string EventCalExec(ExecAction cal)
{
Application.DoEvents();
string result;
try
{
result = cal();
}
catch (Exception ex)
{
Close();
return ex.Message;
}
Close();
return result;
}
///
/// 文件加载数据视图 执行回调函数
///
///
///
///
///
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 func)
{
var frm = new frmLoading();
frm.Show();
return frm.Exec(func);
}
public string Exec(Func func)
{
Application.DoEvents();
string result;
try
{
result = func();
}
catch (Exception ex)
{
Close();
return ex.Message;
}
Close();
return result;
}
///
/// 执行带分页回调函数
///
///
///
///
///
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;
}
///
///
///
///
///
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++;
}
}
}