/// <summary> /// 构建鼠标点击事件,同时遍历iframe /// </summary> private void InspectMouseEvent() { mshtml.HTMLDocument htmlDoc = browser.WebbrowserObject.Document as mshtml.HTMLDocument; mshtml.DispHTMLDocument disp = htmlDoc as mshtml.DispHTMLDocument; DHTMLEventHandler onmousedownhandler = new DHTMLEventHandler(htmlDoc); onmousedownhandler.Handler += new DHTMLEvent(Mouse_Down); disp.onmousedown = onmousedownhandler; IHTMLElementCollection col = BrowserExtensions.GetFrames((IHTMLDocument2)htmlDoc); InspectFrameMouseEvent(col); }
private void InspectFrameMouseEvent(mshtml.IHTMLElementCollection fc) { if (fc == null) { return; } if (fc.length > 0) { for (int i = 0; i < fc.length; i++) { object id = (object)i; IHTMLWindow2 frameWindow = (IHTMLWindow2)fc.item(id, 0); mshtml.HTMLDocument frameDoc = (mshtml.HTMLDocument)frameWindow.document; mshtml.DispHTMLDocument frameDispDoc = (mshtml.DispHTMLDocument)frameDoc; DHTMLEventHandler onmousedownhand = new DHTMLEventHandler(frameDoc); onmousedownhand.Handler += new DHTMLEvent(Mouse_Down); frameDispDoc.onmousedown = onmousedownhand; IHTMLElementCollection col = BrowserExtensions.GetFrames((IHTMLDocument2)frameDoc); InspectFrameMouseEvent(col); } } }
/// <summary> /// Recursive method to walk the DOM, acounts for frames /// </summary> /// <param name="node">Parent DOM node to walk</param> /// <param name="deep">tree deep</param> /// <returns></returns> private void ParseNodes(IHTMLDOMNode nd, TreeNode node, int deep) { deep--; try { intervalTimes++; if (intervalTimes > 20) { Application.DoEvents(); intervalTimes = 0; } if (nd.nodeName == Framenode || nd.nodeName == Iframenode) { mshtml.DispHTMLDocument doc3 = (mshtml.DispHTMLDocument)((SHDocVw.IWebBrowser2)nd).Document; var tempnode = (IHTMLDOMNode)doc3.documentElement; //get the comments for this node, if any var framends = (IHTMLDOMChildrenCollection)doc3.childNodes; foreach (IHTMLDOMNode tmpnd in framends) { IHTMLElement el = tmpnd as IHTMLElement; var subTreeNode = NewDomNode(tmpnd); node.Nodes.Add(subTreeNode); subTreeNode.IsLoadData = true; ParseNodes(tmpnd, subTreeNode, expandDeep); } return; } //Get the DOM collection bool hasElement = false; foreach (IHTMLDOMNode childNode in nd.childNodes) { if (childNode.nodeName == Textnode) { node.Text += string.Format("{0}", childNode.nodeValue); } else if (childNode.nodeName == Commentnode) { } else { hasElement = true; var subNode = NewDomNode(childNode); BeginUpdate(); node.Nodes.Add(subNode); EndUpdate(); if (deep > 0) { subNode.IsLoadData = true; ParseNodes(childNode, subNode, deep); } } } if (!hasElement) { node.Text += string.Format("</{0}>", nd.nodeName.ToLower()); } } catch (Exception ex) { logger.Fatal(ex); } }