Ejemplo n.º 1
0
        private void listen()
        {
            Timer t = new Timer();

            t.Interval = 1000;
            t.Start();

            while (true)
            {
                if (t.Interval == 1000)
                {
                    string tempString = string.Empty;
                    tempString = System.Windows.Forms.Clipboard.GetText();
                    var element = AutomationElement.FocusedElement;
                    if ((!string.IsNullOrWhiteSpace(tempString)))
                    {
                        var sb = new StringBuilder();
                        sb.AppendLine(tempString);
                        string selectedText = string.Empty;
                        if (sb.ToString() != "\r\n")
                        {
                            popUpX = GetCursorPosition().X;
                            popUpY = GetCursorPosition().Y;
                            translateWord.originalWord = sb.ToString();
                            try
                            {
                                string content = translateWord.originalWord;
                                translateIsDone = false;

                                // Set the From and To language
                                //string fromLanguage = "English";
                                //string toLanguage = "Turkish";

                                // Create a Language mapping
                                var languageMap = new Dictionary <string, string>();
                                InitLanguageMap(languageMap);

                                // Create an instance of WebClient in order to make the language translation
                                //Uri address = new Uri("http://translate.google.com/");
                                Uri       address = new Uri("https://translate.google.com/");
                                WebClient wc      = new WebClient();
                                wc.Headers[HttpRequestHeader.ContentType] = "application/x-www-form-urlencoded";
                                wc.UploadStringCompleted += new UploadStringCompletedEventHandler(wc_UploadStringCompleted);

                                // Async Upload to the specified source
                                // i.e http://translate.google.com/translate_t for handling the translation.
                                wc.UploadStringAsync(address,
                                                     GetPostData(languageMap[fromLanguage], languageMap[toLanguage], content));

                                while (wc.IsBusy || !translateIsDone)
                                {
                                    System.Threading.Thread.Sleep(2000);
                                }
                                if (!string.IsNullOrWhiteSpace(translateWord.translatedWord))
                                {
                                    if (oldSelectedText != translateWord.originalWord)
                                    {
                                        oldSelectedText = translateWord.originalWord;
                                        int uzunluk = oldSelectedText.Length;
                                        Yaz(oldSelectedText.Remove(uzunluk - 2, 2) + " :   " + translateWord.translatedWord + "\n");
                                        PopUp frm = new PopUp(translateWord.translatedWord, 500, 500);
                                        frm.Location = new Point(popUpX, popUpY);
                                        frm.ShowDialog();
                                        translateWord.originalWord   = string.Empty;
                                        translateWord.translatedWord = string.Empty;
                                    }
                                }
                            }
                            catch (Exception ex)
                            {
                                MessageBox.Show("Bir hata oluştu." + ex.Message);
                            }
                        }
                        element = null;
                    }
                }
            }
        }
Ejemplo n.º 2
0
        private void listen()
        {
            Timer t = new Timer();
            t.Interval = 1000;
            t.Start();

            while (true)
            {
                if (t.Interval == 1000)
                {
                    string tempString = string.Empty;
                    tempString = System.Windows.Forms.Clipboard.GetText();
                    var element = AutomationElement.FocusedElement;
                    if ((!string.IsNullOrWhiteSpace(tempString)))
                    {
                        var sb = new StringBuilder();
                        sb.AppendLine(tempString);
                        string selectedText = string.Empty;
                        if (sb.ToString() != "\r\n")
                        {
                            popUpX = GetCursorPosition().X;
                            popUpY = GetCursorPosition().Y;
                            translateWord.originalWord = sb.ToString();
                            try
                            {
                                string content = translateWord.originalWord;
                                translateIsDone = false;

                                // Set the From and To language
                                //string fromLanguage = "English";
                                //string toLanguage = "Turkish";

                                // Create a Language mapping
                                var languageMap = new Dictionary<string, string>();
                                InitLanguageMap(languageMap);

                                // Create an instance of WebClient in order to make the language translation
                                //Uri address = new Uri("http://translate.google.com/");
                                Uri address = new Uri("https://translate.google.com/");
                                WebClient wc = new WebClient();
                                wc.Headers[HttpRequestHeader.ContentType] = "application/x-www-form-urlencoded";
                                wc.UploadStringCompleted += new UploadStringCompletedEventHandler(wc_UploadStringCompleted);

                                // Async Upload to the specified source
                                // i.e http://translate.google.com/translate_t for handling the translation.
                                wc.UploadStringAsync(address,
                                   GetPostData(languageMap[fromLanguage], languageMap[toLanguage], content));

                                while (wc.IsBusy || !translateIsDone)
                                {
                                    System.Threading.Thread.Sleep(2000);
                                }
                                if (!string.IsNullOrWhiteSpace(translateWord.translatedWord))
                                {
                                    if (oldSelectedText != translateWord.originalWord)
                                    {
                                        oldSelectedText = translateWord.originalWord;
                                        int uzunluk = oldSelectedText.Length;
                                        Yaz(oldSelectedText.Remove(uzunluk-2,2) + " :   " + translateWord.translatedWord + "\n");
                                        PopUp frm = new PopUp(translateWord.translatedWord, 500, 500);
                                        frm.Location = new Point(popUpX, popUpY);
                                        frm.ShowDialog();
                                        translateWord.originalWord = string.Empty;
                                        translateWord.translatedWord = string.Empty;
                                    }
                                }
                            }
                            catch (Exception ex)
                            {
                                MessageBox.Show("Bir hata oluştu." + ex.Message);
                            }
                        }
                        element = null;

                    }
                }


            }
        }