How to find the src attribute of a <FRAME> element in Visual C++ .NET or in Visual C++ 2005 (815720)
The information in this article applies to:
- Microsoft Visual C++ 2005 Express Edition
- Microsoft Visual C++ .NET (2003)
- Microsoft .NET Framework 1.1
For a Microsoft Visual Basic .NET version of this article, see 311292.
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 TASKSUMMARYThis article contains a way to programmatically find the src
attribute of a <FRAME> element of a Web document by using Microsoft Visual C++ .NET 2003 or Microsoft Visual C++ 2005 when a Windows Forms Application project hosts a Microsoft WebBrowser control. This article describes how to add references to the Microsoft HTML Object
Library and to the Microsoft Internet Controls and describes how to use their programming models inside
the code. 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
This
article assumes that you are familiar with the following topics:
- Programming with Microsoft Visual C++ .NET 2003 or Microsoft Visual C++ 2005
- Hosting WebBrowser controls (Shdocvw.dll)
back to the topHost the WebBrowser control To add a WebBrowser control to a Windows Forms Application project, follow these steps. Create a Windows Forms Application project- Start Microsoft Visual Studio .NET 2003 or Microsoft Visual C++ 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. Under Templates, click Windows
Forms Application (.NET).
Note In Visual Studio 2005, under Project Types, click Visual
C++. Under Templates, click Windows
Forms Application. - In the Name box, type
Framesrc, and then click
OK. By default, the Form1 form is created.
back to the topAdd the WebBrowser control- In Solution Explorer, right-click the
Framesrc node, and then click Add
Reference.
- Click the COM tab in the Add
Reference dialog box.
Note In Visual C++ 2005, Add
Reference is changed to Reference. - In the list of Microsoft Component Object Model (COM) components, double-click Microsoft HTML Object Library, double-click Microsoft Internet
Controls, and then click OK.
- In the Toolbox, click the Windows Forms
tab.
- Right-click the Toolbox, and then click Add/Remove
Items.
Note In Visual C++ 2005, Add/Remove
Items is changed to Choose Items. - In the
Customize Toolbox dialog box, click the COM Components tab.
- Browse the list of COM components until you find a component
that is named Microsoft Web Browser. This component points to the Shdocvw.dll file that is 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 of the Toolbox. This control is the WebBrowser control that you can
add to the form. - Add a Microsoft Web
Browser control to Form1.
- Resize Form1 and the Web Browser
control.
- Add a Button control to
Form1.
- Double-click Form1.
This action creates a Form1_Load event handler in the
Form1 class and positions the cursor at the first line of the event
handler. - Add the following code to the Form1_Load
event handler:
// Declare objects to hold the URL string and a NULL value.
Object * myurl = S"http://msdn.microsoft.com/library";
Object *nullobj = NULL;
// Navigate to a Web page that contains different frames.
axWebBrowser1->Navigate2(&myurl,&nullobj,&nullobj,&nullobj,&nullobj); - Press CTRL+SHIFT+B to build the
application.
- Press CTRL+F5 to start the
application.
Form1 appears, and the Web Browser control displays
the Web page. back to the topFind the src attribute for each <FRAME> elementTo find the src attribute of each <FRAME> element inside the
Web document, follow these steps:
- Switch to the Form1 Design mode.
- Double-click the button1 control on
Form1.
This action creates a
button1_Click event handler inside the Form1 class, and positions the
cursor at the first line of the event handler. - Add the following code to the
button1_Click event handler:
// Declare the document and Element Collection variables.
mshtml::IHTMLDocument2 *pDoc = NULL;
mshtml::IHTMLElementCollection *pColl = NULL;
Object *nullObject = NULL;
int x;
// Get the document pointer that is loaded in the WebBrowser control.
pDoc = static_cast<mshtml::IHTMLDocument2*>(axWebBrowser1->Document);
// Get a pointer to all Element Collections.
pColl = pDoc->get_all();
// Loop through the Element Collection.
int y = pColl->length;
for(x = 0; x < y ; x++)
{
// Read the tag name of each element in the Element Collection.
Object *objNum = __box(x);
String * str = __try_cast<String*>( __try_cast<mshtml::IHTMLElement*>(pDoc->get_all()->item(objNum,nullObject))->get_tagName());
// Check whether it is a <FRAME> tag.
if(String::Compare(str,"FRAME")==0)
{
// Put the value of the src property into a string.
String *ss = __try_cast<String*>( __try_cast<mshtml::IHTMLFrameBase*>(pDoc->get_all()->item(objNum,nullObject))->get_src());
MessageBox::Show(ss);
}
} Note You must add the common language runtime support compiler option (/clr:oldSyntax) in
Visual C++ 2005 to successfully compile the previous code sample.
To add the common language runtime support compiler option in Visual C++ 2005, follow these steps:
- Click Project, and then click <ProjectName> Properties.
Note <ProjectName> is a placeholder for the
name of the project. - Expand Configuration Properties, and then click
General.
- Click to select Common Language Runtime Support, Old Syntax (/clr:oldSyntax) in the
Common Language Runtime support project setting in the right pane, click Apply, and then
click OK.
For more information about the common language runtime support compiler option, visit the following Microsoft Web site: - Press CTRL+SHIFT+S to save the
project.
- Press CTRL+SHIFT+B to build the
application.
- Press CTRL+F5 to start the
application.
- After a frameset page is loaded in the WebBrowser
control, click the button1 button in the
Form1 form to see the src attributes of the different <FRAME>
elements of the Web document that was loaded.
Note The src attribute values that you see are based on the parent frameset
page. If any one of the src attribute values is changed dynamically by client-side or
by server-side script, you will not see these changes by using this
method. back to the topTroubleshootingMake
sure that you have added the Microsoft HTML Object Library and the Microsoft Internet
Controls references to the project. For more information, visit the " Host the WebBrowser control" section of this article. back to the topREFERENCESFor more information about MSHTML, visit the following Microsoft Developer Network (MSDN) Web site: back to the top
Modification Type: | Major | Last Reviewed: | 1/18/2006 |
---|
Keywords: | kbWindowsForms kbWebBrowser kbhtml kbCOMInterop kbCollections kbinterop kbHOWTOmaster KB815720 kbAudDeveloper |
---|
|
|
©2004 Microsoft Corporation. All rights reserved.
|
|