using System; using System.Windows.Forms; using DevExpress.XtraEditors; namespace KepGridEditor { public partial class FrmRectificationProgress : XtraForm { private Action cancelAction; private bool closed = false; private Func getProgressFunc; private Timer timer; private int progressIndex = 0; public FrmRectificationProgress(Func getProgress, Action cancel, string methodName = "IDW井点校正") { InitializeComponent(); getProgressFunc = getProgress; cancelAction = cancel; labelStatus.Text = $"{methodName},正在计算..."; btnCancel.Click += BtnCancel_Click; this.labelProgress.Visible = false; // 增加定时器 if (getProgressFunc != null) { timer = new Timer(); timer.Interval = 200; timer.Tick += Timer_Tick; timer.Start(); } } private void Timer_Tick(object sender, EventArgs e) { if (closed) return; int progress = getProgressFunc?.Invoke() ?? 0; if(progress > progressIndex) progressIndex = progress; if(progressIndex == 0) this.labelProgress.Text = $"..."; else this.labelProgress.Text = $"{progressIndex}%"; if (progress >= 100) { timer.Stop(); Done(); } if(progress == -1) { timer.Stop(); if (closed) return; closed = true; this.Close(); } } protected override void OnShown(EventArgs e) { base.OnShown(e); this.ControlBox = false; this.FormBorderStyle = FormBorderStyle.None; // 纯无边框 this.StartPosition = FormStartPosition.CenterParent; } public void CloseByThread() { if (this.InvokeRequired) this.BeginInvoke((Action)(() => this.DialogResult = DialogResult.OK)); else this.DialogResult = DialogResult.OK; } public void Done() { if (closed) return; closed = true; this.DialogResult = DialogResult.OK; this.Close(); } private void BtnCancel_Click(object sender, EventArgs e) { if (closed) return; closed = true; cancelAction?.Invoke(); this.DialogResult = DialogResult.Cancel; this.Close(); } } }