开发者

Scroll to top of WebBrowser control

开发者 https://www.devze.com 2023-02-14 10:07 出处:网络
How can you scroll to the top of a web broswercontrol. The page I am loading has an iframe and the scr开发者_StackOverflow中文版oll bar is starting 20px down. It only happens in my application. I woul

How can you scroll to the top of a web broswer control. The page I am loading has an iframe and the scr开发者_StackOverflow中文版oll bar is starting 20px down. It only happens in my application. I would like to auto scroll to the top.


Quick search yields: webBrowser1.Document.Window.ScrollTo(0, 200);


If what you mean is wanting to scroll the content of the iframe to the top, the following should help.

First you will need 2 things:

  1. Add reference to C:\Program Files\Microsoft.NET\Primary Interop Assemblies\Microsoft.mshtml.dll
  2. edit your <iframe> tag so that it has an id, eg: id="something"

Finally, the code:

HtmlElement ele = webBrowser1.Document.GetElementById("something");
mshtml.HTMLIFrameClass frame = ele.DomElement as mshtml.HTMLIFrameClass;
if (frame != null)
{
    mshtml.HTMLDocumentClass doc = frame.document as mshtml.HTMLDocumentClass;
    if (doc != null)
    {
        object i = 0;
        mshtml.HTMLWindow2Class win = doc.frames.item(ref i) as mshtml.HTMLWindow2Class;
        if(win != null)
            win.scrollTo(0, 0);
    }
}
0

精彩评论

暂无评论...
验证码 换一张
取 消