// 翻译 private string Translate(bool sendToWindow) { string from = "auto", dst = ""; try { if (textBox1_Source.Text == "") { return(""); } if (comboBox2_DestLang.SelectedItem.ToString() != "不翻译") { if (tranInterface.IndexOf("百度") != -1) { Baidu.Translation(textBox1_Source.Text, from, Baidu_to[comboBox2_DestLang.SelectedIndex], out string src, out dst); } else if (tranInterface.IndexOf("有道") != -1) { dst = Youdao.Translation(textBox1_Source.Text, from, Youdao_to[comboBox2_DestLang.SelectedIndex]); } else if (tranInterface.IndexOf("腾讯") != -1) { dst = Tengxun.Translation(textBox1_Source.Text, from, Tengxun_to[comboBox2_DestLang.SelectedIndex]); } } else { dst = textBox1_Source.Text; } // 保存当前选项 FrmMain.TranDestLang = comboBox2_DestLang.SelectedIndex; FrmMain.AutoPressKey = comboBox1_SourceLang.SelectedIndex; FrmMain.AutoSend = checkBox1_AutoSend.Checked; if (!sendToWindow) // 不将译文发送到窗口 { return(dst); } else { this.WindowState = FormWindowState.Minimized; SendText(dst); } } catch (Exception ex) { MessageBox.Show(ex.Message, "翻译", MessageBoxButtons.OK, MessageBoxIcon.Error); } finally { if (sendToWindow) // 如果是不将译文发送到窗口,则不会关闭本窗口 { this.Close(); } } return(dst); }
private string TranslationResultDisplay(string str) { var youdao = new Youdao(); string transResultJson = youdao.translator(str); dynamic transResult = JToken.Parse(transResultJson) as dynamic; var errotCode = transResult?.errorCode; if (errotCode == "108" && i < 5) { TranslationResultDisplay(str); i++; return("{}"); } if (errotCode == "401") { Idkey.idkdy(); } i = 0; Mvvm.StrI = str; // 将翻译结果写入 transResult.json 文件 #if LOG Loger.json(transResult); #endif string detailsStr = transResult?.translation?[0] +Environment.NewLine; if (transResult?.basic != null) { detailsStr += "----------------" + Environment.NewLine; foreach (var strr in transResult?.basic?.explains) { detailsStr += strr + Environment.NewLine; } } if (transResult?.web != null) { detailsStr += "----------------" + Environment.NewLine; foreach (var element in transResult.web) { detailsStr += element.key + Environment.NewLine; if (element?.value != null) { foreach (var strr in element.value) { detailsStr += " " + strr + Environment.NewLine; } } } } try { string s = transResult?.translation?[0]; string z = detailsStr.Substring(0, detailsStr.Length - 2); Mvvm.StrQ = s.Replace("\n", ""); Mvvm.StrO = z; return(z); } catch (Exception) { } return(""); }
private void button_YoudaoKeyTest_Click(object sender, EventArgs e) { if (TextBoxIsEmpty(groupBox2))// 先判断是否有 没有填的项 { return; } // 先保存数据 // 有道翻译 InitDictionary(changeYoudaoKey: true); if (!Youdao.YoudaoKeyTest()) { return; } try { ConfigFile.WriteFile(config); MessageBox.Show("测试成功!", this.Text, MessageBoxButtons.OK, MessageBoxIcon.Information); } catch (Exception ex) { MessageBox.Show("保存到文件失败!\n" + ex.Message, this.Text, MessageBoxButtons.OK, MessageBoxIcon.Error); } }
private void YoudaoTranslation() { chkYoudao.Checked = false; chkYoudao.Enabled = false; textYoudao.Text = Youdao.Translation(textOriginal.Text, langDic["必应"][cobForm.Text], langDic["必应"][cobTo.Text]); chkYoudao.Enabled = true; chkYoudao.Checked = true; }
//private DialogResult ShowScreenShotDialog(out Image img) //{ // if (shot != null && !shot.IsDisposed) // shot.Dispose(); // shot = new FrmScreenShot(); // if (shot.Start() == DialogResult.Cancel) // 显示截图窗口 // { // img = null; // return DialogResult.Cancel; // } // else // { // img = (Image)shot.CaptureImage?.Clone(); // return DialogResult.OK; // } //} // 调用翻译Api翻译后显示翻译结果 private async void TranslateAndShowResult(Image captureImage) { string src = "", dst = ""; await Task.Run(() => { try { if (tranInterface.IndexOf("百度") != -1) { Baidu.PictureTranslation(captureImage, "auto", Baidu.LangDict[destLang], out src, out dst); } else if (tranInterface.IndexOf("有道") != -1) { Youdao.PictureTranslation(captureImage, "auto", Youdao.LangDict[destLang], out src, out dst); } else if (tranInterface.IndexOf("腾讯") != -1) { Tengxun.PictureTranslation(captureImage, "auto", Tengxun.LangDict[destLang], out src, out dst); } } catch (Exception ex) { dst = ("错误:" + ex.Message); } }); if (copySourceTextToClip) { Clipboard.SetText(src);// 复制原文到剪切板 } if (copyDestTextToClip) { Clipboard.SetText(dst);// 复制译文到剪切板 } if (isSpeak) { Speech(dst); // 文字转语音 } this.Invoke(new Action(() => ShowText(dst))); // 显示到桌面 }
private void button_YoudaoKeyTest_Click(object sender, EventArgs e) { if (TextBoxIsEmpty(groupBox2))// 先判断是否有 没有填的项 { return; } TextBoxRemoveSpace(groupBox2); // 移除空格 SaveYoudaoKeyToDict(); // 先保存翻译Key if (!Youdao.YoudaoKeyTest()) // 有道翻译 { return; } try { ConfigFile.WriteFile(config); MessageBox.Show("测试成功!", this.Text, MessageBoxButtons.OK, MessageBoxIcon.Information); } catch (Exception ex) { MessageBox.Show("保存到文件失败!\n" + ex.Message, this.Text, MessageBoxButtons.OK, MessageBoxIcon.Error); } }
// 截图翻译 private void ScreenTran() { string from = "en", src, dst = null; Image captureImage = default; FrmScreenShot shot = new FrmScreenShot(); SetStateDelegate setState = new SetStateDelegate(MinimizeWindow); try { // 截图翻译并显示 if (tranMode == TranMode.TranAndShowText) { this.Invoke((EventHandler) delegate { setState(true); }); Thread.Sleep(200); // 如果是固定区域翻译(isFixedScreen = true则视为固定区域翻译) if (isFixedScreen == true) { captureImage = FixedScreen(); } else { shot.CopyScreen(); if (shot.ShowDialog() == DialogResult.Cancel) // 显示截图窗口 { return; } captureImage = shot.CaptureImage; } // 翻译模式isEnToZh=true为英译中,false为俄译中 if (isEnToZh == false) { from = "ru"; } // sourceOfTran有“有道”两字使用有道翻译,否则使用百度翻译 if (sourceOfTran.IndexOf("有道") != -1) { Youdao.YoudaoTran(null, from, "zh-CHS", out src, out dst, captureImage); } else { Baidu.BaiduTran(captureImage, from, out src, out dst); } if (copySourceTextToClip) { Clipboard.SetText(src);// 复制原文到剪切板 } if (copyDestTextToClip) { Clipboard.SetText(dst);// 复制译文到剪切板 } if (isSpeak) { SpeechSynthesizer speech = new SpeechSynthesizer(); speech.Rate = 3; // 语速 speech.SpeakAsync(dst); } } else { dst = showCont; } // 不为空 if (!string.IsNullOrEmpty(dst)) { ShowText(dst); } } catch (Exception ex) { if (ex.GetType().FullName == "System.Threading.ThreadAbortException") { return; } // 显示错误 ShowText("错误:" + ex.Message); } finally { if (shot != null && !shot.IsDisposed) { shot.Dispose(); } //if (captureImage != null) // captureImage.Dispose(); } }
// 翻译 private string Translate(bool sendToWindow) { try { if (textBox1_Source.Text == "") { return(""); } if (comboBox2_DestLang.SelectedItem.ToString() != "不翻译") { if (sourceOfTran.IndexOf("有道") != -1) { from = "zh-CHS"; Youdao.YoudaoTran(textBox1_Source.Text, from, Youdao_to[comboBox2_DestLang.SelectedIndex], out src, out dst); } else { from = "zh"; Baidu.Translate(textBox1_Source.Text, from, Baidu_to[comboBox2_DestLang.SelectedIndex], out src, out dst); } } else { dst = textBox1_Source.Text; } // 保存当前选项 FrmMain.TranDestLang = comboBox2_DestLang.SelectedIndex; FrmMain.AutoPressKey = comboBox1_SourceLang.SelectedIndex; FrmMain.AutoSend = checkBox1_AutoSend.Checked; if (!sendToWindow) // 不将译文发送到窗口 { return(dst); } this.WindowState = FormWindowState.Minimized; // 查找窗口句柄 IntPtr hwnd = FindWindowHandle(); // 激活窗口 if (!Api.SetForegroundWindow(hwnd)) { throw new Exception("无法激活窗口!"); } Thread.Sleep(500); KeyBoard(Key[comboBox1_SourceLang.SelectedIndex]); Thread.Sleep(500); SendKeys.SendWait(dst); // SendKeys.Send(dst); // 输入要发送的内容 Thread.Sleep(1000); if (checkBox1_AutoSend.Checked) // 自动发送(按下回车键) { KeyBoard(Keys.Enter); } } catch (Exception ex) { MessageBox.Show(ex.Message, "翻译", MessageBoxButtons.OK, MessageBoxIcon.Error); } finally { if (sendToWindow) // 如果是不将译文发送到窗口,则不会关闭本窗口 { this.Close(); } } return(dst); }