// 翻译 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 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); }