/// <summary> /// 转换选项改变事件 /// </summary> /// <param name="sender"></param> /// <param name="e"></param> private void rbConvertOption_CheckedChanged(object sender, EventArgs e) { RadioButton radioButton = (RadioButton)sender; if (radioButton.Checked) { ConvertOption = (enumConvertOption)Convert.ToInt32(radioButton.Tag); } }
/// <summary> /// 字符串转为繁体字 /// </summary> /// <param name="str">简体中文字符串</param> /// <returns>繁体中文字符串</returns> public string StringToTraditional(string str, enumConvertOption convertOption = enumConvertOption.OldWord) { try { switch (convertOption) { case enumConvertOption.Quick: return(Microsoft.VisualBasic.Strings.StrConv(str, Microsoft.VisualBasic.VbStrConv.TraditionalChinese, 0)); case enumConvertOption.OldWord: return(ChineseConvert.ToTraditional(str)); case enumConvertOption.High: return(HighConvert.Chs2Cht(str)); default: return(""); } } catch (Exception) { return(""); } }
/// <summary> /// 开始转换按钮 /// </summary> /// <param name="sender"></param> /// <param name="e"></param> private void btnStartConvert_Click(object sender, EventArgs e) { string path = txtPath.Text.Trim(); string format = cbFormat.Text.Trim(); string encode = cbEncode.Text.Trim(); string nameStyle = txtFileName.Text.Trim(); enumConvertOption convertOption = ConvertOption; int fileLength = listViewFile.Items.Count; if (fileLength <= 0) { MessageBox.Show("无可转换的文件不存在,请先打开字幕文件夹。", "无法转换", MessageBoxButtons.OK, MessageBoxIcon.Error); return; } else if (CheckFileStyle(nameStyle, out string err) == false) { MessageBox.Show(err, "文件名样式错误", MessageBoxButtons.OK, MessageBoxIcon.Error); return; } else if (CheckListViewItemSame(listViewFile, 1) == true) { MessageBox.Show("列表中存在文件名重复,请先修改名称。", "文件名冲突", MessageBoxButtons.OK, MessageBoxIcon.Error); return; } else { Dictionary <string, string> dicFile = new Dictionary <string, string>(); foreach (ListViewItem item in listViewFile.Items) { dicFile.Add(item.SubItems[1].Text, item.SubItems[2].Text); } pbConvert.Value = 0; pbConvert.Maximum = dicFile.Count; btnStartConvert.Hide(); int cnt = 0; Stopwatch Watch = new Stopwatch(); Watch.Start(); foreach (KeyValuePair <string, string> file in dicFile) { Task.Factory.StartNew(() => { string newFilePath = Path.GetDirectoryName(file.Value) + "//" + file.Key; Console.WriteLine(format + "\t" + newFilePath); if (format == "转为简体") { SaveFile(newFilePath, StringToSimlified(ReadFile(file.Value), convertOption), encode); } else { SaveFile(newFilePath, StringToTraditional(ReadFile(file.Value), convertOption), encode); } this.Invoke(new Action(() => { cnt = cnt + 1; pbConvert.Value = cnt; if (cnt >= fileLength) { Watch.Stop(); MessageBox.Show("转换完成,共输出" + pbConvert.Value + "个字幕文件,耗时" + string.Format("{0:0.###}", Watch.Elapsed.TotalSeconds) + "秒。", "转换完成", MessageBoxButtons.OK, MessageBoxIcon.Information); pbConvert.Value = 0; btnStartConvert.Show(); listViewFile.Items.Clear(); txtPath.Clear(); } })); }); } } }