說明了如何在控件中獲得所在窗口的IWebBrowser2的引用。但是,開發(fā)者實(shí)際上經(jīng)常需要的是飽含框架集的頂層IWebBrowser2的引用。例如,當(dāng)你在網(wǎng)頁載入之前調(diào)用statusText() 命令設(shè)置狀態(tài)欄的值時(shí)可以用到。因?yàn)檫@個(gè)屬性對WebBrowser控件無效,所以調(diào)用所在框架的IWebBrowser2的函數(shù)會(huì)產(chǎn)生錯(cuò)誤。
為獲得頂層IWebBrowser2 引用, 從客戶站點(diǎn)獲取IServiceProvider 并且 執(zhí)行一個(gè)QueryService 操作獲取IID_IServiceProvider服務(wù)SID_STopLevelBrowser (在Shlguid.h中定義)。對第二個(gè)IServiceProvider,執(zhí)行一個(gè)QueryService 獲取IID_IWebBrowser2 服務(wù)SID_SWebBrowserApp.
干這個(gè)的最好的地方是在IOleObject的SetClientSite() 方法里面:
#include#define COMRELEASE(ptr)/ if (ptr != NULL) {/ ptr->Release();/ ptr = NULL;/ } IWebBrowser2 *browser = NULL; STDMETHODIMP SetClientSite(IOleClientSite *pClientSite) { HRESULT hr = S_OK; IServiceProvider *isp, *isp2 = NULL; if (!pClientSite) { COMRELEASE(browser); } else { hr = pClientSite->QueryInterface(IID_IServiceProvider, reinterpret_cast (&isp)); if (FAILED(hr)) { hr = S_OK; goto cleanup; } hr = isp->QueryService(SID_STopLevelBrowser, IID_IServiceProvider, reinterpret_cast (&isp2)); if (FAILED(hr)) { hr = S_OK; goto cleanup; } hr = isp2->QueryService(SID_SWebBrowserApp, IID_IWebBrowser2, reinterpret_cast (&browser)); if (FAILED(hr)) { hr = S_OK; goto cleanup; } cleanup: // Free resources. COMRELEASE(isp); COMRELEASE(isp2); return hr; } }
要更多信息,單擊下面的文檔編號查看微軟知識(shí)庫中的文章
Q172763 信息: 在ActiveX中訪問對象模型要更多關(guān)于開發(fā)基于Web的Internet Explorer解決方案,請?jiān)L問下列站點(diǎn):
http://msdn.microsoft.com/workshop/entry.asphttp://msdn.microsoft.com/ie/
http://support.microsoft.com/highlights/iep.asp?FR=0&SD=MSDN
© 微軟公司 2000,保留所有權(quán)利
微軟公司的Jay Andrew Allen 投稿
額外的查詢關(guān)鍵字:IServiceProvider SID_STopLevelBrowser IWebBrowser2
關(guān)鍵字: kbActiveX kbCtrl kbIE kbIE400 kbGrpDSInet kbie500 kbDSupport kbie501
kbie550
文章類型 : kbhowto
技術(shù) : kbIEsearch kbAudDeveloper kbSDKIESearch
kbIE500Search kbSDKIE400 kbSDKIE401 kbSDKIE401SP1 kbSDKIE401SP2 kbSDKIE500
kbSDKIE501 kbSDKIE550 kbIE550Search
P {margin:0px;padding:0px;} body {font-size:10pt;font-family:Tahoma;}
http://support.microsoft.com/default.aspx?scid=kb;en-us;Q257717
View products that this article applies to.
Article ID | : | 257717 |
Last Review | : | September 1, 2005 |
Revision | : | 2.2 |
This article was previously published under Q257717
The Microsoft Knowledge Base article
172763 (http://support.microsoft.com/kb/172763/EN-US/) INFO: Accessing the Object Model from Within an ActiveX Control
explains how to obtain the IWebBrowser2 reference for the host window of an ActiveX control. However, often what developers actually want is a reference to the topmost IWebBrowser2, the one containing the frameset itself. This can be useful if you want to call the statusText() command, for example, to set the value of the window status box before the page has been loaded. Because this property does not function on the WebBrowser control, calling it from the IWebBrowser2 of the embedded frame results in an error.To retrieve the top-level IWebBrowser2 reference, get IServiceProvider from the client site and perform a QueryService for IID_IServiceProvider under the service SID_STopLevelBrowser (defined in Shlguid.h). From this second IServiceProvider, perform a QueryService for IID_IWebBrowser2 in the SID_SWebBrowserApp service.
The best place to perform this work is in the SetClientSite() method of IOleObject:
#include #define COMRELEASE(ptr)/ IWebBrowser2
*browser = NULL; STDMETHODIMP SetClientSite(IOleClientSite
*pClientSite) For additional information, click the article number below to view the
article in the Microsoft Knowledge Base:
172763
(http://support.microsoft.com/kb/172763/EN-US/) INFO: Accessing the
Object Model from Within an ActiveX Control http://msdn.microsoft.com/library/default.asp?url=/workshop/entry.asp
(http://msdn.microsoft.com/library/default.asp?url=/workshop/entry.asp) http://msdn.microsoft.com/ie/
(http://msdn.microsoft.com/ie/) http://support.microsoft.com/iep
(http://support.microsoft.com/iep)
if (ptr !=
NULL) {/
ptr->Release();/
ptr = NULL;/
}
{
HRESULT hr = S_OK;
IServiceProvider *isp, *isp2 =
NULL;
if (!pClientSite)
{
COMRELEASE(browser);
}
else
{
hr
= pClientSite->QueryInterface(IID_IServiceProvider, reinterpret_cast
if (FAILED(hr))
{
hr = S_OK;
goto
cleanup;
}
hr = isp->QueryService(SID_STopLevelBrowser,
IID_IServiceProvider, reinterpret_cast
if
(FAILED(hr))
{
hr = S_OK;
goto cleanup;
}
hr =
isp2->QueryService(SID_SWebBrowserApp, IID_IWebBrowser2,
reinterpret_cast
if (FAILED(hr))
{
hr =
S_OK;
goto cleanup;
}
cleanup:
// Free
resources.
COMRELEASE(isp);
COMRELEASE(isp2);
return
hr;
}
}
REFERENCES
APPLIES TO
•
Microsoft Internet Explorer 4.0 128-Bit Edition
•
Microsoft Internet Explorer 4.01 Service Pack 2
•
Microsoft Internet Explorer 4.01 Service Pack 1
•
Microsoft Internet Explorer 4.01 Service Pack 2
•
Microsoft Internet Explorer 5.0
•
Microsoft Internet Explorer 2.1
•
Microsoft Internet Explorer 2.01
Back to the top
Keywords: |
kbhowto kbctrl KB257717 |
聲明:本網(wǎng)頁內(nèi)容旨在傳播知識(shí),若有侵權(quán)等問題請及時(shí)與本網(wǎng)聯(lián)系,我們將在第一時(shí)間刪除處理。TEL:177 7030 7066 E-MAIL:11247931@qq.com