C#借助谷歌翻译实现翻译小工具(一)基本功能实现

软件效果:

C#借助谷歌翻译实现翻译小工具(一)基本功能实现_第1张图片

C#借助谷歌翻译实现翻译小工具(一)基本功能实现_第2张图片

实现原理很简单,就是封装谷歌翻译网站:http://translate.google.cn/,一个WebBrowser“肢解”谷歌翻译网站的HtmlElement元素,删除不要的,留下需要的就是了,没什么难度

实现步骤:

一个winform窗体+一个WebBrowser控件

C#借助谷歌翻译实现翻译小工具(一)基本功能实现_第3张图片

 

设置WebBrowser的URL为谷歌翻译网站,删除不需要的html元素

全部代码:

public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
            this.Shown += Form1_Shown;
        }
        const string url = "http://translate.google.cn/?hl=zh-cn#zh-CN/en/%E6%9D%A5%E8%87%AA%0Ahttp%3A%2F%2Ftranslate.google.cn%2F";
        private void Form1_Load(object sender, EventArgs e)
        {
            webBrowser1.IsWebBrowserContextMenuEnabled = false;
            webBrowser1.ScriptErrorsSuppressed = true;
            webBrowser1.Url = new Uri(url);
        }
        void Form1_Shown(object sender, EventArgs e)
        {
            while (webBrowser1.Document == null)
                Application.DoEvents();
            HtmlElement element;
            deleteAllElement(webBrowser1.Document.Body, "DIV", "gt-c", out element);
            deleteAllElement(element, "DIV", "gt-form-c", out element);
            deleteAllElement(element, "FORM", "gt-form", out element);
            deleteAllElement(element, "DIV", "gt-text-all", out element);
            deleteAllElement(element, "DIV", "gt-main", out element);
            deleteAllElement(element, "DIV", "gt-text-c", out element);
            deleteElementByID(element, "gt-ft");

            //获得gc-text-top元素
            foreach (HtmlElement ele in element.Children)
            {
                if (ele.Id == "gt-text-top")
                {
                    element = ele;
                    break;
                }
            }

            #region 去掉 上传,帮助
            HtmlElement srcElement = null;
            foreach (HtmlElement ele in element.Children)
            {
                if (ele.Id == "gt-src-c")
                {
                    srcElement = ele;
                    break;
                }
            }
            deleteAllElement(srcElement, "", "gt-src-p", out srcElement);
            deleteAllElement(srcElement, "DIV", "gt-src-wrap", out srcElement);

            #endregion
            //去掉有错误
            #region 去掉 上传,帮助
            HtmlElement resElement = null;
            foreach (HtmlElement ele in element.Children)
            {
                if (ele.Id == "gt-res-c")
                {
                    resElement = ele;
                    break;
                }
            }
            deleteAllElement(resElement, "", "gt-res-p", out resElement);
            deleteAllElement(resElement, "", "gt-res-data", out resElement);
            deleteAllElement(resElement, "DIV", "gt-res-wrap", out resElement);
            deleteElementByID(resElement, "gt-res-tools");
            #endregion
        }


        void deleteAllElement(HtmlElement HtmlEle, string TagName, string IgroneID, out HtmlElement element)
        {
            element = null;
            if (HtmlEle == null) return;
            List<HtmlElement> lst = new List<HtmlElement>();

            foreach(HtmlElement div in HtmlEle.Children)
            {

                if (!String.IsNullOrEmpty(TagName) && div.TagName != TagName) continue;
                if (div.Id != IgroneID)
                {
                    lst.Add(div);
                    
                }
                else
                    element = div;
            }
            foreach (HtmlElement ele in lst)
            {
                IHTMLDOMNode node = ele.DomElement as IHTMLDOMNode;//this.webBrowser1.DomElement as mshtml.IHTMLDOMNode;
                if (node != null)
                {
                    node.parentNode.removeChild(node);
                }
            }
        }
        void deleteElementByID(HtmlElement HtmlEle, string DeleteID)
        {
            
            
            foreach (HtmlElement div in HtmlEle.Children)
            {

                if (div.Id == DeleteID)
                {
                    IHTMLDOMNode node = div.DomElement as IHTMLDOMNode;//this.webBrowser1.DomElement as mshtml.IHTMLDOMNode;
                    if (node != null)
                    {
                        node.parentNode.removeChild(node);
                    }
                    return;

                }
            }

        }
    }

下载地址:CSDN下载

下一节:

C#借助谷歌翻译实现翻译小工具(二)添加托盘图标

你可能感兴趣的:(C#)