Solved

Resize a WebBrowser control based on the Website page

Posted on 2014-01-24
4
6,655 Views
Last Modified: 2014-02-08
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
Comment
Question by:davidst98
  • 2
4 Comments
 
LVL 18

Expert Comment

by:Matthew Kelly
ID: 39807863
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
 

Author Comment

by:davidst98
ID: 39808029
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
 
LVL 12

Expert Comment

by:deanvanrooyen
ID: 39808035
0
 
LVL 18

Accepted Solution

by:
Matthew Kelly earned 500 total points
ID: 39808119
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

Space-Age Communications Transitions to DevOps

ViaSat, a global provider of satellite and wireless communications, securely connects businesses, governments, and organizations to the Internet. Learn how ViaSat’s Network Solutions Engineer, drove the transition from a traditional network support to a DevOps-centric model.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

In my previous article (http://www.experts-exchange.com/Programming/Languages/.NET/.NET_Framework_3.x/A_4362-Serialization-in-NET-1.html) we saw the basics of serialization and how types/objects can be serialized to Binary format. In this blog we wi…
A theme is a collection of property settings that allow you to define the look of pages and controls, and then apply the look consistently across pages in an application. Themes can be made up of a set of elements: skins, style sheets, images, and o…
This is Part 3 in a 3-part series on Experts Exchange to discuss error handling in VBA code written for Excel. Part 1 of this series discussed basic error handling code using VBA. http://www.experts-exchange.com/videos/1478/Excel-Error-Handlin…
Although Jacob Bernoulli (1654-1705) has been credited as the creator of "Binomial Distribution Table", Gottfried Leibniz (1646-1716) did his dissertation on the subject in 1666; Leibniz you may recall is the co-inventor of "Calculus" and beat Isaac…

820 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question