FAQ: Two ways access multi-storey nested Frame

xiaoxiao2021-03-06  61

Q======================================== In your website, I read about TWEBBROWSER. How to use, but have always been a problem, how to get the original code of Frame nested Frame, I just know how a single frame gets the source code, but there is no way, but multiple frame nesters have no way, please teach!

2004-09-29 23:41:28 Answ ====================================== = To get the source code, you must first get FRAME. Accessing Frame generally has two methods:

1. Get a collection of Frame by WebBrowser's document interface, and then access one by one.

HRESULT IHTMLDocument2 :: get_frames (htmlframescollection2 ** p);

The ITEM method of the IHTMLFramesCollection2 interface can access the corresponding frame, the corresponding Frame (from 0 start) or the name of the frame, and PVARRESULT returns an IDispatch interface (or an array of iDispatch interfaces, multi-layer nested cases).

HRESULT ITEM (VARIANT * PVARINDEX, VARIANT * PVARRESULT);

The example is as follows, assuming that the 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 you use the index number to access FrameRequested.bstrval = l "RightFrame" in the index number; // to access //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. Access the WebBrowser object by enumerating the embedded object through the IOLCONTAINER.

void CMyHtmlView :: RefreshFrames () {// IDispatch pointer acquired 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 an enumerator HR = PContainer-> enumobjects (OLECONTF_EMBEDDINGS, & PENUMERATOR); PContainer-> Release (); if (Failed (HR)) Return HR;

IUNKNOWN * PUNK; ulong ufetched; // enumerate and refreshes all frame forces (uint i = 0; s_ok == penumerator-> next (1, & punk, & ufetched); i ) {iWebBrowser2 * PBROWSER;

HR = punk-> queryinterface (IID_IWebBrowser2, (void **) & pbrowser; punk-> release (); if (succeededed (hr)) {PBROWSER-> Refresh (); PBROWSER-> Release ();}} penumerator > Release ();

3, multi-layered nested frame for access to each frame can also include its own frame, and the method mentioned above is achieved for a webbrowser window, and does not involve deep Frame. To achieve multiple layers of nested Frame, you only need to add a bit of recursive operation. If PBROWSER in PRIGHTFRAMEWINDOW and 2 in 1, the function is slightly modified, and the recursive call is made after obtaining two pointers. 4. Access the source code below from ChtmlView, is a comparative formal method (which can keep the web page raw format).

Bool Chtmlview :: getSource (cstring & refstring) {bool bretval = false; ccomptr spdisp = gethtmlDocument ();

if (! spDisp = NULL) {HGLOBAL hMemory; hMemory = GlobalAlloc (GMEM_MOVEABLE, 0); if (hMemory = NULL!) {CComQIPtr spPersistStream = spDisp; if (spPersistStream = NULL!) {CComPtr 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 Will 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);}}}}}}}}}}}}}}}}}}}}}}}}}}}}}

Quote Address: FAQ: Two ways access multi-storey nested Frame

转载请注明原文地址:https://www.9cbs.com/read-111525.html

New Post(0)