简体   繁体   English

IE9的C#BHO中的Onscroll事件处理

[英]Onscroll event handling in C# BHO for IE9

I'm working on C# BHO plug-in for IE. 我正在为IE开发C#BHO插件。 Plug-in supposed to react on scroll event. 插件应该对滚动事件做出反应。 Code bellow responsible for it: 下面的代码负责:

var document = (HTMLDocument)webBrowser.Document;
((HTMLWindowEvents2_Event)document.parentWindow).onscroll += WebBrowserWindowOnScroll;

This approach works pretty good in IE7 and IE8. 这种方法在IE7和IE8中效果很好。 But completely useless in IE9. 但是在IE9中完全没用。 I have found this workaround: http://social.msdn.microsoft.com/Forums/et-EE/ieextensiondevelopment/thread/808df95a-c559-44c3-93b7-b9e3b2c3b737 我发现此解决方法: http : //social.msdn.microsoft.com/Forums/et-EE/ieextensiondevelopment/thread/808df95a-c559-44c3-93b7-b9e3b2c3b737

It seems that it should solve problem but unfortunately it on C++ and I failed to move it on C#. 看来它应该可以解决问题,但不幸的是,它在C ++上却无法在C#上移动。 Can someone suggest workaround for IE9 or how to implement approach mentioned above on C#? 有人可以建议IE9的解决方法还是在C#上实现上述方法?

Thanks so much! 非常感谢!

I managed to find the solution. 我设法找到了解决方案。

IHTMLWindow3 has a method attachEvent which requires name of the event as a first argument (“onscroll” in my case) and object which will be responsible for event handling. IHTMLWindow3有一个方法attachEvent ,它需要事件的名称作为第一个参数(在我的情况下为“ onscroll”)和负责事件处理的对象。 The trickiest part is connected with this handler object. 最棘手的部分与此处理程序对象连接。 It should implement IDispatch interface, but IE9 use this interface in a pretty bizarre way. 它应该实现IDispatch接口,但是IE9以一种非常奇怪的方式使用此接口。 It calls IDispatch.Invoke without specifying a method name which should be called. 它调用IDispatch.Invoke而不指定应调用的方法名称。 .NET automatically implements IDispatch when class marked by [ClassInterface(ClassInterfaceType.AutoDispatch)] attribute, and uses reflection to call its instance methods according to arguments of IDispatch.Invoke . [ClassInterface(ClassInterfaceType.AutoDispatch)]属性标记的类时,.NET自动实现IDispatch ,并根据IDispatch.Invoke的参数使用反射来调用其实例方法。 In our case method name is empty so nothing will be called. 在我们的例子中,方法名称为空,因此不会调用任何内容。 [DispId(0)] attribute allows to solve this problem, it specifies method what should be called if Invoke receives empty method name. [DispId(0)]属性允许解决此问题,它指定方法,如果Invoke接收到空方法名称,则应调用什么方法。

[ComVisible(true)]
[ClassInterface(ClassInterfaceType.AutoDispatch)]
public class EventListener
{
    [DispId(0)]
    public void HandleEvent(object target)
    {

    }
}

It should be mentioned that name of handler method doesn't matter. 应该提到的是,处理程序方法的名称无关紧要。 But its signature is important. 但是它的签名很重要。 fe for 'onscroll' event it should be like shown above, 'onclick' handler requires no arguments etc. fe for'onscroll'事件应该如上所示,'onclick'处理程序不需要参数等。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM