using DevExpress.Internal.WinApi.Windows.UI.Notifications; using DevExpress.XtraEditors; using DevExpress.XtraLayout.Utils; using System; using System.Collections.Generic; using System.ComponentModel; using System.Data; using System.Drawing; using System.IO; using System.Linq; using System.Text; using System.Threading.Tasks; using System.Windows.Forms; namespace XdCxRhDW.App.CorTools { public partial class CorToolForm : DevExpress.XtraEditors.XtraForm { static readonly string inifile = Path.Combine(Application.StartupPath, "par.ini"); volatile bool beRunning = false; BindingList gridSource = new BindingList(); public CorToolForm() { InitializeComponent(); } void openfile(object sender) { if (openFileDialog1.ShowDialog() == DialogResult.OK) { if (sender == btnFile1) { btnFile1.Text = openFileDialog1.FileName; } else { btnFile2.Text = openFileDialog1.FileName; } } } /// /// 读取配置 /// void ReadIni() { if (File.Exists(inifile)) { var lines = File.ReadAllLines(inifile); if (lines.Length != 9) { return; } btnFile1.Text = lines[0]; btnFile2.Text = lines[1]; teCount.Text = lines[2]; tefs.Text = lines[3]; teCenter.Text = lines[4]; teRange.Text = lines[5]; teSnr.Text = lines[6]; tePos.Text = lines[7]; teDfRange.Text = lines[8]; } } /// /// 写入配置 /// void WriteIni() { var lines = new List(); lines.Add(btnFile1.Text); lines.Add(btnFile2.Text); lines.Add(teCount.Text); lines.Add(tefs.Text); lines.Add(teCenter.Text); lines.Add(teRange.Text); lines.Add(teSnr.Text); lines.Add(tePos.Text); lines.Add(teDfRange.Text); File.WriteAllLines(inifile, lines); } private void MainForm_Load(object sender, EventArgs e) { this.gridControl1.DataSource = gridSource; ReadIni(); } private void MainForm_FormClosing(object sender, FormClosingEventArgs e) { //if (!DxHelper.MsgBoxHelper.ShowConfirm("是否退出当前程序")) //{ // e.Cancel = true; //} } private bool ValidateFile(string filename) { if (string.IsNullOrWhiteSpace(filename)) { DxHelper.MsgBoxHelper.ShowError($"请选择文件!"); return false; } if (!File.Exists(filename)) { DxHelper.MsgBoxHelper.ShowError($"文件【{filename}】不存在"); return false; } return true; } private async void btnOK_Click(object sender, EventArgs e) { if (btnOK.Text == "停止") { //XcorrUtils.Stop(); return; } //开始计算 //校验文件名称 if (!ValidateFile(btnFile1.Text)) { return; } if (!ValidateFile(btnFile2.Text)) { return; } XcorrStruct xItem = new XcorrStruct(); xItem.file1 = btnFile1.Text; xItem.file2 = btnFile2.Text; try { xItem.smpCount = int.Parse(teCount.Text); xItem.samplingRate = Convert.ToInt32(double.Parse(tefs.Text) * 1e6); xItem.dtCenter = int.Parse(teCenter.Text); xItem.dtRange = int.Parse(teRange.Text); xItem.dfRange = int.Parse(teDfRange.Text); xItem.smpStart = int.Parse(tePos.Text); xItem.snrThreshold = int.Parse(teSnr.Text); } catch { DxHelper.MsgBoxHelper.ShowError($"参数错误"); return; } WriteIni(); btnOK.Text = "停止"; beRunning = true; //if (chkDama.Checked is false) //{ var result = await ExecuteCorAsync(xItem); if (result != null) { gridSource.Add(result); gridView1.FocusedRowHandle = gridSource.Count - 1; } //} //else //{ // foreach (var cafItem in gridSource) // { // if (!beRunning) continue; // xItem.smpStart = (int)cafItem.smpstart; // xItem.smpCount = (int)cafItem.smplen; // var result = await ExecuteCorAsync(xItem); // if (result != null) // { // cafItem.dt = result.dt; // cafItem.df = result.df; // cafItem.snr = result.snr; // cafItem.tm = result.tm; // } // } // gridControl1.RefreshDataSource(); //} btnOK.Text = "计算"; beRunning = false; } private async Task ExecuteCorAsync(XcorrStruct xItem) { XcorrUtils xcorr = new XcorrUtils(); var result = await xcorr.Calc(xItem); //开始计算 if (result.flag == -2) { DxHelper.MsgBoxHelper.ShowError($"内部错误"); btnOK.Text = "计算"; beRunning = false; return null; } else if (result.flag == -1) { DxHelper.MsgBoxHelper.ShowError($"计算所需数据超出文件范围"); btnOK.Text = "计算"; beRunning = false; return null; } else if (result.flag == -3) { btnOK.Text = "计算"; beRunning = false; return null; } return result; } private void ExecuteDamaAsync() { } /// /// 网格右键菜单弹出 /// /// /// private void gridView1_MouseDown(object sender, MouseEventArgs e) { if (e.Button == MouseButtons.Right && !beRunning) { popupMenu1.ShowPopup(Cursor.Position); } } /// /// 清除网格 /// /// /// private void barButtonItem1_ItemClick(object sender, DevExpress.XtraBars.ItemClickEventArgs e) { gridSource.Clear(); } /// /// 文件拖拽 /// /// /// private void btnFile_DragOver(object sender, DragEventArgs e) { e.Effect = e.Data.GetDataPresent(DataFormats.FileDrop) ? DragDropEffects.Copy : DragDropEffects.None; } private void btnFile_DragDrop(object sender, DragEventArgs e) { string[] files = (string[])e.Data.GetData(DataFormats.FileDrop); if (files != null && files.Length != 0) { if (sender == btnFile1) { btnFile1.Text = files[0]; } else if (sender == btnFile2) { btnFile2.Text = files[0]; } } } private void btnFile_ButtonClick(object sender, DevExpress.XtraEditors.Controls.ButtonPressedEventArgs e) { openfile(sender); } private void btnFile_DoubleClick(object sender, EventArgs e) { openfile(sender); } private void barButtonItem2_ItemClick(object sender, DevExpress.XtraBars.ItemClickEventArgs e) { SaveFileDialog dialog = new SaveFileDialog(); dialog.Filter = "csv|*.csv"; if (dialog.ShowDialog() == DialogResult.OK) { gridView1.ExportToCsv(dialog.FileName); } } private async void btnCheck_Click(object sender, EventArgs e) { //if (!ValidateFile(btnFile1.Text)) //{ // return; //} //var dmcResult = await XcorrUtils.DmcCheckAsync(btnFile1.Text, double.Parse(tefs.Text)); //gridSource.Clear(); //foreach (var dmcItem in dmcResult) //{ // gridSource.Add(new CafResult() // { // file1 = btnFile1.Text, // file2 = btnFile2.Text, // smpstart = dmcItem.Start, // smplen = dmcItem.Length // }); //} } private void chkDama_CheckedChanged(object sender, EventArgs e) { //lciBtnCheck.Visibility = chkDama.Checked ? LayoutVisibility.Always : LayoutVisibility.Never; //lciStartPos.Visibility = lciSampleLength.Visibility = chkDama.Checked ? LayoutVisibility.Never : LayoutVisibility.Always; //layoutControl1.BestFit(); } } }