Two ways to access a multilayer nested frame

Source: Internet
Author: User
Tags bool interface return window
Access

========================================

I read about the use of Twebbrowser on your website, but there is always a problem that bothers me is how to get the original code of the HTML of frame nested frame, I just know how to get the source of a single frame, but there is no way for multiple frame nesting, please ask!

Answer ========================================

To get the source code, you must first get the frame. There are generally two ways to access a frame:

1, through the WebBrowser of the document interface to get a set of frame, and then access.

HRESULT ihtmldocument2::get_frames (IHTMLFramesCollection2 **p);

The Item method of the IHTMLFramesCollection2 interface can access the corresponding frame with the index number of the frame (starting at 0) or the name of the frame. Pvarresult returns a IDispatch interface (or an array of IDispatch interfaces, multiple nested cases).

HRESULT Item (
VARIANT *pvarindex,
VARIANT *pvarresult
);

The example below assumes that Pwin is a valid Ihtmlwindow interface pointer to the main window.

......
VARIANT framerequested;
VARIANT frameout;
Ihtmlframescollection2* Pframescol;
Ihtmlwindow2* Prightframewindow;
Ihtmldocument2* Prightdoc;

FRAMEREQUESTED.VT = vt_bstr;//If VT_I4 is accessed by index number
Framerequested.bstrval = L "Rightframe";//access by name
FRAMEREQUESTED.VT = VT_I4;
Framerequested.bstrval = (BSTR) 0;

hr = Pwin->get_frames (&pframescol);
hr = Pframescol->item (&framerequested, &frameout);

hr = Frameout.pdispval->queryinterface (Iid_ihtmlwindow2, (void**) &prightframewindow);
hr = Prightframewindow->get_document (&prightdoc);
......

2, the WebBrowser object is accessed by IOleContainer enumeration of embedded objects.

void Cmyhtmlview::refreshframes ()
{
Get IDispatch pointer to document
Lpdispatch lpdisp = NULL;
Lpdisp = Gethtmldocument ();

if (LPDISP)
{
Iolecontainer* Pcontainer;
HRESULT hr = Lpdisp->queryinterface (Iid_iolecontainer, (void**) &pcontainer);
Lpdisp->release ();
if (FAILED (HR))
return HR;

ienumunknown* Penumerator;
Get Enumerator
hr = Pcontainer->enumobjects (olecontf_embeddings, &penumerator);
Pcontainer->release ();
if (FAILED (HR))
return HR;

iunknown* PUnk;
ULONG ufetched;
Enumerate and Refresh all frame
for (UINT i = 0; S_OK = = Penumerator->next (1, &punk, &ufetched); i++)
{
Iwebbrowser2* Pbrowser;

hr = Punk->queryinterface (Iid_iwebbrowser2, (void**) &pbrowser);
Punk->release ();
if (SUCCEEDED (HR))
{
Pbrowser->refresh ();
Pbrowser->release ();
}
}
Penumerator->release ();
}

3, access to multi-layer nested frame

Note that each frame can contain its own frame, and the method described above is implemented for a WebBrowser window and does not involve a deep frame. To achieve access to a multi-tiered nested frame, you just need to add a little recursive operation. As for the Prightframewindow in 1 and the Pbrowser in 2, the function is slightly modified to be called recursively after two pointers are obtained.

4. Access to source code

The following approach comes from CHtmlView, which is a more formal approach (to keep the original format of the page).

BOOL Chtmlview::getsource (cstring& refstring)
{
BOOL bretval = FALSE;
Ccomptr<idispatch> spdisp = Gethtmldocument ();

  if (spdisp!= NULL)
  {
    hglobal hmemory;
    hmemory = GlobalAlloc (gmem_moveable, 0);
    if (hmemory!= NULL)
    {
      ccomqiptr< ipersiststreaminit> sppersiststream = spdisp;
      if (sppersiststream!= NULL)
      {
         ccomptr<istream> Spstream;
        if (SUCCEEDED (CreateStreamOnHGlobal (Hmemory, TRUE, &spstream))
        {
          Sppersiststream->save (Spstream, FALSE);

          LPCTSTR pstr = (LPCTSTR) globallock (hmemory);
          if (pstr!= NULL)
           {
           //Stream is Always ANSI, but CString
           //Assignment Operator'll convert implicitly.

            bretval = TRUE;
            TRY
             {
               refstring = pstr;
           }
            Catch_all (e)
             {
               bretval = FALSE;
              delete_exception (e);
           }
            End_catch_all

if (Bretval = = FALSE)
GlobalFree (hmemory);
Else
GlobalUnlock (hmemory);
}
}
}
}
}

return bretval;
}



Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.