예제 #1
0
        private void ShowTrans(string text, string textBeforeTrans)
        {
            //翻译结果弹窗
            if (Switch4.IsOn && text != textBeforeTrans)
            {
                if (SwitchManyPopups.IsOn)
                {
                    var translateResult = new TranslateResult {
                        TextBox = { Text = text }
                    };

                    //每次弹窗启动位置偏移,未实现
                    //translateResult.WindowStartupLocation = WindowStartupLocation.Manual;
                    //translateResult.Left = System.Windows.Forms.Control.MousePosition.X;
                    //translateResult.Top = System.Windows.Forms.Control.MousePosition.Y;

                    translateResult.Show();
                }
                else
                {
                    if (_firstlySwitch)
                    {
                        var translateResult = new TranslateResult {
                            TextBox = { Text = text }
                        };

                        //每次弹窗启动位置偏移,未实现
                        //translateResult.WindowStartupLocation = WindowStartupLocation.Manual;
                        //translateResult.Left = System.Windows.Forms.Control.MousePosition.X;
                        //translateResult.Top = System.Windows.Forms.Control.MousePosition.Y;

                        translateResult.Show();

                        _firstlySwitch = false;
                        return;
                    }
                    //Get Window
                    if (!(Application.Current.Windows
                          .Cast <Window>()
                          .LastOrDefault(window => window is TranslateResult) is TranslateResult transWindow))
                    {
                        var translateResult = new TranslateResult {
                            TextBox = { Text = text }
                        };
                        translateResult.Show();
                    }
                    else
                    {
                        transWindow.TextBox.Text = text;
                    }
                }
        private void ClipboardChanged(object sender, EventArgs e)
        {
            switch1.IsChecked = Switch1Check;
            switch2.IsChecked = Switch2Check;
            switch3.IsChecked = Switch3Check;
            switch4.IsChecked = Switch4Check;

            if (_firstClipboardChange)
            {
                // Handle your clipboard update
                if (Clipboard.ContainsText())
                {
                    Debug.WriteLine(Clipboard.GetText());

                    // Get the cut/copied text.
                    var text = Clipboard.GetText();

                    text = text.Replace("", "");

                    //Console.WriteLine("123");

                    if (Switch1Check || Switch2Check)
                    {
                        for (var counter = 0; counter < text.Length - 1; counter++)
                        {
                            if (Switch1Check)
                            {
                                if (text[counter + 1].ToString() == "\r")
                                {
                                    if (text[counter].ToString() == ".")
                                    {
                                        continue;
                                    }
                                    if (text[counter].ToString() == "。")
                                    {
                                        continue;
                                    }
                                    text = text.Remove(counter + 1, 2);

                                    //判断英文单词结尾,则加一个空格
                                    if (Regex.IsMatch(text[counter].ToString(), "[a-zA-Z]"))
                                    {
                                        text = text.Insert(counter + 1, " ");
                                    }

                                    //判断"-"结尾,则去除"-"
                                    if (text[counter].ToString() == "-")
                                    {
                                        text = text.Remove(counter, 1);
                                    }
                                }
                            }

                            if (Switch2Check)
                            {
                                if (text[counter].ToString() == " ")
                                {
                                    text = text.Remove(counter, 1);
                                }
                            }
                        }
                    }

                    if (Switch3Check)
                    {
                        if (ChangeStatus == false)
                        {
                            //判断中文
                            if (!Regex.IsMatch(text, @"[\u4e00-\u9fa5]"))
                            {
                                var appId     = TranslateId;
                                var secretKey = TranslateKey;
                                if (Settings.Default.AppID != "none" && Settings.Default.SecretKey != "none")
                                {
                                    appId     = Settings.Default.AppID;
                                    secretKey = Settings.Default.SecretKey;
                                }

                                //这个if已经无效
                                if (appId == "none" || secretKey == "none")
                                {
                                    //MessageBox.Show("请先设置翻译接口", "Copy++");
                                    Show_InputAPIWindow();
                                }
                                else
                                {
                                    text = BaiduTrans(appId, secretKey, text);

                                    //翻译结果弹窗
                                    if (Switch4Check)
                                    {
                                        //MessageBox.Show(text);
                                        var translateResult = new TranslateResult {
                                            textBox = { Text = text }
                                        };

                                        //translateResult.WindowStartupLocation = WindowStartupLocation.Manual;
                                        //translateResult.Left = System.Windows.Forms.Control.MousePosition.X;
                                        //translateResult.Top = System.Windows.Forms.Control.MousePosition.Y;
                                        translateResult.Show();

                                        //var left = translateResult.Left;
                                        //var top = translateResult.Top;
                                    }
                                }
                            }
                        }
                    }

                    //stop monitoring to prevent loop
                    //Clipboard.StopMonitoring();
                    _windowClipboardManager.ClipboardChanged -= ClipboardChanged;

                    Clipboard.SetDataObject(text);
                    //System.Windows.Clipboard.Flush();


                    //restart monitoring
                    //InitializeClipboardMonitor();
                    _windowClipboardManager.ClipboardChanged += ClipboardChanged;
                }

                _firstClipboardChange = false;
            }
            else
            {
                _firstClipboardChange = true;
            }
        }
        private void ClipboardChanged(Object sender, SharpClipboard.ClipboardChangedEventArgs e)
        {
            // Is the content copied of text type?
            if (e.ContentType == SharpClipboard.ContentTypes.Text)
            {
                // Get the cut/copied text.
                string text = e.Content.ToString();

                text = text.Replace("", "");

                //Console.WriteLine("123");

                if (Switch1Check == true || Switch2Check == true)
                {
                    for (int counter = 0; counter < text.Length - 1; counter++)
                    {
                        if (Switch1Check == true)
                        {
                            if (text[counter + 1].ToString() == "\r")
                            {
                                if (text[counter].ToString() == ".")
                                {
                                    continue;
                                }
                                if (text[counter].ToString() == "。")
                                {
                                    continue;
                                }
                                text = text.Remove(counter + 1, 2);

                                //判断英文单词结尾,则加一个空格
                                if (Regex.IsMatch(text[counter].ToString(), "[a-zA-Z]"))
                                {
                                    text = text.Insert(counter + 1, " ");
                                }

                                //判断"-"结尾,则去除"-"
                                if (text[counter].ToString() == "-")
                                {
                                    text = text.Remove(counter, 1);
                                }
                            }
                        }

                        if (Switch2Check == true)
                        {
                            if (text[counter].ToString() == " ")
                            {
                                text = text.Remove(counter, 1);
                            }
                        }
                    }
                }

                if (Switch3Check == true)
                {
                    if (ChangeStatus == false)
                    {
                        //判断中文
                        if (!Regex.IsMatch(text, @"[\u4e00-\u9fa5]"))
                        {
                            string appId     = TranslateId;
                            string secretKey = TranslateKey;
                            if (Properties.Settings.Default.AppID != "none" && Properties.Settings.Default.SecretKey != "none")
                            {
                                appId     = Properties.Settings.Default.AppID;
                                secretKey = Properties.Settings.Default.SecretKey;
                            }

                            //这个if已经无效
                            if (appId == "none" || secretKey == "none")
                            {
                                //MessageBox.Show("请先设置翻译接口", "Copy++");
                                Show_InputAPIWindow();
                            }
                            else
                            {
                                text = BaiduTrans(appId, secretKey, text);

                                //翻译结果弹窗
                                if (Switch4Check == true)
                                {
                                    //MessageBox.Show(text);
                                    TranslateResult translateResult = new TranslateResult();
                                    translateResult.textBox.Text = text;

                                    //translateResult.WindowStartupLocation = WindowStartupLocation.Manual;
                                    //translateResult.Left = System.Windows.Forms.Control.MousePosition.X;
                                    //translateResult.Top = System.Windows.Forms.Control.MousePosition.Y;
                                    translateResult.Show();

                                    //var left = translateResult.Left;
                                    //var top = translateResult.Top;
                                }
                            }
                        }
                    }
                }

                //stop monitoring to prevent loop
                Clipboard.StopMonitoring();

                System.Windows.Clipboard.SetDataObject(text);
                //System.Windows.Clipboard.Flush();



                //restart monitoring
                InitializeClipboardMonitor();
            }

            // If the cut/copied content is complex, use 'Other'.
            else if (e.ContentType == SharpClipboard.ContentTypes.Other)
            {
                //do nothing

                // Do something with 'clipboard.ClipboardObject' or 'e.Content' here...
            }
        }
        private void ClipboardChanged(Object sender, SharpClipboard.ClipboardChangedEventArgs e)
        {
            // Is the content copied of text type?
            if (e.ContentType == SharpClipboard.ContentTypes.Text)
            {
                // Get the cut/copied text.
                string text = e.Content.ToString();

                if (switch1Check == true || switch2Check == true)
                {
                    for (int counter = 0; counter < text.Length - 1; counter++)
                    {
                        if (switch1Check == true)
                        {
                            if (text[counter + 1].ToString() == "\r")
                            {
                                if (text[counter].ToString() == ".")
                                {
                                    continue;
                                }
                                if (text[counter].ToString() == "。")
                                {
                                    continue;
                                }
                                text = text.Remove(counter + 1, 2);
                            }
                        }

                        if (switch2Check == true)
                        {
                            if (text[counter].ToString() == " ")
                            {
                                text = text.Remove(counter, 1);
                            }
                        }
                    }
                }

                if (switch3Check == true)
                {
                    if (changeStatus == false)
                    {
                        string appId     = Properties.Settings.Default.AppID;
                        string secretKey = Properties.Settings.Default.SecretKey;
                        if (appId == "none" || secretKey == "none")
                        {
                            //MessageBox.Show("请先设置翻译接口", "Copy++");
                            Show_InputAPIWindow();
                        }
                        else
                        {
                            text = BaiduTrans(appId, secretKey, text);

                            if (switch4Check == true)
                            {
                                //MessageBox.Show(text);
                                TranslateResult translateResult = new TranslateResult();
                                translateResult.textBox.Text = text;

                                //translateResult.WindowStartupLocation = WindowStartupLocation.Manual;
                                //translateResult.Left = System.Windows.Forms.Control.MousePosition.X;
                                //translateResult.Top = System.Windows.Forms.Control.MousePosition.Y;
                                translateResult.Show();

                                //var left = translateResult.Left;
                                //var top = translateResult.Top;
                            }
                        }
                    }
                }

                //Prevent loop
                clipboard.StopMonitoring();
                Clipboard.SetText(text);
                Clipboard.Flush();
                InitializeClipboardMonitor();
            }

            // If the cut/copied content is complex, use 'Other'.
            else if (e.ContentType == SharpClipboard.ContentTypes.Other)
            {
                //do nothing

                // Do something with 'clipboard.ClipboardObject' or 'e.Content' here...
            }
        }