// 设置固定翻译坐标 private void button2_SetPosition_Click(object sender, EventArgs e) { string savePath = DateTime.Now.ToString("yyyy-dd-MM_HH_mm_ss") + ".bmp"; FrmScreenShot shot = new FrmScreenShot(savePath, true); try { // 显示截图窗口 DialogResult result = shot.ShowDialog(); if (result == DialogResult.Cancel) { throw new Exception("用户取消截图!"); } POINT p = new POINT(); p.X = shot.MouseDownPoint.X; p.Y = shot.MouseDownPoint.Y; IntPtr hwnd = FindWindow("grcWindow", null); if (IntPtr.Zero == hwnd) { throw new Exception("找不到GTA窗口句柄"); } // 屏幕坐标转为客户端窗口坐标 ScreenToClient(hwnd, ref p); // 保存截图坐标高宽 screenRect = new Rectangle(p.X, p.Y, shot.ScreenWidth, shot.ScreenHeight); if (shot.ScreenWidth > 0 && shot.ScreenHeight > 0) { MessageBox.Show("设置成功,请点击“保存配置”按钮。", "截图翻译", MessageBoxButtons.OK, MessageBoxIcon.Information); } else { throw new Exception("设置失败,请重试!"); } } catch (Exception ex) { MessageBox.Show("设置固定翻译坐标失败!\n原因:" + ex.Message, "截图翻译", MessageBoxButtons.OK, MessageBoxIcon.Error); } finally { if (File.Exists(savePath)) { File.Delete(savePath); } if (shot != null && !shot.IsDisposed) { shot.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(); } } }