检测WebBrowser控件中的滚动到底部 [英] Detect scroll to bottom in WebBrowser control
问题描述
我正在创建一个Windows表单来接受公司的一些条款和条件。因此,条款和条件在Web上,并通过WebBrowser控件导航到WinForm。只有在将完整文档滚动到底部后才需要启用接受按钮。我正在搜索类似于VScrollBar控件中的ValueChanged事件(如下所述)或任何其他选项的事件。
I am creating a Windows Form to accept some terms and conditions for a company. So the terms and conditions are on the web and it is navigated to the WinForm through WebBrowser control. It is required to enable the Accept button only after the full document is scrolled to the bottom. I am searching for an Event similar to ValueChanged Event in VScrollBar control(mentioned below) or any other option.
private void vScrollBar1_ValueChanged(object sender, EventArgs e)
{
if (vScrollBar1.Value+9 == vScrollBar1.Maximum)
{
acceptBtn.Enabled = true;
}
}
推荐答案
你应该处理 onscroll
窗口
对象事件并检查 scrollHeight - scrollTop
等于 clientHeight
for documentElement
。为此:
You should handle onscroll
event of window
object and check if scrollHeight - scrollTop
equals to clientHeight
for documentElement
. To do so:
private void webBrowser1_DocumentCompleted(object sender,
WebBrowserDocumentCompletedEventArgs e)
{
this.webBrowser1.Document.Window.AttachEventHandler("onscroll", OnScroll);
}
void OnScroll(object sender, EventArgs e)
{
var script =
@"(function()
{
var e = document.documentElement;
if (e.scrollHeight - e.scrollTop === e.clientHeight)
return true;
else
return false;
})();";
var result = webBrowser1.Document.InvokeScript("eval", new object[] { script });
if ((bool)result)
MessageBox.Show("Scrolled to end!");
}
这篇关于检测WebBrowser控件中的滚动到底部的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!