How to open some part of a web page in a form in c#

I am writing a small program (as a training exercise in training): you need to open a web page. In theory, this is quite simple to do:

webBrowser1.Navigate("http://google.com");

But is it possible to somehow control the part that will be displayed in webBrowser1? Let's say I have a webBrowser1 size of 200x200 and I want to display the upper-right square of Google. Is it possible to implement this somehow?

Author: Ella Svetlaya, 2016-02-26

1 answers

You need to subscribe to the DocumentCompleted event, find out the width of the loaded document, the width of the window (note that it is not necessarily equal to the width of the control itself, because for example, the scrollbar can eat up space) and set the scroll position to the desired location:

webBrowser1.DocumentCompleted += webBrowser1_DocumentCompleted;

...

private void webBrowser1_DocumentCompleted(
    object sender,
    WebBrowserDocumentCompletedEventArgs e)
{
    // получаем ширину документа, доступную для скролла
    var documentWidth = webBrowser1.Document.Body.ScrollRectangle.Width;
    // получаем реальную ширину окна документа
    var windowWidth = webBrowser1.Document.Window.Size.Width;
    // устанавливаем скролл в правый верхний угол
    webBrowser1.Document.Window.ScrollTo(documentWidth - windowWidth, 0);
}
 1
Author: andreycha, 2016-02-26 19:12:26