在MSHTML界面HTMLDocumentEvents4中 应该包含大约30个事件 (根据msdn页面http://msdn.microsoft.com/en-us/library/ff976251(v=vs.85).aspx#events) 但只有onmssitemodejumplistitemremoved和onmsthumbnailclick可用我想访问其他事件..特别是更改事件.. 如何访问其他活动
using System.Runtime.InteropServices;
namespace MSHTML
{
[ComVisible(false)]
[TypeLibType(TypeLibTypeFlags.FHidden)]
public interface HTMLDocumentEvents4_Event
{
event HTMLDocumentEvents4_onmssitemodejumplistitemremovedEventHandler onmssitemodejumplistitemremoved;
event HTMLDocumentEvents4_onmsthumbnailclickEventHandler onmsthumbnailclick;
}
}
在我的代码中,我试图访问如下,但它无效(侦听器未分配)
//Event handler class
public delegate void DHTMLEvent(IHTMLEventObj e);
[ComVisible(true)]
public class DHTMLEventHandler
{
public DHTMLEvent Handler;
HTMLDocument Document;
public DHTMLEventHandler(HTMLDocument doc)
{
this.Document = doc;
}
[DispId(0)]
public void Call()
{
Handler(Document.parentWindow.@event);
}
}
//My code
HTMLDocumentEvents2_Event Events = (HTMLDocumentEvents2_Event)document;
Events.onclick += new HTMLDocumentEvents2_onclickEventHandler(Events_onclick);//working
HTMLDocumentEvents4_Event Events2 = (HTMLDocumentEvents4_Event)document;
// Events2.change ->Not availabe so as a workaround I did the below but that is also not // working
DHTMLEventHandler onchangeHandler = new DHTMLEventHandler(document);
onchangeHandler.Handler += new DHTMLEvent(Events_onchange);
document.attachEvent("onchange", onchangeHandler); //Not working
document.onchange = onchangeHandler; //Not Working
private void Events_onchange(IHTMLEventObj e)
{
//I want to do my operation here but this is not called as listener is not assigned
}
private Boolean Events_onclick(IHTMLEventObj e)
{
//working perfectly
}
所以我想通过自定义方法或使用HTMLDocumentEvents4界面的隐藏事件来监听onchange事件。帮助我
答案 0 :(得分:1)
我得到了一个解决方法,用于将事件监听器附加到元素
1.创建HTMLHandlerClass,用于将事件处理程序附加到元素
2.设置文档中的所有元素或要为其添加监听器的元素
3.将元素广播为IHTMLElement2
4.通过调用element.attachEvent来附加eventlistener
第1步
using MSHTML;
using System;
using System.Collections.Generic;
using System.Globalization;
using System.Linq;
using System.Reflection;
using System.Text;
namespace IEToolbarEngine
{
class HtmlHandler : IReflect
{
public HtmlHandler(EventHandler evHandler, IHTMLWindow2 sourceWindow)
{
this.eventHandler = evHandler;
this.htmlWindow = sourceWindow;
}
public IHTMLWindow2 SourceHTMLWindow
{
get { return this.htmlWindow; }
}
#region IReflect
FieldInfo IReflect.GetField(string name, BindingFlags bindingAttr)
{
return this.typeIReflectImplementation.GetField(name, bindingAttr);
}
FieldInfo[] IReflect.GetFields(BindingFlags bindingAttr)
{
return this.typeIReflectImplementation.GetFields(bindingAttr);
}
MemberInfo[] IReflect.GetMember(string name, BindingFlags bindingAttr)
{
return this.typeIReflectImplementation.GetMember(name, bindingAttr);
}
MemberInfo[] IReflect.GetMembers(BindingFlags bindingAttr)
{
return this.typeIReflectImplementation.GetMembers(bindingAttr);
}
MethodInfo IReflect.GetMethod(string name, BindingFlags bindingAttr)
{
return this.typeIReflectImplementation.GetMethod(name, bindingAttr);
}
MethodInfo IReflect.GetMethod(string name, BindingFlags bindingAttr, Binder binder, Type[] types, ParameterModifier[] modifiers)
{
return this.typeIReflectImplementation.GetMethod(name, bindingAttr, binder, types, modifiers);
}
MethodInfo[] IReflect.GetMethods(BindingFlags bindingAttr)
{
return this.typeIReflectImplementation.GetMethods(bindingAttr);
}
PropertyInfo[] IReflect.GetProperties(BindingFlags bindingAttr)
{
return this.typeIReflectImplementation.GetProperties(bindingAttr);
}
PropertyInfo IReflect.GetProperty(string name, BindingFlags bindingAttr)
{
return this.typeIReflectImplementation.GetProperty(name, bindingAttr);
}
PropertyInfo IReflect.GetProperty(string name, BindingFlags bindingAttr, Binder binder, Type returnType, Type[] types, ParameterModifier[] modifiers)
{
return this.typeIReflectImplementation.GetProperty(name, bindingAttr, binder, returnType, types, modifiers);
}
object IReflect.InvokeMember(string name, BindingFlags invokeAttr, Binder binder, object target, object[] args, ParameterModifier[] modifiers, CultureInfo culture, string[] namedParameters)
{
if (name == "[DISPID=0]")
{
if (this.eventHandler != null)
{
this.eventHandler(this, EventArgs.Empty);
}
}
return null;
}
Type IReflect.UnderlyingSystemType
{
get
{
return this.typeIReflectImplementation.UnderlyingSystemType;
}
}
#endregion
private IReflect typeIReflectImplementation = typeof(HtmlHandler);
private EventHandler eventHandler;
private IHTMLWindow2 htmlWindow;
}
}
步骤2
这里我得到所有的输入元素,因为我想附加onchange事件监听器
private void attachOnChangeEvent(HTMLDocument doc, Boolean flag)
{
// Install onchange for <input type=password, text, file>.
IHTMLElementCollection inputElementList = doc.getElementsByTagName("input");
foreach (IHTMLElement el in inputElementList)
{
String inputType = el.getAttribute("type") as String;
if (("text" == inputType) || ("password" == inputType) || ("file" == inputType))
{
IHTMLElement2 inputElement = el as IHTMLElement2;
IHTMLWindow2 wnd = (el.document as IHTMLDocument2).parentWindow;
inputElement.attachEvent("onchange", new HtmlHandler(onchangeInput, wnd));
}
}
}
private void onchangeInput(object sender, EventArgs e)
{
HtmlHandler htmlHandler = (HtmlHandler)sender;
IHTMLElement element = htmlHandler.SourceHTMLWindow.@event.srcElement;
//Do whatever i want with the element
}