// 翻译 private void Tran() { try { if (SourceTran.IndexOf("有道") != -1) { from = "zh-CHS"; Youdao.YoudaoTran(textBox1.Text, from, Youdao_to[comboBox2.SelectedIndex], out src, out dst); } else { from = "zh"; Baidu.Translate(textBox1.Text, from, Baidu_to[comboBox2.SelectedIndex], out src, out dst); } // 查找窗口句柄 IntPtr hwnd = FindWindow(GtaClass, null); if (hwnd == IntPtr.Zero) { throw new Exception("查找GTA5窗口句柄失败!"); } // 激活窗口 if (!SetForegroundWindow(hwnd)) { throw new Exception("无法激活GTA5窗口!"); } Thread.Sleep(100); // 模拟按下按键 //keybd_event(Key[comboBox1.SelectedIndex], 0, 0, 0); //Thread.Sleep(50); //keybd_event(Key[comboBox1.SelectedIndex], 0, KEYEVENTF_KEYUP, 0); SendKeys.Send("{T}"); // SendKeys.Send("我们家有很多菜"); } catch (Exception ex) { MessageBox.Show(ex.Message); } }
// 截图翻译 private void ScreenTran() { string savePath = Environment.GetEnvironmentVariable("TEMP") + "\\" + DateTime.Now.ToString("yyyy-dd-MM_HH_mm_ss") + ".bmp"; string from = "en", src, dst = null; ScreenShot shot = new ScreenShot(savePath, true); ShowCont sc = new ShowCont(ShowTime); try { // 截图翻译并显示 if (TM == TranMode.Tran) { // 显示截图窗口 DialogResult result = shot.ShowDialog(); // 如果文件不存在,则视为用户取消截图 if (!File.Exists(savePath) || result == DialogResult.Cancel) { return; } // 翻译模式EnToZh=true为英译中,false为俄译中 if (EnToZh == false) { from = "ru"; } // SourceTran有“有道”两字使用有道翻译,否则使用百度翻译 if (SourceTran.IndexOf("有道") != -1) { Youdao.YoudaoTran(savePath, from, "zh-CHS", out src, out dst); } else { Baidu.BaiduTran(savePath, from, "zh", out src, out dst); } if (S_CopyToClip) { Clipboard.SetText(src);// 复制原文到剪切板 } if (D_CopyToClip) { Clipboard.SetText(dst);// 复制译文到剪切板 } if (Speak) { SpeechSynthesizer speech = new SpeechSynthesizer(); speech.Rate = 3; // 语速 speech.SpeakAsync(dst); } } else { dst = Show_cont; } if (dst != null && dst != "") { sc.ContText(dst); sc.ShowDialog(); } } catch (ThreadAbortException te) { return; } catch (Exception ex) { dst = "错误:" + ex.Message; sc.ContText(dst); sc.ShowDialog(); //MessageBox.Show(ex.Message); return; } finally { if (File.Exists(savePath)) { File.Delete(savePath); } shot.Dispose(); sc.Dispose(); } }
// 截图翻译 private void ScreenTran() { string savePath = DateTime.Now.ToString("yyyy-dd-MM_HH_mm_ss") + ".bmp"; string from = "en", src, dst = null; FrmScreenShot shot = new FrmScreenShot(savePath, true); try { // 截图翻译并显示 if (tranMode == TranMode.TranAndShowText) { Thread.Sleep(200); // 如果是固定区域翻译(isFixedScreen = true则视为固定区域翻译) if (isFixedScreen == true) { if (screenRect.Width <= 0 || screenRect.Height <= 0) { throw new Exception("请设置固定截图翻译坐标!"); } IntPtr hwnd = FindWindow("grcWindow", null); if (IntPtr.Zero == hwnd) { throw new Exception("找不到GTA窗口句柄"); } POINT p = new POINT(); p.X = screenRect.X; p.Y = screenRect.Y; ClientToScreen(hwnd, ref p); Screenshot(savePath, p.X, p.Y, screenRect.Width, screenRect.Height); // 如果文件不存在,则视为截图失败 if (!File.Exists(savePath)) { throw new Exception("固定区域截图失败!"); } } else { // 显示截图窗口 DialogResult result = shot.ShowDialog(); // 如果文件不存在,则视为用户取消截图 if (!File.Exists(savePath) || result == DialogResult.Cancel) { return; } } // 翻译模式isEnToZh=true为英译中,false为俄译中 if (isEnToZh == false) { from = "ru"; } // sourceOfTran有“有道”两字使用有道翻译,否则使用百度翻译 if (sourceOfTran.IndexOf("有道") != -1) { Youdao.YoudaoTran(savePath, from, "zh-CHS", out src, out dst); } else { Baidu.BaiduTran(savePath, from, "zh", 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)) { using (FrmShowCont sc = new FrmShowCont(showTime)) { sc.ContText(dst); sc.ShowDialog(); } } } catch (Exception ex) { if (ex.GetType().FullName == "System.Threading.ThreadAbortException") { return; } dst = "错误:" + ex.Message; // 显示错误 using (FrmShowCont sc = new FrmShowCont(showTime)) { sc.ContText(dst); sc.ShowDialog(); } } finally { if (File.Exists(savePath)) { File.Delete(savePath); } if (shot != null && !shot.IsDisposed) { shot.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 = FindWindow(null, GtaWindowName); if (hwnd == IntPtr.Zero) { throw new Exception("查找GTA5窗口句柄失败!"); } // 激活窗口 if (!SetForegroundWindow(hwnd)) { throw new Exception("无法激活GTA5窗口!"); } 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) // 如果是不将译文发送到窗口,则不会关闭本窗口 { CloseForm(); } } return(dst); }