110,538
社区成员
发帖
与我相关
我的任务
分享
[ComVisible(true)]
public class HTMLEventHandler
{
public HTMLEvent OnHandler;
private HTMLDocument Document;
public HTMLEventHandler(HTMLDocument doc)
{
this.Document = doc;
}
[DispId(0)]
public void Call()
{
if (System.Windows.Forms.Application.OpenForms.Count > 0)
{
System.Windows.Forms.Form frm = System.Windows.Forms.Application.OpenForms.Cast<System.Windows.Forms.Form>().First();
if (frm.InvokeRequired)
{
frm.Invoke(new Action(() =>
{
OnHandler(Document.parentWindow.@event);
}));
}
}
}
}
((mshtml.HTMLDocumentEvents2_Event)Doc).onclick += new HTMLDocumentEvents2_onclickEventHandler(Program_onclick);
private bool Program_onclick(IHTMLEventObj e)
{
if (this.InvokeRequired)
{
this.Invoke(new Action(() => {
InvokeShow(string.Format("TagName:{0},InnserText:{1}", e.srcElement.tagName, e.srcElement.innerText)); <--
}));
}
return true;
}
第二种只是纯监听Click事件,这样就不会像上面那样,可以取到鼠标点击了哪个元素
public delegate void DOMEvent(mshtml.IHTMLEventObj e);
public class DOMEventHandler
{
public DOMEvent Handler;
DispHTMLDocument Document;
public DOMEventHandler(DispHTMLDocument doc)
{
this.Document = doc;
}
[DispId(0)]
public void Call()
{
Handler(Document.parentWindow.@event);
}
}
mshtml.DispHTMLDocument disp = ie.Document as DispHTMLDocument;
DOMEventHandler onmousedownhandler = new DOMEventHandler(disp);
onmousedownhandler.Handler += new DOMEvent(Mouse_Down);
disp.onmousedown = onmousedownhandler;
public void Mouse_Down(mshtml.IHTMLEventObj e)
{
if (System.Windows.Forms.Application.OpenForms.Count > 0) 跟上面一样这里要用委托 要不然会提示夸线程异常
{
System.Windows.Forms.Form frm = System.Windows.Forms.Application.OpenForms.Cast<System.Windows.Forms.Form>().First();
if (frm.InvokeRequired)
{
frm.Invoke(new Action(() =>
{
OnHandler(Document.parentWindow.@event);
}));
}
}
}