zoukankan      html  css  js  c++  java
  • C#借助谷歌翻译实现翻译小工具(一)基本功能实现

    软件效果:

    image

    image

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

    实现步骤:

    一个winform窗体+一个WebBrowser控件

    image

    设置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#借助谷歌翻译实现翻译小工具(二)添加托盘图标

    慎于行,敏于思!GGGGGG
  • 相关阅读:
    Codeforces Round #622 C2.Skyscrapers (hard version)
    蓝桥杯 54合根植物(并查集+统计集合个数)
    蓝桥杯 6翻硬币
    Codeforces Round #622 (Div. 2) C1. Skyscrapers (easy version)(简单版本暴力)
    Codeforces Round #622 (Div. 2) A. Fast Food Restaurant
    洛谷P1734 最大约数和(01背包)
    HDU 1069 Monkey and Banana(线性DP)
    2019CSP-S T1格雷码
    eclipse使用git提交项目
    GitHub 注册失败的原因 以及解决 。
  • 原文地址:https://www.cnblogs.com/GarsonZhang/p/4335042.html
Copyright © 2011-2022 走看看