using System; using System.Collections.Generic; using System.ComponentModel; using System.Data; using System.Drawing; using System.Globalization; using System.Linq; using System.Speech.Synthesis; using System.Text; using System.Threading.Tasks; using System.Windows.Forms; using JY.DAL; using JY.Model; namespace JY.Inspection.Frm { public partial class FrmAbnormalVoice : MetroFramework.Forms.MetroForm { private DataTable _dt; private SpeechSynthesizer _speech; public FrmAbnormalVoice() { InitializeComponent(); this.Load += FrmAbnormalVoice_Load; this.FormClosing += FrmAbnormalVoice_FormClosing; btnAdd.Click += BtnAdd_Click; btnEdit.Click += BtnEdit_Click; btnDelete.Click += BtnDelete_Click; grdData.SelectionChanged += GrdData_SelectionChanged; grdData.CellContentClick += GrdData_CellContentClick; } private void FrmAbnormalVoice_Load(object sender, EventArgs e) { try { BindGridStyle(); LoadData(); _speech = new SpeechSynthesizer(); _speech.Volume = 100; //音量 CultureInfo keyboardCulture = InputLanguage.CurrentInputLanguage.Culture; InstalledVoice neededVoice = _speech.GetInstalledVoices(keyboardCulture).FirstOrDefault(); if (neededVoice != null) { _speech.SelectVoice(neededVoice.VoiceInfo.Name); } } catch (Exception ex) { MessageBox.Show(ex.Message); } } private void LoadData() { _dt = SqlHelper.QueryTable("select *,'播放' as Operation from tb_AbnormalVoice"); grdData.DataSource = _dt.DefaultView; } private void BtnAdd_Click(object sender, EventArgs e) { if (string.IsNullOrEmpty(txtCode.Text.Trim())) { txtCode.Focus(); MessageBox.Show("请输入工位编码"); return; } if (string.IsNullOrEmpty(txtRemark.Text.Trim())) { txtRemark.Focus(); MessageBox.Show("请输入播报内容"); return; } try { string sql = $"insert tb_AbnormalVoice(Code,Remark) values('{txtCode.Text.Trim()}','{txtRemark.Text.Trim()}')"; int result = SqlHelper.Execute(sql); if (result > 0) { LoadData(); MessageBox.Show("保存成功!"); } else { MessageBox.Show("保存失败!"); } } catch (Exception ex) { MessageBox.Show("保存失败:" + ex.Message); } } private void BtnEdit_Click(object sender, EventArgs e) { try { string sql = $"update tb_AbnormalVoice set Code='{txtCode.Text.Trim()}',Remark='{txtRemark.Text.Trim()}' where id={grdData.CurrentRow.Cells["ID"].Value}"; int result = SqlHelper.Execute(sql); if (result > 0) { LoadData(); MessageBox.Show("编辑成功!"); } else { MessageBox.Show("编辑失败!"); } } catch (Exception ex) { MessageBox.Show("编辑失败:" + ex.Message); } } private void BtnDelete_Click(object sender, EventArgs e) { if (MessageBox.Show("确认删除当前选中记录吗?", "提示", MessageBoxButtons.YesNo) == DialogResult.No) { return; } try { string sql = $"delete tb_AbnormalVoice where id={grdData.CurrentRow.Cells["ID"].Value}"; int result = SqlHelper.Execute(sql); if (result > 0) { LoadData(); MessageBox.Show("删除成功!"); } else { MessageBox.Show("删除失败!"); } } catch (Exception ex) { MessageBox.Show("删除失败:" + ex.Message); } } private void GrdData_SelectionChanged(object sender, EventArgs e) { DataGridViewRow row = grdData.CurrentRow; txtCode.Text = row.Cells["Code"].Value.ToString(); txtRemark.Text = row.Cells["Remark"].Value.ToString(); } private void GrdData_CellContentClick(object sender, DataGridViewCellEventArgs e) { if (e.ColumnIndex == 3) { DataGridViewRow row = grdData.CurrentRow; if (_speech != null) { _speech.SpeakAsync(row.Cells["Remark"].Value.ToString()); } } } private void FrmAbnormalVoice_FormClosing(object sender, FormClosingEventArgs e) { if (_speech != null) { if (_speech.State == SynthesizerState.Speaking) { _speech.Pause(); _speech.Dispose(); } } } private void BindGridStyle() { grdData.AlternatingRowsDefaultCellStyle.BackColor = System.Drawing.Color.WhiteSmoke;//FromArgb(((int)(((byte)(255)))), ((int)(((byte)(224)))), ((int)(((byte)(192))))); //grdData.SelectionMode = DataGridViewSelectionMode.FullRowSelect; grdData.MultiSelect = false; grdData.AllowUserToAddRows = false; grdData.AutoGenerateColumns = false; DataGridViewTextBoxColumn col1 = new DataGridViewTextBoxColumn(); col1.DefaultCellStyle.Alignment = DataGridViewContentAlignment.MiddleCenter; col1.Name = "ID"; col1.DataPropertyName = col1.Name; col1.HeaderText = "ID"; col1.Width = 50; grdData.Columns.Add(col1); DataGridViewTextBoxColumn col2 = new DataGridViewTextBoxColumn(); col2.DefaultCellStyle.Alignment = DataGridViewContentAlignment.MiddleCenter; col2.Name = "Code"; col2.DataPropertyName = col2.Name; col2.HeaderText = "工位编码"; col2.Width = 90; grdData.Columns.Add(col2); DataGridViewTextBoxColumn col3 = new DataGridViewTextBoxColumn(); col3.DefaultCellStyle.Alignment = DataGridViewContentAlignment.MiddleLeft; col3.Name = "Remark"; col3.DataPropertyName = col3.Name; col3.HeaderText = "播报内容"; col3.AutoSizeMode = DataGridViewAutoSizeColumnMode.Fill; //col2.Width = 200; grdData.Columns.Add(col3); DataGridViewLinkColumn col7 = new DataGridViewLinkColumn(); col7.DefaultCellStyle.Alignment = DataGridViewContentAlignment.MiddleCenter; col7.Name = "Operation"; col7.DataPropertyName = col7.Name; col7.HeaderText = ""; col7.Width = 60; grdData.Columns.Add(col7); } } }