Main.cs 26 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683
  1. using Microsoft.Office.Interop.Excel;
  2. using System;
  3. using System.Collections.Generic;
  4. using System.ComponentModel;
  5. using System.Data;
  6. using System.Drawing;
  7. using System.Globalization;
  8. using System.IO;
  9. using System.Linq;
  10. using System.Media;
  11. using System.Runtime.InteropServices;
  12. using System.Security.Principal;
  13. using System.Text;
  14. using System.Text.RegularExpressions;
  15. using System.Threading.Tasks;
  16. using System.Windows.Forms;
  17. using UAS_Tools_HY;
  18. using UAS_Tools_HY.Properties;
  19. using UAS_Tools_HY.PublicMethods;
  20. using Application = Microsoft.Office.Interop.Excel.Application;
  21. using DataTable = System.Data.DataTable;
  22. namespace UAS_MES_Tools
  23. {
  24. public partial class Main : Form
  25. {
  26. string _Account, _Password, _UserName;
  27. DataTable dt;
  28. Timer loadTime;
  29. string minTime, maxTime;
  30. string curRule, curRuleLeng;
  31. int ishaveInd;
  32. public Main(string account, string password)
  33. {
  34. _Account = account;
  35. _Password = password;
  36. InitializeComponent();
  37. }
  38. private void Main_Load(object sender, EventArgs e)
  39. {
  40. currUser.Text = "当前账户: " + _Account;
  41. dt = ConnectDB.ExecuteSelect($"select * from employee where em_code = '{_Account}'");
  42. if (dt.Rows.Count > 0)
  43. {
  44. _UserName = dt.Rows[0]["em_name"].ToString();
  45. }
  46. loadTime_Tick(null, null);
  47. loadTime = new Timer();
  48. loadTime.Interval = 1000;
  49. loadTime.Tick += loadTime_Tick;
  50. loadTime.Start();
  51. SN.SelectAll();
  52. SN.Focus();
  53. settingLeftInput1.Text = BaseUtil.GetCacheData("SettingCount").ToString();
  54. settingLeftInput2.Text = BaseUtil.GetCacheData("SettingVolume").ToString();
  55. dSum.Text = BaseUtil.GetCacheData("SettingVolume").ToString();
  56. settingRightInput1.Text = BaseUtil.GetCacheData("SettingNGPath").ToString();
  57. settingRightInput2.Text = BaseUtil.GetCacheData("SettingExportPath").ToString();
  58. QDcheckbox1.Checked = true;
  59. QDdtp2.Value = QDdtp1.Value.AddDays(1);
  60. rulesQuery_Click(null, null);
  61. QDquery_Click(null, null);
  62. }
  63. private void SN_KeyDown(object sender, KeyEventArgs e)
  64. {
  65. if (e.KeyCode != Keys.Enter) return;
  66. if (CheckEnter()) return;
  67. bool checkRes = true;
  68. if (radio1.Checked)
  69. {
  70. dt = ConnectDB.ExecuteSelect($@"SELECT COUNT(1) sum FROM dcrlogexcel WHERE item = '{SN.Text.Trim()}'");
  71. if (dt.Rows[0]["sum"].ToString() == "0")
  72. {
  73. PlaySound("NG");
  74. MessageBox.Show($"{SN.Text.Trim()},没有DCR测试数据", "警告", MessageBoxButtons.OK, MessageBoxIcon.Warning);
  75. checkRes = false;
  76. return;
  77. }
  78. }
  79. if (FormMethods.IsCheckSNRules(curRule, SN.Text.Trim(), curRuleLeng))
  80. {
  81. PlaySound("NG");
  82. MessageBox.Show($"{SN.Text.Trim()},不符合条码规则", "警告", MessageBoxButtons.OK, MessageBoxIcon.Warning);
  83. checkRes = false;
  84. return;
  85. }
  86. ishaveInd = -1;
  87. if (Datas.Rows.Count > 0)
  88. {
  89. foreach (DataGridViewRow item in Datas.Rows)
  90. {
  91. if (SN.Text.Trim() == item.Cells[0].Value.ToString())
  92. {
  93. ishaveInd = Datas.Rows.IndexOf(item);
  94. }
  95. }
  96. }
  97. if (ishaveInd >= 0)
  98. {
  99. int iCou = Convert.ToInt32(Datas.Rows[ishaveInd].Cells[2].Value);
  100. iCou += 1;
  101. Datas.Rows[ishaveInd].Cells[3].Value = currTime.Text;
  102. Datas.Rows[ishaveInd].Cells[6].Value = checkRes ? "OK" : "NG";
  103. PlaySound("OK");
  104. }
  105. else
  106. {
  107. DataGridViewRow row = new DataGridViewRow();
  108. row.CreateCells(Datas);
  109. row.Cells[0].Value = SN.Text.Trim();
  110. row.Cells[1].Value = BoxNo.Text.Trim();
  111. row.Cells[2].Value = 1;
  112. row.Cells[3].Value = currTime.Text;
  113. row.Cells[4].Value = _UserName;
  114. row.Cells[5].Value = radio1.Checked ? "是" : "否";
  115. row.Cells[6].Value = checkRes ? "OK" : "NG";
  116. Datas.Rows.Add(row);
  117. PlaySound("OK");
  118. Datas.FirstDisplayedScrollingRowIndex = Datas.Rows.Count - 1;
  119. if (ChangeDetail())
  120. {
  121. SN.Text = "";
  122. BoxNo.Text = "";
  123. dCount.Text = "0";
  124. Datas.Rows.Clear();
  125. BoxNo.Focus();
  126. BoxNo.SelectAll();
  127. }
  128. }
  129. }
  130. private bool ChangeDetail()
  131. {
  132. int inputSum = Convert.ToInt32(dCount.Text);
  133. dCount.Text = (inputSum + 1).ToString();
  134. if(Convert.ToInt32(dCount.Text) == Convert.ToInt32(dSum.Text))
  135. {
  136. foreach (DataGridViewRow item in Datas.Rows)
  137. {
  138. int insertCount = ConnectDB.ExecuteInsert($@"INSERT INTO g_packing_sncheck (work_order,part_no,sn,outbox_no,
  139. count,update_time,update_name,check_id,rule_name,rule_value)
  140. VALUES ( '', '', '{item.Cells[0].Value}', '{item.Cells[1].Value}',
  141. '{item.Cells[2].Value}',sysdate,'{_UserName}',packcheckid_seq.NEXTVAL,'{dRules.Text.Trim()}','{curRule}' )");
  142. }
  143. return true;
  144. }
  145. else
  146. {
  147. return false;
  148. }
  149. }
  150. private void BoxNo_KeyDown(object sender, KeyEventArgs e)
  151. {
  152. if (e.KeyCode != Keys.Enter) return;
  153. if (CheckEnter()) return;
  154. if (Datas.Rows.Count > 0)
  155. {
  156. foreach(DataGridViewRow item in Datas.Rows)
  157. {
  158. item.Cells[1].Value = BoxNo.Text.Trim();
  159. }
  160. }
  161. BoxNo.Enabled = false;
  162. SN.SelectAll();
  163. SN.Focus();
  164. }
  165. private bool CheckEnter()
  166. {
  167. if (string.IsNullOrEmpty(settingLeftInput1.Text))
  168. {
  169. MessageBox.Show("请维护列表记录数", "警告", MessageBoxButtons.OK, MessageBoxIcon.Warning);
  170. return true;
  171. }
  172. if (string.IsNullOrEmpty(settingLeftInput2.Text))
  173. {
  174. MessageBox.Show("请维护箱内容量", "警告", MessageBoxButtons.OK, MessageBoxIcon.Warning);
  175. return true;
  176. }
  177. if (!Regex.IsMatch(dSum.Text, @"^[1-9]\d*$"))
  178. {
  179. MessageBox.Show("箱内容量维护应为正整数", "警告", MessageBoxButtons.OK, MessageBoxIcon.Warning);
  180. return true;
  181. }
  182. if (string.IsNullOrEmpty(dRules.Text))
  183. {
  184. MessageBox.Show("请选择条码规则", "警告", MessageBoxButtons.OK, MessageBoxIcon.Warning);
  185. return true;
  186. }
  187. if (dRules.Enabled)
  188. {
  189. MessageBox.Show("请选择确认条码规则", "警告", MessageBoxButtons.OK, MessageBoxIcon.Warning);
  190. return true;
  191. }
  192. return false;
  193. }
  194. private void settingLeftSubmit_Click(object sender, EventArgs e)
  195. {
  196. if (string.IsNullOrEmpty(settingLeftInput1.Text))
  197. {
  198. MessageBox.Show("请输入列表记录数", "警告", MessageBoxButtons.OK, MessageBoxIcon.Warning);
  199. return;
  200. }
  201. if (string.IsNullOrEmpty(settingLeftInput2.Text))
  202. {
  203. MessageBox.Show("请输入箱内容量", "警告", MessageBoxButtons.OK, MessageBoxIcon.Warning);
  204. return;
  205. }
  206. BaseUtil.SetCacheData("SettingCount", settingLeftInput1.Text);
  207. BaseUtil.SetCacheData("SettingVolume", settingLeftInput2.Text);
  208. dSum.Text = BaseUtil.GetCacheData("SettingVolume").ToString();
  209. MessageBox.Show("已提交扫描设置", "提示", MessageBoxButtons.OK, MessageBoxIcon.Information);
  210. }
  211. private void settingRightSubmit_Click(object sender, EventArgs e)
  212. {
  213. if (string.IsNullOrEmpty(settingRightInput2.Text))
  214. {
  215. MessageBox.Show("请选择导出文件目录", "警告", MessageBoxButtons.OK, MessageBoxIcon.Warning);
  216. return;
  217. }
  218. if(!string.IsNullOrEmpty(settingRightInput1.Text)) BaseUtil.SetCacheData("SettingNGPath", settingRightInput1.Text);
  219. BaseUtil.SetCacheData("SettingExportPath", settingRightInput2.Text);
  220. MessageBox.Show("已提交文件设置", "提示", MessageBoxButtons.OK, MessageBoxIcon.Information);
  221. }
  222. private void QDquery_Click(object sender, EventArgs e)
  223. {
  224. StringBuilder filterStr = new StringBuilder();
  225. if (!string.IsNullOrEmpty(QDInput1.Text))
  226. {
  227. filterStr.Append($"WHERE instr(SN,'{QDInput1.Text}') > 0");
  228. }
  229. if (!string.IsNullOrEmpty(QDInput2.Text))
  230. {
  231. if (filterStr.Length == 0)
  232. {
  233. filterStr.Append($"WHERE instr(OUTBOX_NO,'{QDInput2.Text}') > 0");
  234. }
  235. else
  236. {
  237. filterStr.Append($" and instr(OUTBOX_NO, '{QDInput2.Text}') > 0");
  238. }
  239. }
  240. if (QDcheckbox1.Checked)
  241. {
  242. string dtp1 = QDdtp1.Value.ToString("yyyy-MM-dd 00:00:00");
  243. string dtp2 = QDdtp2.Value.ToString("yyyy-MM-dd 00:00:00");
  244. if (filterStr.Length == 0)
  245. {
  246. filterStr.Append($"WHERE update_time > TO_DATE('{dtp1}','YYYY-MM-DD HH24:MI:SS') AND update_time <= TO_DATE('{dtp2}','YYYY-MM-DD HH24:MI:SS')");
  247. }
  248. else
  249. {
  250. filterStr.Append($" AND update_time > TO_DATE('{dtp1}','YYYY-MM-DD HH24:MI:SS') AND update_time <= TO_DATE('{dtp2}','YYYY-MM-DD HH24:MI:SS')");
  251. }
  252. }
  253. dt = ConnectDB.ExecuteSelect($@"SELECT sn dqsn,outbox_no dqoutbox_no,count dqcount,
  254. update_time dqupdate_time,update_name dqname,rule_name dqrule_name,rule_value dqrule_value
  255. FROM g_packing_sncheck {filterStr.ToString()} ORDER BY sn,update_time desc");
  256. QDDatas.DataSource = dt;
  257. }
  258. private void QDexport_Click(object sender, EventArgs e)
  259. {
  260. if(QDDatas.Rows.Count == 0)
  261. {
  262. MessageBox.Show("请先查询出导出数据", "警告", MessageBoxButtons.OK, MessageBoxIcon.Warning);
  263. return;
  264. }
  265. if (string.IsNullOrEmpty(settingRightInput2.Text))
  266. {
  267. MessageBox.Show("请先在设置中选择导出路径", "警告", MessageBoxButtons.OK, MessageBoxIcon.Warning);
  268. return;
  269. }
  270. Application excelApp = null;
  271. Workbook workbook = null;
  272. Worksheet worksheet = null;
  273. Range range = null;
  274. string tempTemplatePath = null;
  275. try
  276. {
  277. string exportPath = Path.Combine(settingRightInput2.Text, DateTime.Now.ToString("yyyy-MM-dd HH-mm-ss") + ".xlsx");
  278. if (string.IsNullOrEmpty(settingRightInput2.Text) || !Directory.Exists(settingRightInput2.Text))
  279. {
  280. MessageBox.Show($"输出目录不存在: {settingRightInput2.Text}", "警告", MessageBoxButtons.OK, MessageBoxIcon.Warning);
  281. return;
  282. }
  283. byte[] templateBytes = Resources.exportModel; // 假设Resources.exportModel是有效的资源
  284. if (templateBytes == null || templateBytes.Length == 0)
  285. {
  286. MessageBox.Show("未能加载模板文件", "警告", MessageBoxButtons.OK, MessageBoxIcon.Warning);
  287. return;
  288. }
  289. tempTemplatePath = Path.GetTempFileName().Replace(".tmp", ".xlsx");
  290. File.WriteAllBytes(tempTemplatePath, templateBytes);
  291. excelApp = new Application();
  292. excelApp.Visible = false;
  293. excelApp.DisplayAlerts = false;
  294. workbook = excelApp.Workbooks.Open(tempTemplatePath);
  295. object sheetIndex = 1;
  296. worksheet = (Worksheet)workbook.Worksheets[sheetIndex];
  297. int startRow = 2;
  298. int dataRowsCount = QDDatas.Rows.Count;
  299. for (int i = 0; i < QDDatas.Rows.Count; i++)
  300. {
  301. worksheet.Cells[startRow + i, 1] = i + 1; // 改为从1开始编号,更符合Excel习惯
  302. worksheet.Cells[startRow + i, 2] = QDDatas.Rows[i].Cells[0].Value?.ToString();
  303. }
  304. int endRow = startRow + dataRowsCount - 1;
  305. range = worksheet.Range[worksheet.Cells[startRow, 1], worksheet.Cells[endRow, 2]];
  306. range.HorizontalAlignment = XlHAlign.xlHAlignCenter; // 水平居中
  307. range.VerticalAlignment = XlVAlign.xlVAlignCenter; // 垂直居中
  308. worksheet.Columns.AutoFit();
  309. // 修复SaveAs方法,使用正确的参数
  310. workbook.SaveAs(
  311. Filename: exportPath,
  312. FileFormat: XlFileFormat.xlOpenXMLWorkbook,
  313. Password: Type.Missing,
  314. WriteResPassword: Type.Missing,
  315. ReadOnlyRecommended: Type.Missing,
  316. CreateBackup: Type.Missing,
  317. AccessMode: XlSaveAsAccessMode.xlExclusive,
  318. ConflictResolution: Type.Missing,
  319. AddToMru: false,
  320. TextCodepage: Type.Missing,
  321. TextVisualLayout: Type.Missing,
  322. Local: Type.Missing
  323. );
  324. MessageBox.Show($"Excel文件已成功导出到: {exportPath}", "成功", MessageBoxButtons.OK, MessageBoxIcon.Information);
  325. }
  326. catch (Exception ex)
  327. {
  328. MessageBox.Show($"生成 Excel 文件时出错: {ex.Message}", "警告", MessageBoxButtons.OK, MessageBoxIcon.Warning);
  329. }
  330. finally
  331. {
  332. // 正确释放COM对象
  333. if (worksheet != null)
  334. {
  335. try
  336. {
  337. Marshal.ReleaseComObject(worksheet);
  338. }
  339. catch { }
  340. worksheet = null;
  341. }
  342. if (workbook != null)
  343. {
  344. try
  345. {
  346. workbook.Close(false);
  347. Marshal.ReleaseComObject(workbook);
  348. }
  349. catch { }
  350. workbook = null;
  351. }
  352. if (excelApp != null)
  353. {
  354. try
  355. {
  356. excelApp.Quit();
  357. Marshal.ReleaseComObject(excelApp);
  358. }
  359. catch { }
  360. excelApp = null;
  361. }
  362. if (!string.IsNullOrEmpty(tempTemplatePath) && File.Exists(tempTemplatePath))
  363. {
  364. try
  365. {
  366. File.Delete(tempTemplatePath);
  367. }
  368. catch (Exception ex)
  369. {
  370. }
  371. }
  372. GC.Collect();
  373. GC.WaitForPendingFinalizers();
  374. }
  375. }
  376. private void rulesAdd_Click(object sender, EventArgs e)
  377. {
  378. StringBuilder filterStr = new StringBuilder();
  379. if (!string.IsNullOrEmpty(rulesInput1.Text))
  380. {
  381. filterStr.Append($"WHERE instr(RULE_NAME,'{rulesInput1.Text}') > 0");
  382. }
  383. if (!string.IsNullOrEmpty(rulesInput2.Text))
  384. {
  385. if(filterStr.Length == 0)
  386. {
  387. filterStr.Append($"WHERE instr(RULE_VALUE,'{rulesInput2.Text}') > 0");
  388. }
  389. else
  390. {
  391. filterStr.Append($" and instr(RULE_VALUE, '{rulesInput2.Text}') > 0");
  392. }
  393. }
  394. Form rulesList = new RulesList("Add",_UserName);
  395. if (rulesList.ShowDialog() == DialogResult.OK)
  396. {
  397. MessageBox.Show("新增规则成功", "提示", MessageBoxButtons.OK, MessageBoxIcon.Information);
  398. dt = ConnectDB.ExecuteSelect($@"SELECT RULE_ID ruleId,RULE_NAME RulesCol1, RULE_VALUE RulesCol2, RELE_REMARK RulesCol3,
  399. RULE_LENGTH RulesCol4,to_char(UPDATE_TIME, 'YYYY-MM-DD HH:Mi:SS') RulesCol5,UPDATE_USER RulesCol6 FROM g_packing_rules {filterStr.ToString()} order by UPDATE_TIME desc");
  400. RulesDatas.DataSource = dt;
  401. }
  402. }
  403. private void rulesQuery_Click(object sender, EventArgs e)
  404. {
  405. StringBuilder filterStr = new StringBuilder();
  406. if (!string.IsNullOrEmpty(rulesInput1.Text))
  407. {
  408. filterStr.Append($"WHERE instr(RULE_NAME,'{rulesInput1.Text}') > 0");
  409. }
  410. if (!string.IsNullOrEmpty(rulesInput2.Text))
  411. {
  412. if (filterStr.Length == 0)
  413. {
  414. filterStr.Append($"WHERE instr(RULE_VALUE,'{rulesInput2.Text}') > 0");
  415. }
  416. else
  417. {
  418. filterStr.Append($" and instr(RULE_VALUE, '{rulesInput2.Text}') > 0");
  419. }
  420. }
  421. dt = ConnectDB.ExecuteSelect($@"SELECT RULE_ID ruleId,RULE_NAME RulesCol1, RULE_VALUE RulesCol2, RELE_REMARK RulesCol3,
  422. RULE_LENGTH RulesCol4,to_char(UPDATE_TIME, 'YYYY-MM-DD HH:Mi:SS') RulesCol5,UPDATE_USER RulesCol6 FROM g_packing_rules {filterStr.ToString()} order by UPDATE_TIME desc");
  423. RulesDatas.DataSource = dt;
  424. }
  425. private void RulesDatas_CellMouseDoubleClick(object sender, DataGridViewCellMouseEventArgs e)
  426. {
  427. if (e.RowIndex >= 0 && !RulesDatas.Rows[e.RowIndex].IsNewRow)
  428. {
  429. DataGridView dgv = sender as DataGridView;
  430. if (dgv != null)
  431. {
  432. DataGridViewRow selectedRow = dgv.Rows[e.RowIndex];
  433. string RULE_ID = selectedRow.Cells[0].Value.ToString();
  434. StringBuilder filterStr = new StringBuilder();
  435. if (!string.IsNullOrEmpty(rulesInput1.Text))
  436. {
  437. filterStr.Append($"WHERE instr(RULE_NAME,'{rulesInput1.Text}') > 0");
  438. }
  439. if (!string.IsNullOrEmpty(rulesInput2.Text))
  440. {
  441. if (filterStr.Length == 0)
  442. {
  443. filterStr.Append($"WHERE instr(RULE_VALUE,'{rulesInput2.Text}') > 0");
  444. }
  445. else
  446. {
  447. filterStr.Append($" and instr(RULE_VALUE, '{rulesInput2.Text}') > 0");
  448. }
  449. }
  450. Form rulesList = new RulesList("Modify",RULE_ID, _UserName);
  451. if (rulesList.ShowDialog() == DialogResult.OK)
  452. {
  453. MessageBox.Show("修改规则成功", "提示", MessageBoxButtons.OK, MessageBoxIcon.Information);
  454. dt = ConnectDB.ExecuteSelect($@"SELECT RULE_ID ruleId,RULE_NAME RulesCol1, RULE_VALUE RulesCol2, RELE_REMARK RulesCol3,
  455. RULE_LENGTH RulesCol4,to_char(UPDATE_TIME, 'YYYY-MM-DD HH:Mi:SS') RulesCol5,UPDATE_USER RulesCol6 FROM g_packing_rules {filterStr.ToString()} order by UPDATE_TIME desc");
  456. RulesDatas.DataSource = dt;
  457. }
  458. }
  459. }
  460. }
  461. private void RulesDatas_CellMouseClick(object sender, DataGridViewCellMouseEventArgs e)
  462. {
  463. if (e.RowIndex >= 0 && !RulesDatas.Rows[e.RowIndex].IsNewRow)
  464. {
  465. DataGridView dgv = sender as DataGridView;
  466. if (dgv != null)
  467. {
  468. DataGridViewRow selectedRow = dgv.Rows[e.RowIndex];
  469. if (dRules.Enabled)
  470. {
  471. dRules.Text = selectedRow.Cells[1].Value.ToString();
  472. curRule = selectedRow.Cells[2].Value.ToString();
  473. curRuleLeng = selectedRow.Cells[4].Value.ToString();
  474. }
  475. }
  476. }
  477. }
  478. private void boxBtn_Click(object sender, EventArgs e)
  479. {
  480. BoxNo.Enabled = true;
  481. BoxNo.Focus();
  482. BoxNo.SelectAll();
  483. }
  484. private void settingRightInput1_Click(object sender, EventArgs e)
  485. {
  486. using (OpenFileDialog openFileDialog = new OpenFileDialog())
  487. {
  488. openFileDialog.Title = "请选择告警文件";
  489. openFileDialog.Filter = "可执行文件 (*.wav)|*.wav|所有文件 (*.*)|*.*";
  490. openFileDialog.FilterIndex = 1;
  491. openFileDialog.RestoreDirectory = true;
  492. if (!string.IsNullOrEmpty(settingRightInput1.Text) && System.IO.File.Exists(settingRightInput1.Text))
  493. {
  494. openFileDialog.InitialDirectory = System.IO.Path.GetDirectoryName(settingRightInput1.Text);
  495. }
  496. else
  497. {
  498. openFileDialog.InitialDirectory = Environment.GetFolderPath(Environment.SpecialFolder.ProgramFiles);
  499. }
  500. if (openFileDialog.ShowDialog() == DialogResult.OK)
  501. {
  502. settingRightInput1.Text = openFileDialog.FileName;
  503. }
  504. }
  505. }
  506. private void settingRightInput2_Click(object sender, EventArgs e)
  507. {
  508. using (FolderBrowserDialog folderBrowserDialog = new FolderBrowserDialog())
  509. {
  510. folderBrowserDialog.Description = "请选择导出文件夹";
  511. if (!string.IsNullOrEmpty(settingRightInput2.Text) && System.IO.Directory.Exists(settingRightInput2.Text))
  512. {
  513. folderBrowserDialog.SelectedPath = settingRightInput2.Text;
  514. }
  515. else
  516. {
  517. folderBrowserDialog.SelectedPath = Environment.GetFolderPath(Environment.SpecialFolder.MyDocuments); // folderBrowserDialog.SelectedPath = "";
  518. }
  519. if (folderBrowserDialog.ShowDialog() == DialogResult.OK)
  520. {
  521. settingRightInput2.Text = folderBrowserDialog.SelectedPath;
  522. }
  523. }
  524. }
  525. private void QDdtp1_ValueChanged(object sender, EventArgs e)
  526. {
  527. if (QDdtp1.Value > QDdtp2.Value)
  528. {
  529. QDdtp1.Value = QDdtp2.Value.AddDays(-1);
  530. MessageBox.Show("时间不得大于最大时间", "警告", MessageBoxButtons.OK, MessageBoxIcon.Warning);
  531. return;
  532. }
  533. minTime = QDdtp1.Value.ToString("yyyy-mm-dd hh:mm:ss");
  534. }
  535. private void DRulesBtn_Click(object sender, EventArgs e)
  536. {
  537. if (string.IsNullOrEmpty(dRules.Text))
  538. {
  539. MessageBox.Show("请选择条码规则", "警告", MessageBoxButtons.OK, MessageBoxIcon.Warning);
  540. return;
  541. }
  542. dRules.Enabled = false;
  543. }
  544. private void DRulesBtn1_Click(object sender, EventArgs e)
  545. {
  546. dRules.Enabled = true;
  547. }
  548. private void QDdtp2_ValueChanged(object sender, EventArgs e)
  549. {
  550. if (QDdtp2.Value < QDdtp1.Value)
  551. {
  552. QDdtp2.Value = QDdtp1.Value.AddDays(1);
  553. MessageBox.Show("时间不得小于最小时间", "警告", MessageBoxButtons.OK, MessageBoxIcon.Warning);
  554. return;
  555. }
  556. maxTime = QDdtp2.Value.ToString("yyyy-mm-dd hh:mm:ss");
  557. }
  558. private void PlaySound(string type)
  559. {
  560. if (type == "OK")
  561. {
  562. ShowMsg.Text = "Pass";
  563. ShowMsg.BackColor = Color.LawnGreen;
  564. }
  565. else
  566. {
  567. ShowMsg.Text = "Fail";
  568. ShowMsg.BackColor = Color.Red;
  569. }
  570. Task.Run(() => {
  571. if (type == "OK")
  572. {
  573. using (var stream = UAS_Tools_HY.Properties.Resources.OK)
  574. {
  575. SoundPlayer player = new SoundPlayer(stream);
  576. player.Play();
  577. }
  578. }
  579. else
  580. {
  581. if (string.IsNullOrEmpty(settingRightInput1.Text))
  582. {
  583. using (var stream = UAS_Tools_HY.Properties.Resources.NG)
  584. {
  585. SoundPlayer player = new SoundPlayer(stream);
  586. player.Play();
  587. }
  588. }
  589. else
  590. {
  591. string customPath = settingRightInput1.Text.Trim();
  592. if (File.Exists(customPath))
  593. {
  594. SoundPlayer player = new SoundPlayer(customPath);
  595. player.Play();
  596. }
  597. }
  598. }
  599. });
  600. }
  601. public void loadTime_Tick(object sender, EventArgs e)
  602. {
  603. string currentDate = DateTime.Now.ToString("yyyy-MM-dd hh:mm:ss");
  604. currTime.Text = currentDate;
  605. }
  606. }
  607. }