【发布时间】:2015-08-03 17:33:09
【问题描述】:
我有一个使用默认 WebBrowser 控件 (System.Windows.Controls.WebBrowser) 的 Windows WPF 应用程序。 我需要拦截浏览器上发生的 MouseMove 事件。为了订阅该事件,我使用以下代码片段(这些是包装 WebBrowser 的 UserControl 的方法):
public void HookInputElementForKeyboard()
{
HTMLDocument htmlDocument = (HTMLDocument)webBrowserControl.Document;
htmlDocument.attachEvent("oncontextmenu", new ContextMenuDisablerEventHandler());
HTMLDocumentEvents_Event documentEvents = htmlDocument as HTMLDocumentEvents_Event;
documentEvents.onmousemove += DocumentOnMouseMove;
}
private void DocumentOnMouseMove()
{
HTMLDocument document = webBrowserControl.Document as HTMLDocument;
var window = document.parentWindow as IHTMLWindow2;
var currentEvent = window.@event;
MouseMoveOnDocumentEventArgs args = new MouseMoveOnDocumentEventArgs(currentEvent.clientX, currentEvent.clientX);
var now = DateTime.Now;
logger.Debug(now.Second + "." + now.Millisecond + ": " + args.ToString());
// fires the event of the wrapping UserControl to notify the MouseMove to external clients
OnMouseMoveOnDocument(args);
}
HookInputElementForKeyboard 在 WebBrowser 的 LoadCompleted 事件上被调用。
问题是在看似随机的时间(通常是几秒钟)之后,回调DocumentOnMouseMove 停止被调用。
这是我的日志的摘录,带有事件的时间戳(格式为 <timestampSeconds>: [<clientX>; <clientY>]):
2015-07-14 10:30:22,005 [8] DEBUG Common.GUI.UserControls.WebBrowserView [(null)] - onmousemove event subscribed at 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 now on, the MouseMove event is not captured any more, even if I keep moving the pointer]
在应用程序日志和事件查看器中都没有跟踪到错误。
有人知道导致这种行为的原因是什么吗?有什么可能的解决方法吗?
【问题讨论】:
标签: c# wpf webbrowser-control activex