如何以编程方式滚动WPF WebBrowser?

时间:2021-07-29 03:42:57

In Windows Forms Applications Im using the following Code to scroll the Page inside a Webbrowser:

在Windows窗体应用程序中我使用以下代码在Web浏览器中滚动页面:

 HtmlDocument doc = webBrowser.Document;
 mshtml.IHTMLDocument2 htmldoc = (mshtml.IHTMLDocument2)doc.DomDocument;
 htmldoc.parentWindow.scrollBy(265, 20);

Does anyone know how to do the same in an WPF Application (without using WindowsFormsHost)?

有没有人知道如何在WPF应用程序中执行相同操作(不使用WindowsFormsHost)?

3 个解决方案

#1


4  

If you're using System.Windows.Controls.WebBrowser class take a look at the Document property. You should be able to cast it to mshtml.HTMLDocument or mshtml.IHTMLDocument2 and code

如果您正在使用System.Windows.Controls.WebBrowser类,请查看Document属性。您应该能够将其强制转换为mshtml.HTMLDocument或mshtml.IHTMLDocument2和代码

mshtml.HTMLDocument htmlDoc = webBrowser.Document as mshtml.HTMLDocument;
if (htmlDoc != null) htmlDoc.parentWindow.scrollBy(265, 20);

should be working fine for you.

应该对你有用。

hope this helps, regards

希望这会有所帮助,问候

#2


1  

you should do it easily :

你应该很容易做到:

    private void WebBrowser_LoadCompleted(object sender, System.Windows.Navigation.NavigationEventArgs e)
    {

        ((WebBrowser)sender).InvokeScript("eval", "$( document ).scrollTop( 1700 );");
    }

#3


0  

The Javascript:

Javascript:

window.onload=toBottom;

在window.onload = toBottom;

function toBottom() { alert("Scrolling to bottom ..."); window.scrollTo(0, document.body.scrollHeight); }

function toBottom(){alert(“Scrolling to bottom ...”); window.scrollTo(0,document.body.scrollHeight); }

The HTML:

HTML:

> <html>
>     <head>
>         <script src="testme.js" language="javascript" type="text/javascript"></script>
>     </head>
>     <body>
>         Some big text<br>
>         Some big text<br>
>         Some big text<br>
>         Some big text<br>
>         Some big text<br>
>         Some big text<br>
>         Some big text<br>
>         Some big text<br>
>         Some big text<br>
>         Some big text<br>
>         Some big text<br>
>         Some big text<br>
>         Some big text<br>
>         Some big text<br>
>         Some big text<br>
>         Some big text<br>
>         Some big text<br>
>         Some big text<br>
>         Some big text<br>
>         Some big text<br>
>         Some big text<br>
>         Some big text<br>
>         Some big text<br>
>         Some big text<br>
>         Some big text<br>
>         Some big text<br>
>         Some big text<br>
>         Some big text<br>           
>     </body>
> </html>

#1


4  

If you're using System.Windows.Controls.WebBrowser class take a look at the Document property. You should be able to cast it to mshtml.HTMLDocument or mshtml.IHTMLDocument2 and code

如果您正在使用System.Windows.Controls.WebBrowser类,请查看Document属性。您应该能够将其强制转换为mshtml.HTMLDocument或mshtml.IHTMLDocument2和代码

mshtml.HTMLDocument htmlDoc = webBrowser.Document as mshtml.HTMLDocument;
if (htmlDoc != null) htmlDoc.parentWindow.scrollBy(265, 20);

should be working fine for you.

应该对你有用。

hope this helps, regards

希望这会有所帮助,问候

#2


1  

you should do it easily :

你应该很容易做到:

    private void WebBrowser_LoadCompleted(object sender, System.Windows.Navigation.NavigationEventArgs e)
    {

        ((WebBrowser)sender).InvokeScript("eval", "$( document ).scrollTop( 1700 );");
    }

#3


0  

The Javascript:

Javascript:

window.onload=toBottom;

在window.onload = toBottom;

function toBottom() { alert("Scrolling to bottom ..."); window.scrollTo(0, document.body.scrollHeight); }

function toBottom(){alert(“Scrolling to bottom ...”); window.scrollTo(0,document.body.scrollHeight); }

The HTML:

HTML:

> <html>
>     <head>
>         <script src="testme.js" language="javascript" type="text/javascript"></script>
>     </head>
>     <body>
>         Some big text<br>
>         Some big text<br>
>         Some big text<br>
>         Some big text<br>
>         Some big text<br>
>         Some big text<br>
>         Some big text<br>
>         Some big text<br>
>         Some big text<br>
>         Some big text<br>
>         Some big text<br>
>         Some big text<br>
>         Some big text<br>
>         Some big text<br>
>         Some big text<br>
>         Some big text<br>
>         Some big text<br>
>         Some big text<br>
>         Some big text<br>
>         Some big text<br>
>         Some big text<br>
>         Some big text<br>
>         Some big text<br>
>         Some big text<br>
>         Some big text<br>
>         Some big text<br>
>         Some big text<br>
>         Some big text<br>           
>     </body>
> </html>