Microsoft KB Archive/815720

= How to find the src attribute of a  element in Visual C++ .NET or in Visual C++ 2005 =

Article ID: 815720

Article Last Modified on 5/12/2007

-

APPLIES TO


 * Microsoft Visual C++ 2005 Express Edition
 * Microsoft Visual C++ .NET 2003 Standard Edition
 * 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 TASK

 * SUMMARY
 * Requirements
 * Host the WebBrowser control
 * Create a Windows Forms Application (.NET) project
 * Add the WebBrowser control
 * Find the src attribute for each  element
 * Troubleshooting
 * REFERENCES



SUMMARY
This article contains a way to programmatically find the src attribute of a  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 top

Requirements
The 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 top

Host the WebBrowser control
To add a WebBrowser control to a Windows Forms Application project, follow these steps.

Create a Windows Forms Application project

 * 1) Start Microsoft Visual Studio .NET 2003 or Microsoft Visual C++ 2005.
 * 2) On the File menu, point to New, and then click Project. The New Project dialog box appears.
 * 3) 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.
 * 1) In the Name box, type Framesrc, and then click OK. By default, the Form1 form is created.

back to the top

Add 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.</li> In the Customize Toolbox dialog box, click the COM Components tab.</li> 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.</li> 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.</li> Add a Microsoft Web Browser control to Form1.</li> Resize Form1 and the Web Browser control.</li> Add a Button control to Form1.</li> 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.</li>  Add the following code to the Form1_Load event handler: // Declare objects to hold the URL string and a NULL value. Object * myurl = S&quot;http://msdn.microsoft.com/library&quot;; Object *nullobj = NULL;

// Navigate to a Web page that contains different frames. axWebBrowser1->Navigate2(&myurl,&nullobj,&nullobj,&nullobj,&nullobj); </li> Press CTRL+SHIFT+B to build the application.</li> Press CTRL+F5 to start the application.

Form1 appears, and the Web Browser control displays the Web page.</li></ol>

back to the top

Find the src attribute for each <FRAME> element
To find the src attribute of each <FRAME> element inside the Web document, follow these steps: <ol> Switch to the Form1 Design mode.</li> 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.</li>  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,&quot;FRAME&quot;)==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: <ol style="list-style-type: lower-alpha;"> Click Project, and then click  Properties.

Note  is a placeholder for the name of the project.</li> Expand Configuration Properties, and then click General.</li> 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.</li></ol>

For more information about the common language runtime support compiler option, visit the following Microsoft Web site:

/clr (Common Language Runtime Compilation)

http://msdn2.microsoft.com/en-us/library/k8d11d4s.aspx

</li> <li>Press CTRL+SHIFT+S to save the project.</li> <li>Press CTRL+SHIFT+B to build the application.</li> <li>Press CTRL+F5 to start the application.</li> <li>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.</li></ol>

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 top

Troubleshooting
Make sure that you have added the Microsoft HTML Object Library and the Microsoft Internet Controls references to the project. For more information, visit the &quot;Host the WebBrowser control&quot; section of this article.

back to the top

<div class="references_section">