How to obtain the width value and the height value from the Window.open method of a WebBrowser control by using Visual C++ .NET or Visual C++ 2005 (815719)
The information in this article applies to:
- Microsoft Visual C++ .NET (2003)
- Microsoft .NET Framework 1.1
- Microsoft Visual C++ 2005 Express Edition
For a Microsoft Visual Basic .NET version of this
article, see
311290. For a Microsoft Visual C# .NET version of this
article, see
313966. This article refers to the following
Microsoft .NET Framework Class Library namespaces:
- System::ComponentModel
- System::Collections
- System::Windows::Forms
- System::Windows::Forms::AxHost
IN THIS TASKSUMMARYIn a Microsoft Visual C++ .NET or Microsoft Visual C++ .NET application, the WebBrowser
control navigates to an .html page that calls the window.open method to open a new window. The WebBrowser control can handle
the NewWindow2 event to catch the window.open call that the script generates. Sometimes, you may want
to resize the width and the height of the application window according to the
width and height values that are passed to the features argument of the window.open method in the script. This step-by-step article describes how to obtain the new
width and height values of the WebBrowser control. It also describes how to resize your form
by using these values. back to the
topRequirementsThe
following list outlines the recommended hardware, software, network
infrastructure, and service packs that you need: - Microsoft Visual C++ .NET 2003 or Microsoft Visual C++ 2005
- Microsoft .NET Framework 1.1
- Microsoft Internet Explorer 5.5 Service Pack 2 or later
This
article assumes that you are familiar with the following topics:
- Programming by using Visual C++ .NET 2003 or Microsoft Visual C++ 2005
- Hosting a WebBrowser control (Shdocvw.dll)
back to the
topCreate the sample This section describes how to host the WebBrowser control in a
Visual C++ .NET application, how to handle the NewWindow2 event of the WebBrowser control, and how to handle the WindowSetWidth and the WindowSetHeight events to resize your application window. To do this,
follow these steps:
- Start Microsoft Visual Studio .NET 2003 or Microsoft Visual Studio 2005.
- On the File menu, point to
New, and then click Project.
The
New Project dialog box appears. - Under Project Types, click Visual
C++ Projects.
Note In Microsoft Visual Studio 2005, click Visual
C++ under Project Types. - Under Templates, click Windows
Forms Application (.NET).
- In the Name box, type
ResizeForm, and then click
OK.
By default, the Form1 form is
created. - In the toolbox, click the Windows Forms
tab.
- Right-click the toolbox, and then click Add/Remove
Items.
The Customize Toolbox dialog box appears. - In the
Customize Toolbox dialog box, click the COM Components tab.
Note In Microsoft Visual Studio 2005, right-click the toolbox, and then click Choose Items. - In the COM components list, locate the component
that is named Microsoft Web Browser that points to Shdocvw.dll in your
System32 folder.
- Click to select the Microsoft Web Browser
check box, and then click OK.
A new control that is named
Microsoft Web Browser appears on the Windows
Forms tab in the toolbox. This is the WebBrowser control that you can
add to the form. - Double-click the Microsoft Web
Browser control to add a Microsoft Web Browser
control to the Form1 form.
- Resize the Form1 form and the WebBrowser
control.
- Double-click the Button
control to add a Button control to the Form1 form.
- Double-click the TextBox
control to add a TextBox control to the Form1 form.
- Double-click the button1Button control on
the Form1 form.
This step creates a
button1_Click event handler in the Form1 class and puts the insertion point at the first line of the event
handler. - Add the following code in the
button1_Click event handler.
Object *Nullobj = NULL;
Object *url = textBox1->Text;
axWebBrowser1->Navigate2(&url, &Nullobj,&Nullobj, &Nullobj,&Nullobj); - In Solution Explorer, right-click the Form1.h
node in the Header Files folder, and then click View
Designer.
- Right-click the WebBrowser control on the form, and then
click Properties.
- In the Properties window, click Events.
- Double-click NewWindow2 in the list of
events.
This step creates an axWebBrowser1_NewWindow2
event handler in the Form1 class and puts the insertion point at the first line of the event
handler. - Add the following code in the
axWebBrowser1_NewWindow2 event handler.
Form1 * newWindow = new Form1();
newWindow->Show();
newWindow->axWebBrowser1->RegisterAsBrowser = true;
e->ppDisp = newWindow->axWebBrowser1->Application;
newWindow->Visible = true; - Repeat steps 17 through 20 to add the
WindowSetHeight event handler in the Form1 class, and then add the following code in
the axWebBrowser1_WindowSetHeight event handler.
int heightDiff;
heightDiff = this->Height - this->axWebBrowser1->Height;
this->Height = heightDiff + e->height; - Repeat steps 17 through 20 to add the
WindowSetWidth event handler in the Form1 class, and then add the following code in
the axWebBrowser1_WindowSetWidth event handler.
int widthDiff;
widthDiff = this->Width - this->axWebBrowser1->Width;
this->Width = widthDiff + e->width; - Press CTRL+SHIFT+S to save the
project.
back to the
topVerify that the application works- Press CTRL+SHIFT+B to build the
solution.
- Start Notepad, and then save the following code as the Test.htm file on your Web server.
<HTML>
<HEAD>
<META NAME="GENERATOR" Content="Microsoft Visual Studio 6.0">
<TITLE></TITLE>
<script>
function openWin()
{
var win;
win = window.open("http://www.microsoft.com","blah","width=600, height=600");
}
</script>
</HEAD>
<BODY>
<button onClick=openWin()>Open Window</button>
</BODY>
</HTML> - Press CTRL+F5 to run the
program.
- On the Form1 page that appears, change the
text in the text box to the URL of the Test.htm file.
- Click button1.
The WebBrowser
control displays the Test.htm file as a Web page. - Click the Open Window button that appears in the WebBrowser control.
Notice that the Microsoft
Corporate Web site opens in a new instance of the application. The form is
resized according to the features that you passed with the call to the window.open method. back to the
topREFERENCESFor more information about the WebBrowser control and the
methods, the properties, and the events that the WebBrowser control exposes, visit the following Microsoft Developer Network (MSDN) Web
site:
For additional information, click the following article number to view the article in the Microsoft Knowledge Base:
259963
HOWTO: Obtain width and height supplied to window.open inside the Visual C++ WebBrowser host
311284 HOW TO: Handle document events in a Visual Basic .NET application
back to the
top
Modification Type: | Major | Last Reviewed: | 4/21/2006 |
---|
Keywords: | kbhtml kbWindowsForms kbWebBrowser kbForms kbCollections kbweb kbNavigation kbHOWTOmaster KB815719 kbAudDeveloper |
---|
|