SUMMARY
This step-by-step article shows you how to get the new width and height of the WebBrowser control and how to resize your form accordingly.
In a Visual C# .NET application, WebBrowser control navigates to an .html page that calls the
window.open method to open a new window. In certain situation, developers want to resize the width and the height of the application according to the size of the new window. To do so, you can obtain the values that are passed by the width and height argument of
window.open in the
NewWindow2 event.
back to the top
Requirements
The following list outlines the recommended hardware, software, network
infrastructure, and service packs that you need for this procedure:
- Microsoft Visual Studio .NET
- Microsoft Internet Explorer 5.5 Service Pack 2 or later
back to the top
Description of Technique
You need to add a reference to Microsoft HTML Object Library (MSHTML) in your project. You have to handle the
WindowSetWidth and
WindowSetHeight events that are exposed by the
Mshtml.HTMLDocumentEvents2_WindowSetWidthEvent and
Mshtml.HTMLDocumentEvents2_WindowSetHeightEvent interfaces respectively.
In your event handler functions, the new width and height of the WebBrowser control are passed in through the second parameter; you need to retrieve the new values and to resize your form to the appropriate width and height.
back to the top
Code Sample
public Form1()
{
// Required for Windows Form Designer support
InitializeComponent();
// Webbrowser Event handler
this.axWebBrowser1.WindowSetHeight += new AxSHDocVw.DWebBrowserEvents2_WindowSetHeightEventHandler(this.axWebBrowser1_WindowSetHeight);
this.axWebBrowser1.WindowSetWidth += new AxSHDocVw.DWebBrowserEvents2_WindowSetWidthEventHandler(this.axWebBrowser1_WindowSetWidth);
this.axWebBrowser1.NewWindow2 += new AxSHDocVw.DWebBrowserEvents2_NewWindow2EventHandler(this.axWebBrowser1_NewWindow2);
}
private void axWebBrowser1_WindowSetHeight(object sender, AxSHDocVw.DWebBrowserEvents2_WindowSetHeightEvent e)
{
int heightDiff;
heightDiff = this.Height - this.axWebBrowser1.Height;
this.Height = heightDiff + e.height;
}
private void axWebBrowser1_WindowSetWidth(object sender, AxSHDocVw.DWebBrowserEvents2_WindowSetWidthEvent e)
{
int widthDiff;
widthDiff = this.Width - this.axWebBrowser1.Width;
this.Width = widthDiff + e.width;
}
private void axWebBrowser1_NewWindow2(object sender, AxSHDocVw.DWebBrowserEvents2_NewWindow2Event e)
{
Form1 newWindow = new Form1();
newWindow.Show();
newWindow.axWebBrowser1.RegisterAsBrowser = true;
e.ppDisp = newWindow.axWebBrowser1.Application;
}
back to the top
REFERENCES
For additional information, click the article number below
to view the article in the Microsoft Knowledge Base:
311284 How To Handle document events in a VB.Net application
For more information about the WebBrowser control and the methods, properties, and events that it exposes, see the WebBrowser documentation on the following Microsoft Web site:
For more information about how to develop Web-based solutions for Internet Explorer, please see the following Microsoft Web sites:
back to the top