具有单击和突出显示 Frame/iFrame 元素的 C# Web 浏览器 [英] C# Web Browser with click and highlight of Frame/iFrame elements
本文介绍了具有单击和突出显示 Frame/iFrame 元素的 C# Web 浏览器的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我正在寻找一个浏览器控件,用户可以在其中预览网页中的框架/iframe,然后突出显示其中的元素,一旦突出显示,我就可以获得所选元素的 div 或 id.
Iam looking for a browser control where users can preview frame/iframe in web page and then highlight elements of it and once highlighted, I can get the div or id of the element selected.
有什么办法可以做到吗?
Is there any way we can do it ?
推荐答案
我有另一种方法...
using System.Runtime.InteropServices;
声明一个委托
public delegate void DOMEvent(IHTMLEventObj e);
private void webBrowser1_DocumentCompleted_1(object sender, WebBrowserDocumentCompletedEventArgs e)
{
if (this.webBrowser1.ReadyState != WebBrowserReadyState.Complete)
return;
else
{
HTMLDocument htmlDoc = (HTMLDocument)this.webBrowser1.Document.DomDocument;
DispHTMLDocument doc = (DispHTMLDocument)htmlDoc;
DOMEventHandler onmousedownhandler = new DOMEventHandler(doc);
onmousedownhandler.Handler += new DOMEvent(Mouse_Down);
doc.onmousedown = onmousedownhandler;
if (htmlDoc.frames.length > 0)
{
FramesCollection FrameList = htmlDoc.frames;
for (int i = 0; i < FrameList.length; i++)
{
object id = (object)i;
IHTMLWindow2 frameWindow = (IHTMLWindow2)FrameList.item(ref id);
HTMLDocument frameDoc = (HTMLDocument)frameWindow.document;
DispHTMLDocument frameDispDoc = (DispHTMLDocument)frameDoc;
DOMEventHandler onmousedownhand = new DOMEventHandler(frameDispDoc);
onmousedownhand.Handler += new DOMEvent(Mouse_Down);
frameDispDoc.onmousedown = onmousedownhand;
}
}
}
}
public class DOMEventHandler
{
public DOMEvent Handler;
DispHTMLDocument Document;
public DOMEventHandler(DispHTMLDocument doc)
{
this.Document = doc;
}
[DispId(0)]
public void Call()
{
Handler(Document.parentWindow.@event);
}
}
public void Mouse_Down(IHTMLEventObj e)
{
IHTMLElement element = (mshtml.IHTMLElement)e.srcElement;
label1.Text = element.id;
}
这篇关于具有单击和突出显示 Frame/iFrame 元素的 C# Web 浏览器的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文