Go Premium for a chance to win a PS4. Enter to Win

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 7774
  • Last Modified:

Resize a WebBrowser control based on the Website page

Hi,

Is there a way to resize a WebBrowser control (which in turn will resize a WinForm or WPF Window) based on a default size of a webpage.   For example if I click on a link in IE it will bring up a pop with a different size and width then my web browser.   I would like to make a quick app that will directly open this pop and will re-size itself to the webpage.   I hope this makes sense.   Thanks.

David
0
davidst98
Asked:
davidst98
  • 2
1 Solution
 
Matthew KellyCommented:
There is no such thing as a default size of a webpage. You would need to know what content piece you were trying to size to on the page which would require knowing exactly what pages you are navigating to and what elements to look for.

Take this EE page for example. You may say, I want it to size so I see only the right and left hand navigation with everything else on either side of it cut off.

So there is a "bodyWrapper" id DIV you could size to, but that is encapsulated in other div containers that are set to 100%.

Web pages are designed to size to the browser, not the other way around. If your WebBrowser control is only setup to go to webpages you control you could do it by getting the width/height of a container object on the page.
0
 
davidst98Author Commented:
Hi Matthew,  thanks for the response.  I'm a little confused.  How do pop web pages are able to change the size of the web browser and also remove menu and toolbars?

David
0
 
deanvanrooyenCommented:
0
 
Matthew KellyCommented:
Ok, this needs a lot more work, such as setting a maximum width/height etc, but here is a general concept that will resize a webbrowser control to the size of a webpage based on elimination of scroll bar space.

namespace WindowsFormsApplication2
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
            webBrowser1.Navigate(new Uri("http://google.com"));

        }

        private void webBrowser1_DocumentCompleted(object sender, WebBrowserDocumentCompletedEventArgs e)
        {
            if (HasHorizontalScrollbar)
            {
                webBrowser1.Size = new Size(webBrowser1.Document.Body.ScrollRectangle.Width, webBrowser1.Document.Body.ScrollRectangle.Height);
                Form1.ActiveForm.Size = new Size(webBrowser1.Document.Body.ScrollRectangle.Width, webBrowser1.Document.Body.ScrollRectangle.Height);
            }
        }

        public bool HasHorizontalScrollbar
        {
            get
            {
                var width1 = webBrowser1.Document.Body.ScrollRectangle.Width;
                var width2 = webBrowser1.Document.Window.Size.Width;

                return width1 > width2;
            }
        }
    }
}

Open in new window

0

Featured Post

Important Lessons on Recovering from Petya

In their most recent webinar, Skyport Systems explores ways to isolate and protect critical databases to keep the core of your company safe from harm.

  • 2
Tackle projects and never again get stuck behind a technical roadblock.
Join Now