本文介绍了检测卷轴上的WebBrowser控件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我有以下的code表示,古怪,工作了几秒钟,然后停止工作(我的事件处理程序方法不再是所谓的):
公共部分类Form1中:形态
{
私人无效Form1_Load的(对象发件人,EventArgs的)
{
webBrowser1.DocumentCompleted + =新WebBrowserDocumentCompletedEventHandler(webBrowser1_DocumentCompleted);
WebBrowser1.Navigate时(google.com);
}
私人无效webBrowser1_DocumentCompleted(对象发件人,WebBrowserDocumentCompletedEventArgs E)
{
如果(webBrowser1.IsBusy&安培;!&安培; webBrowser1.Url == e.Url和放大器;&安培; webBrowser1.ReadyState == WebBrowserReadyState.Complete)
{
HTMLWindowEvents_Event的WindowEvent = webBrowser1.Document.Window.DomWindow为HTMLWindowEvents_Event;
windowEvents.onscroll + =新HTMLWindowEvents_onscrollEventHandler(windowEvents_onscroll);
}
}
私人无效windowEvents_onscroll()
{
的HTMLDocument HTMLDOC = webBrowser1.Document;
INT scrollTop的= htmlDoc.GetElementsByTagName(HTML)[0] .ScrollTop;
字符串文本= scrollTop.ToString();
}
}
解决方案
确定找到了解决方法:
保护覆盖无效OnDocumentCompleted(WebBrowserDocumentCompletedEventArgs E)
{
遵循();
如果(IsBusy&安培;!&安培; URL == e.Url和放大器;&安培; readyState的== WebBrowserReadyState.Complete)
{
Document.Window.AttachEventHandler(onscroll,DocScroll);
}
}
如果连接的方式,它的工作原理确定(到目前为止...)。甚至不需要使用MSHTML。
I have the following code that, weirdly, works for a couple of seconds and then stops working (my event handler method stops being called):
public partial class Form1 : Form
{
private void Form1_Load(object sender, EventArgs e)
{
webBrowser1.DocumentCompleted += new WebBrowserDocumentCompletedEventHandler(webBrowser1_DocumentCompleted);
webBrowser1.Navigate("google.com");
}
private void webBrowser1_DocumentCompleted(object sender, WebBrowserDocumentCompletedEventArgs e)
{
if (!webBrowser1.IsBusy && webBrowser1.Url == e.Url && webBrowser1.ReadyState == WebBrowserReadyState.Complete)
{
HTMLWindowEvents_Event windowEvents = webBrowser1.Document.Window.DomWindow as HTMLWindowEvents_Event;
windowEvents.onscroll += new HTMLWindowEvents_onscrollEventHandler(windowEvents_onscroll);
}
}
private void windowEvents_onscroll()
{
HtmlDocument htmlDoc = webBrowser1.Document;
int scrollTop = htmlDoc.GetElementsByTagName("HTML")[0].ScrollTop;
string text = scrollTop.ToString();
}
}
解决方案
OK Found a solution:
protected override void OnDocumentCompleted(WebBrowserDocumentCompletedEventArgs e)
{
Follow();
if (!IsBusy && Url == e.Url && ReadyState == WebBrowserReadyState.Complete)
{
Document.Window.AttachEventHandler("onscroll", DocScroll);
}
}
If attached that way it works OK (so far...). Don't even need to use mshtml.
这篇关于检测卷轴上的WebBrowser控件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!