========================================
在您的網(wǎng)站拜讀了關(guān)于twebbrowser的使用方法,但是一直有一個問題困擾我,就是如何取得frame嵌套frame的html的原碼,我只是知道單個frame如何取得源碼,但是多個frame嵌套就沒有辦法,請教一下!
答========================================
要得到源代碼,必須先得到frame。訪問frame一般說來有兩種方法:
1、通過webbrowser的文檔接口得到frame的集合,再逐一訪問。
hresult ihtmldocument2::get_frames(ihtmlframescollection2 **p);
由ihtmlframescollection2接口的item方法,可以以frame的索引號(從0開始)或frame的名稱來訪問相應(yīng)的frame,pvarresult則返回一個idispatch接口(或一個idispatch接口的數(shù)組,多層嵌套的情況).
hresult item(
variant *pvarindex,
variant *pvarresult
);
例子如下,假設(shè)pwin是一個指向主窗口的有效的ihtmlwindow接口指針。
......
variant framerequested;
variant frameout;
ihtmlframescollection2* pframescol;
ihtmlwindow2* prightframewindow;
ihtmldocument2* prightdoc;
framerequested.vt = vt_bstr;//若為vt_i4則以索引號來訪問
framerequested.bstrval = l"rightframe";//以名稱來訪問
//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、通過iolecontainer枚舉嵌入對象的方式來訪問webbrowser對象。
void cmyhtmlview::refreshframes()
{
// 取得文檔的idispatch指針
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;
// 獲得枚舉器
hr = pcontainer->enumobjects(olecontf_embeddings, &penumerator);
pcontainer->release();
if (failed(hr))
return hr;
iunknown* punk;
ulong ufetched;
// 枚舉并刷新所有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、訪問的多層嵌套frame
注意每個frame又可以包含自己的frame,而上面所說的方法則是針對一個webbrowser的窗口實現(xiàn)的,并不會涉及到深層的frame。要實現(xiàn)多層嵌套frame的訪問,只需要加入一點遞歸的操作就行了。如對1中的prightframewindow和2中的pbrowser,將函數(shù)稍加修改,在得到兩個指針后作遞歸調(diào)用即可。
4、訪問源代碼
下面的方法來自chtmlview,是比較正規(guī)的方法(能夠保持網(wǎng)頁的原始格式)。
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 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);
}
}
}
}
}
return bretval;
}
新聞熱點
疑難解答
圖片精選