Exemplo n.º 1
0
 // 设置固定翻译坐标
 private void button_SetPosition_Click(object sender, EventArgs e)
 {
     try
     {
         IntPtr hwnd = FindAndActiveWindow();
         using (var shot = new FrmScreenShot())
         {
             // 显示截图窗口
             if (shot.Start(hwnd) == DialogResult.Cancel)
             {
                 throw new Exception("用户取消截图!");
             }
             // 保存截图坐标高宽
             screenRect = shot.SelectedArea;
         }
         //if (screenRect.X >= 0 && screenRect.Y >= 0 && screenRect.Width > 0 && screenRect.Height > 0)
         //    MessageBox.Show("设置成功!\n请点击“保存配置”按钮。", this.Text, MessageBoxButtons.OK, MessageBoxIcon.Information);
         //else
         //    throw new Exception("设置失败,请重试!");
         MessageBox.Show("设置成功!\n请点击“保存配置”按钮。", this.Text, MessageBoxButtons.OK, MessageBoxIcon.Information);
     }
     catch (Exception ex)
     {
         MessageBox.Show("设置固定翻译坐标失败!\n原因:" + ex.Message, this.Text, MessageBoxButtons.OK, MessageBoxIcon.Error);
     }
     finally
     {
         MinimizeWindow(false);
     }
 }
Exemplo n.º 2
0
        // 设置固定翻译坐标
        private void button_SetPosition_Click(object sender, EventArgs e)
        {
            FrmScreenShot shot = new FrmScreenShot();

            try
            {
                IntPtr hwnd = FindAndActiveWindow();
                shot.WindowHandle = hwnd;
                shot.CopyScreen();
                // 显示截图窗口
                DialogResult result = shot.ShowDialog();
                if (result == DialogResult.Cancel)
                {
                    throw new Exception("用户取消截图!");
                }

                Api.POINT p = new Api.POINT();
                p.X = shot.StartPos.X;
                p.Y = shot.StartPos.Y;

                // 屏幕坐标转为客户端窗口坐标
                Api.ScreenToClient(hwnd, ref p);
                // 保存截图坐标高宽
                screenRect = new Rectangle(p.X, p.Y, shot.SelectedArea.Width, shot.SelectedArea.Height);
                if (shot.SelectedArea.Width > 0 && shot.SelectedArea.Height > 0)
                {
                    MessageBox.Show("设置成功!\n请点击“保存配置”按钮。", this.Text, MessageBoxButtons.OK, MessageBoxIcon.Information);
                }
                else
                {
                    throw new Exception("设置失败,请重试!");
                }
            }
            catch (Exception ex)
            {
                MessageBox.Show("设置固定翻译坐标失败!\n原因:" + ex.Message, this.Text, MessageBoxButtons.OK, MessageBoxIcon.Error);
            }
            finally
            {
                MinimizeWindow(false);
                if (shot != null && !shot.IsDisposed)
                {
                    shot.Dispose();
                }
            }
        }
Exemplo n.º 3
0
        // 截图翻译
        private void ScreenTranslation()
        {
            Image captureImage;

            if (st != null && !st.IsDisposed)
            {
                st.Dispose();
            }
            try
            {   // 隐藏窗口
                this.Invoke(new Action(() => MinimizeWindow(true)));
                Thread.Sleep(200);
                // 如果是固定区域翻译(isFixedScreen = true则视为固定区域翻译)
                if (isFixedScreen)
                {
                    captureImage = FixedScreen();
                }
                else
                {
                    if (shot != null && !shot.IsDisposed)
                    {
                        shot.Dispose();
                    }
                    shot = new FrmScreenShot();
                    if (shot.Start() == DialogResult.Cancel) // 显示截图窗口
                    {
                        return;
                    }
                    captureImage = (Image)shot.CaptureImage?.Clone();
                }
                TranslateAndShowResult(captureImage);
            }
            catch (Exception ex)
            {
                ShowText("错误:" + ex.Message);
            }
        }
Exemplo n.º 4
0
        // 截图翻译
        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();
            }
        }