各位! 好久不见! 上期本来想着本期给大家讲解“骗人的ProgressBar”的,但由于学业问题,本期来写一个可以自动听写(中 日 法 英 俄 德 拉语)的软件! (本期使用异步编程!!!!)
1.软件界面(Form1)设计


Form2中就3个button 太简洁了
2.代码部分
1 Form2加载的代码
this.FormBorderStyle = FormBorderStyle.None; //设置窗体为无边框样式
OpenFileDialog dialog = new OpenFileDialog();
dialog.Multiselect = false;//该值确定是否可以选择多个文件
dialog.Title = "请选择文件";
dialog.Filter = "listening文件|*.listening";
if (dialog.ShowDialog() == System.Windows.Forms.DialogResult.OK)
{
string path = dialog.FileName;
if (path != "")
{
StreamReader streamReader = new StreamReader(path, Encoding.Default);
string readStr = streamReader.ReadToEnd();
this.WindowState = FormWindowState.Maximized; //最大化窗体
///---设置语音播放的音量
sps.Volume = 100;
///---设置语音播放速率
sps.Rate = 0;
///---读 就 完 了
sps.SpeakAsync(readStr);
}
else
{
MessageBox.Show("文件夹路径不能为空!", "提示", MessageBoxButtons.OKCancel, MessageBoxIcon.Warning);
}
}
看着就简单,只是一个选择文件的Openfile 然后调用朗读 就实现了听写操作
2 继续 暂停 播放 的操作
private void button1_Click(object sender, EventArgs e)
{
sps.Pause();//暂停
}
private void button2_Click(object sender, EventArgs e)
{
sps.Resume()//继续播放
}
private void button3_Click(object sender, EventArgs e)
{
sps.SpeakAsyncCancelAll();
this.Close();//彻底停止
}
3 Form3的写入/更改文件操作
我个人不喜欢file的操作 就喜欢StreamWriter和StreamReader 这个得根据个人喜好来定 我这边展示一下我的代码
try
{
FolderBrowserDialog fbd = new FolderBrowserDialog();
fbd.Description = "请选择文件夹";
if (fbd.ShowDialog() == DialogResult.OK)
{
if (string.IsNullOrEmpty(fbd.SelectedPath))
{
MessageBox.Show("文件夹路径不能为空!", "提示", MessageBoxButtons.OKCancel, MessageBoxIcon.Warning);
return;
}
string path = fbd.SelectedPath;
StreamWriter streamWriter = new StreamWriter(path + @"\新听写.listening");
streamWriter.WriteAsync(richTextBox1.Text);
MessageBox.Show("导出完毕!","丰の提示");
this.Close();
}
}
catch
{
MessageBox.Show("出Bug了哦");
this.Close();
}
导出操作还是大家最喜欢的fbd了
OpenFileDialog dialog = new OpenFileDialog();
dialog.Multiselect = false;//该值确定是否可以选择多个文件
dialog.Title = "请选择文件";
dialog.Filter = "listening文件|*.listening";
if (dialog.ShowDialog() == System.Windows.Forms.DialogResult.OK)
{
string path = dialog.FileName;
if (path != "")
{
StreamReader streamReader = new StreamReader(path, Encoding.Default);
string readStr = streamReader.ReadToEnd();
streamReader.Close();
richTextBox1.Text = readStr;
}
else
{
MessageBox.Show("文件夹路径不能为空!", "提示", MessageBoxButtons.OKCancel, MessageBoxIcon.Warning);
}
写入操作见得太多了 懒得解释
怎么样,这样一个成型的英语听写软件就诞生了!
那么本期就结束了 谢谢大家!