2011-02-25 26 views
0

我想轉換一個IHTMLDOMNodeIHTMLElement在C#中,我曾嘗試以下:將IHTMLDOMNode轉換爲IHTMLElement?

IHTMLElement tempElement = node as IHTMLElement; 
//node is a instance of IHTMLDOMNode internface 

但是,這並不工作 - tempElementnull。有沒有辦法正確執行此轉換?請注意,在我的應用程序中,我嘗試使用WebBrowser來訪問DOM樹中的每個節點並獲取它們的座標。

這裏是我的源代碼,你能告訴我該怎麼辦?

using System; 
using System.Collections.Generic; 
using System.ComponentModel; 
using System.Data; 
using System.Drawing; 
using System.Linq; 
using System.Text; 
using System.Windows.Forms; 
using mshtml; 

namespace TestWindowsFormsApplication1 
{ 
    public partial class Form1 : Form 
    { 
     public Form1() 
     { 
      InitializeComponent(); 
      webBrowser1.ScrollBarsEnabled = true; 
      webBrowser1.Url = new Uri("http://www.bing.com"); 
      webBrowser1.DocumentCompleted += new WebBrowserDocumentCompletedEventHandler(webBrowser1_DocumentCompleted); 
     } 

     void webBrowser1_DocumentCompleted(object sender, WebBrowserDocumentCompletedEventArgs e) 
     { 
      HTMLDocument document = (HTMLDocument)webBrowser1.Document.DomDocument; 
      IHTMLDOMNode bodyNode = (IHTMLDOMNode)webBrowser1.Document.Body.DomElement; 
      TranversalDOM(document, bodyNode); 
     } 

     private void TranversalDOM(HTMLDocument document, IHTMLDOMNode node) 
     { 
      if (node.nodeType == 3) 
      { 
       string nodeInfo = node.nodeValue.ToString(); 

       nodeInfo += ">>>"; 
       IHTMLElement tempElement = node as IHTMLElement; 
       //how to convert IHTMLDOMNode to IHTMLElement? 

       int X1 = findPosX(tempElement); 
       int X2 = X1 + tempElement.offsetWidth; 
       int Y1 = findPosY(tempElement); 
       int Y2 = Y1 + tempElement.offsetHeight; 
       nodeInfo += " LeftTop: ("; 
       nodeInfo += X1.ToString(); 
       nodeInfo += ","; 
       nodeInfo += Y1.ToString(); 
       nodeInfo += ")"; 
       nodeInfo += " RightBottom: ("; 
       nodeInfo += X2.ToString(); 
       nodeInfo += ","; 
       nodeInfo += Y2.ToString(); 
       nodeInfo += ")"; 
       listBox1.Items.Add(nodeInfo); 
      } 
      else 
      { 
       IHTMLDOMChildrenCollection childNodes = node.childNodes as IHTMLDOMChildrenCollection; 
       foreach (IHTMLDOMNode n in childNodes) 
       { 
        TranversalDOM(document, n); 
       } 
      } 
     } 


     public int findPosX(IHTMLElement obj) 
     { 
      int curleft = 0; 
      if (obj.offsetParent != null) 
      { 
       while (obj.offsetParent != null) 
       { 
        curleft += obj.offsetLeft; 
        obj = obj.offsetParent; 
       } 
      } 

      return curleft; 
     } 

     public int findPosY(IHTMLElement obj) 
     { 
      int curtop = 0; 
      if (obj.offsetParent != null) 
      { 
       while (obj.offsetParent != null) 
       { 
        curtop += obj.offsetTop; 
        obj = obj.offsetParent; 
       } 
      } 

      return curtop; 
     } 
    } 
} 

回答

2

如果不是IHTMLElement,則無法進行轉換。

+0

我已經發布我的源代碼,你能告訴我該怎麼辦? – Rockycqu 2011-02-25 17:12:59

+0

這是遞歸的,因此當節點很深時可能發生。在值爲null時查看斷點,並查看類型。我懷疑這是一條評論。 – Aliostad 2011-02-25 17:29:45

+0

Thx,你說得對。 當節點是Text節點時,它不能變成IHTMLElement – Rockycqu 2011-02-25 17:32:03

-1

我不熟悉這些接口,但你可以試試:

IHTMLElement tempElement = (IHTMLElement)node; 
+0

我試過這個,但是在此之後,tempElement = null – Rockycqu 2011-02-25 17:10:12