C# WebBrowser 获得选中部分的html源码

Winform程序 2.0的. 需要引用Microsoft.mshtml.

        private void Form1_Load(object sender, EventArgs e)
        {
            webBrowser1.Navigate("http://www.baidu.com");
            webBrowser1.DocumentCompleted += new WebBrowserDocumentCompletedEventHandler(webBrowser1_DocumentCompleted);
        }

        void webBrowser1_DocumentCompleted(object sender, WebBrowserDocumentCompletedEventArgs e)
        {
            webBrowser1.Document.MouseUp += new HtmlElementEventHandler(Document_MouseUp);
        }

        void Document_MouseUp(object sender, HtmlElementEventArgs e)
        {
            IHTMLDocument2 document = (IHTMLDocument2)webBrowser1.Document.DomDocument;
            IHTMLTxtRange htmlElem = (IHTMLTxtRange)document.selection.createRange();
            string s = htmlElem.htmlText;
            richTextBox1.Text = s;
        }
View Code

需要看document.selection.createRange();类型 需要引用Microsoft.VisualBasic. 用Microsoft.VisualBasic.Information.TypeName(document.selection.createRange());方法查看类型.

差不多就这些了~ 不知道每次记录这些遇到的问题 能帮助别人不..- -|

posted @ 2014-06-27 15:52  Napoléon  阅读(1874)  评论(0编辑  收藏  举报