c# - onmousemove listener on WPF WebBrowser control stops working after few seconds -


i have windows wpf application uses default webbrowser control (system.windows.controls.webbrowser). have need intercept mousemove events happen on browser. in order subscribe event use following code fragment (these methods of usercontrol wraps webbrowser):

public void hookinputelementforkeyboard() {     htmldocument htmldocument = (htmldocument)webbrowsercontrol.document;      htmldocument.attachevent("oncontextmenu", new contextmenudisablereventhandler());     htmldocumentevents_event documentevents = htmldocument htmldocumentevents_event;     documentevents.onmousemove += documentonmousemove; }  private void documentonmousemove() {     htmldocument document = webbrowsercontrol.document htmldocument;     var window = document.parentwindow ihtmlwindow2;     var currentevent = window.@event;     mousemoveondocumenteventargs args = new mousemoveondocumenteventargs(currentevent.clientx, currentevent.clientx);     var = datetime.now;     logger.debug(now.second + "." + now.millisecond + ": " + args.tostring());     // fires event of wrapping usercontrol notify mousemove external clients     onmousemoveondocument(args); } 

hookinputelementforkeyboard gets called on loadcompleted event of webbrowser. problem after seemingly random amount of time (usually seconds), callback documentonmousemove stops being called. extract of log timestamps of events (format <timestampseconds>: [<clientx>; <clienty>]):

2015-07-14 10:30:22,005 [8] debug common.gui.usercontrols.webbrowserview [(null)] - onmousemove event subscribed @ 22.5 [some other unrelated logs] 2015-07-14 10:30:22,568 [8] debug common.gui.usercontrols.webbrowserview [(null)] - 22.568: [4; 4] 2015-07-14 10:30:22,584 [8] debug common.gui.usercontrols.webbrowserview [(null)] - 22.584: [15; 15] 2015-07-14 10:30:22,584 [8] debug common.gui.usercontrols.webbrowserview [(null)] - 22.584: [24; 24] [...] 2015-07-14 10:30:22,599 [8] debug common.gui.usercontrols.webbrowserview [(null)] - 22.599: [33; 33] 2015-07-14 10:30:22,599 [8] debug common.gui.usercontrols.webbrowserview [(null)] - 22.599: [45; 45] 2015-07-14 10:30:22,615 [8] debug common.gui.usercontrols.webbrowserview [(null)] - 22.615: [63; 63] 2015-07-14 10:30:22,615 [8] debug common.gui.usercontrols.webbrowserview [(null)] - 22.615: [92; 92] [...] 2015-07-14 10:30:23,849 [8] debug common.gui.usercontrols.webbrowserview [(null)] - 23.849: [565; 565] 2015-07-14 10:30:23,849 [8] debug common.gui.usercontrols.webbrowserview [(null)] - 23.849: [571; 571] 2015-07-14 10:30:23,865 [8] debug common.gui.usercontrols.webbrowserview [(null)] - 23.865: [580; 580] 2015-07-14 10:30:23,865 [8] debug common.gui.usercontrols.webbrowserview [(null)] - 23.865: [587; 587] [from on, mousemove event not captured more, if keep moving pointer] 

there no error traced in application log nor in event viewer.

anybody knows cause of behaviour? there possible workaround?

just found way implement behavior , works. still not know why. solution might seem bit convoluted, it's way found far. first, define class act collector of mousemove event generated webbrowser control. called class dommousemoveeventmanager:

 [comvisible(true)]  [classinterface(classinterfacetype.autodispatch)]  public class dommousemoveeventmanager  {      public event dommousemoveeventhandler dommousemove;      [dispid(0)]      public void callbackfunction(mshtml.ihtmleventobj arg)      {          //console.writeline(string.format("[{0}, {1}]", arg.clientx, arg.clienty));          ondommousemove(arg.clientx, arg.clienty);      }       private void ondommousemove(int clientx, int clienty)      {          if(dommousemove != null)          {              var args = new dommousemoveeventargs(clientx, clienty);              dommousemove(this, args);          }      }  } 

note class has event called dommousemove: use event instead of direct listener in client class. had:

  private void hookonmousemove()   {       var document = webbrowsercontrol.document htmldocument;       var documentevents = document htmldocumentevents_event;       documentevents.onmousemove += documentevents_onmousemove;   } 

now register event using member of type dommousemoveeventmanager, this:

  private dommousemoveeventmanager mousemovemanager;   private void hookonmousemove()   {       var document = webbrowsercontrol.document htmldocument;       document.attachevent("onmousemove", mousemovemanager);   } 

obviously, need event args class:

  public class dommousemoveeventargs : routedeventargs   {       public int clientx { get; set; }       public int clienty { get; set; }        public dommousemoveeventargs(int clientx, int clienty)       {           clientx = clientx;           clienty = clienty;       }   } 

and, in order complete picture, how attach client class dommousemoveeventmanager:

// constructor of client class must notified mousemove event   public webbrowseradapter()   {       initializecomponent();       this.loaded += webbrowseradapter_loaded;       mousemovemanager = new dommousemoveeventmanager();       mousemovemanager.dommousemove += mousemovemanager_dommousemove;   }    void mousemovemanager_dommousemove(object source, dommousemoveeventargs args)   {       diagnosticblock.text = "[" + args.clientx + "; " + args.clienty + "]";       ondommousemove(args.clientx, args.clienty);   } 

Comments

Popular posts from this blog

javascript - Using jquery append to add option values into a select element not working -

Android soft keyboard reverts to default keyboard on orientation change -

Rendering JButton to get the JCheckBox behavior in a JTable by using images does not update my table -