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 TASK

SUMMARY

This 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 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.
  4. 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

  1. In Solution Explorer, right-click the Framesrc node, and then click Add Reference.
  2. Click the COM tab in the Add Reference dialog box.

    Note In Visual C++ 2005, Add Reference is changed to Reference.
  3. 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.
  4. In the Toolbox, click the Windows Forms tab.
  5. Right-click the Toolbox, and then click Add/Remove Items.

    Note In Visual C++ 2005, Add/Remove Items is changed to Choose Items.
  6. In the Customize Toolbox dialog box, click the COM Components tab.
  7. 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.
  8. 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.
  9. Add a Microsoft Web Browser control to Form1.
  10. Resize Form1 and the Web Browser control.
  11. Add a Button control to Form1.
  12. 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.
  13. 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);
  14. Press CTRL+SHIFT+B to build the application.
  15. Press CTRL+F5 to start the application.

    Form1 appears, and the Web Browser control displays the Web page.
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:
  1. Switch to the Form1 Design mode.
  2. 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.
  3. 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:
    1. Click Project, and then click <ProjectName> Properties.

      Note <ProjectName> is a placeholder for the name of the project.
    2. Expand Configuration Properties, and then click General.
    3. 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:

    /clr (Common Language Runtime Compilation)
    http://msdn2.microsoft.com/en-us/library/k8d11d4s.aspx

  4. Press CTRL+SHIFT+S to save the project.
  5. Press CTRL+SHIFT+B to build the application.
  6. Press CTRL+F5 to start the application.
  7. 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 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 "Host the WebBrowser control" section of this article.

back to the top

REFERENCES

For more information about MSHTML, visit the following Microsoft Developer Network (MSDN) Web site:back to the top

Modification Type:MajorLast Reviewed:1/18/2006
Keywords:kbWindowsForms kbWebBrowser kbhtml kbCOMInterop kbCollections kbinterop kbHOWTOmaster KB815720 kbAudDeveloper