添加引用:using mshtml;(C:\Program Files (x86)\Microsoft.NET\Primary Interop Assemblies\Microsoft.mshtml.dll)
页面上放webBrowser控件。给Url一个网页地址。
放一按钮button,click事件下:
mshtml.HTMLDocument doc1 = (mshtml.HTMLDocument)this.webBrowser1.Document.DomDocument;
mshtml.HTMLDocumentEvents2_Event hevent = (mshtml.HTMLDocumentEvents2_Event)doc1;
hevent.onmouseout += new HTMLDocumentEvents2_onmouseoutEventHandler(hevent_onmouseout);
hevent.onmouseover += new HTMLDocumentEvents2_onmouseoverEventHandler(hevent_onmouseover);
hevent.onmousedown += new HTMLDocumentEvents2_onmousedownEventHandler(hevent_onmousedown);
其中的三个方法:
string oldcolor = "";
void hevent_onmouseover(IHTMLEventObj e)
{
oldcolor = e.toElement.style.border;
e.toElement.style.border = "2px solid blue";
//this.textBox2.Text = e.toElement.parentElement.innerHTML;
string path = "";
IHTMLElement element = e.toElement;this.textBox3.Text = element.className;
while (element.parentElement != null)
{
IHTMLElementCollection elementCollection = element.parentElement.children as IHTMLElementCollection;
path = element.tagName + "(" + elementCollection.length + ")/" + path;
element = element.parentElement;
}
this.textBox3.Text = path;
}
void hevent_onmouseout(IHTMLEventObj pEvtObj)
{
pEvtObj.fromElement.style.border = oldcolor;
}
void hevent_onmousedown(IHTMLEventObj pEvtObj)
{
mshtml.HTMLDocument doc1 = (mshtml.HTMLDocument)this.webBrowser1.Document.DomDocument;
mshtml.HTMLDocumentEvents2_Event hevent = (mshtml.HTMLDocumentEvents2_Event)doc1;
hevent.onmouseover -= new mshtml.HTMLDocumentEvents2_onmouseoverEventHandler(this.hevent_onmouseover);
pEvtObj.toElement.style.background = oldcolor;
hevent.onmouseout -= new HTMLDocumentEvents2_onmouseoutEventHandler(this.hevent_onmouseout);
hevent.onmousedown -= new HTMLDocumentEvents2_onmousedownEventHandler(hevent_onmousedown);
}
这样就实现了。运行,点击下按钮,然后鼠标放在某个DOM元素上,
就将该元素选中,就会用兰色框起来。
页面上放webBrowser控件。给Url一个网页地址。
放一按钮button,click事件下:
mshtml.HTMLDocument doc1 = (mshtml.HTMLDocument)this.webBrowser1.Document.DomDocument;
mshtml.HTMLDocumentEvents2_Event hevent = (mshtml.HTMLDocumentEvents2_Event)doc1;
hevent.onmouseout += new HTMLDocumentEvents2_onmouseoutEventHandler(hevent_onmouseout);
hevent.onmouseover += new HTMLDocumentEvents2_onmouseoverEventHandler(hevent_onmouseover);
hevent.onmousedown += new HTMLDocumentEvents2_onmousedownEventHandler(hevent_onmousedown);
其中的三个方法:
string oldcolor = "";
void hevent_onmouseover(IHTMLEventObj e)
{
oldcolor = e.toElement.style.border;
e.toElement.style.border = "2px solid blue";
//this.textBox2.Text = e.toElement.parentElement.innerHTML;
string path = "";
IHTMLElement element = e.toElement;this.textBox3.Text = element.className;
while (element.parentElement != null)
{
IHTMLElementCollection elementCollection = element.parentElement.children as IHTMLElementCollection;
path = element.tagName + "(" + elementCollection.length + ")/" + path;
element = element.parentElement;
}
this.textBox3.Text = path;
}
void hevent_onmouseout(IHTMLEventObj pEvtObj)
{
pEvtObj.fromElement.style.border = oldcolor;
}
void hevent_onmousedown(IHTMLEventObj pEvtObj)
{
mshtml.HTMLDocument doc1 = (mshtml.HTMLDocument)this.webBrowser1.Document.DomDocument;
mshtml.HTMLDocumentEvents2_Event hevent = (mshtml.HTMLDocumentEvents2_Event)doc1;
hevent.onmouseover -= new mshtml.HTMLDocumentEvents2_onmouseoverEventHandler(this.hevent_onmouseover);
pEvtObj.toElement.style.background = oldcolor;
hevent.onmouseout -= new HTMLDocumentEvents2_onmouseoutEventHandler(this.hevent_onmouseout);
hevent.onmousedown -= new HTMLDocumentEvents2_onmousedownEventHandler(hevent_onmousedown);
}
这样就实现了。运行,点击下按钮,然后鼠标放在某个DOM元素上,
就将该元素选中,就会用兰色框起来。