Google在线翻译(WinForm版)

本文介绍了一款基于Google在线翻译服务的Windows窗体应用程序。该应用使用WebBrowser控件实现翻译功能,通过调用Google翻译网站并提交表单来完成翻译过程。文章提供了关键代码示例,包括设置翻译文本、提交表单及获取翻译结果。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

   最近闲的无事,写了一个Google在线翻译的WinForm版,里面用到了webbrowser空间,本来是不想用这个空间的,但是直接POST提交数据上去,没办法得到返回信息,返回的信息一直为空,很是郁闷,所以不得已用到了webbrowser空间,话不多说,直接上代码.

 


 

 

 

 

 

 

 

 

 

  private void button1_Click(object sender, EventArgs e)
        {
            if (this.textBox1.Text.Equals("") || this.comboBox1.Text.Equals(""))
            {
                MessageBox.Show("请填写内容");
            }
            else
            {
                this.setvalues();
                HtmlElement form = webBrowser1.Document.GetElementById("text_form");
                form.InvokeMember("submit");
                this.textBox2.Text = this.getContent();
            }
        }
        //赋值
        public void setvalues()
        {
            webBrowser1.Document.Forms["text_form"].Document.GetElementById("text").InnerText = this.textBox1.Text;
        }
        private void Form1_Load(object sender, EventArgs e)
        {
            this.comboBox1.Items.Add("从英文到中文");
            this.comboBox1.Items.Add("从中文到英文");
            this.webBrowser1.Navigate("http://translate.google.cn/#");
           
        }

        private void webBrowser1_DocumentCompleted(object sender, WebBrowserDocumentCompletedEventArgs e)
        {
           string tranResult = getContent();
           textBox2.ForeColor = Color.Black;
           textBox2.Text = tranResult;
        }
        //过滤相关信息
        private string getContent()
        {
            string value = "";
            HtmlDocument doc = webBrowser1.Document; //获取document对象
            foreach (HtmlElement em in doc.All) //轮循
            {
                string str = em.Id;
                if (str == "result_box")
                {
                    value = em.OuterText;
                }
            }
            return value;
        }
        //互换
        private void GetUrl()
        {
            HtmlElementCollection links = this.webBrowser1.Document.Links;
            foreach (HtmlElement link in links)
            {
                //这里是模糊查找
                if (link.GetAttribute("href").Contains(@""))
                {
                    link.InvokeMember("click"); //激发链接的点击事件
                }
            }
        }
        private void comboBox1_SelectedIndexChanged(object sender, EventArgs e)
        {
            string text=webBrowser1.Document.Forms["text_form"].Document.GetElementById("old_sl").InnerText;
            if (this.comboBox1.Text.Equals("从英文到中文"))
            {
                this.webBrowser1.Navigate("http://translate.google.cn/#");
            }
            else
            {
                this.GetUrl();
            }
        }

评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值