此文章的信息应用于:
Microsoft Internet Explorer (编程) 版本4.0, 4.01, 4.01 SP1, 4.01 SP2, 5, 5.01, 5.5
转者注:http://support.microsoft.com/default.aspx?scid=kb;en-us;Q257717
概要
微软知识库文章
Q172763 信息:在ActiveX 中访问对象模型
说明了如何在控件中获得所在窗口的IWebBrowser2的引用。但是,开发者实际上经常需要的是饱含框架集的顶层IWebBrowser2的引用。例如,当你在网页载入之前调用statusText() 命令设置状态栏的值时可以用到。因为这个属性对WebBrowser控件无效,所以调用所在框架的IWebBrowser2的函数会产生错误。
更多信息
为获得顶层IWebBrowser2 引用, 从客户站点获取IServiceProvider 并且 执行一个QueryService 操作获取IID_IServiceProvider服务SID_STopLevelBrowser (在Shlguid.h中定义)。对第二个IServiceProvider,执行一个QueryService 获取IID_IWebBrowser2 服务SID_SWebBrowserApp.干这个的最好的地方是在IOleObject的SetClientSite() 方法里面:
#include <SHLGUID.h>
#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<void **>(&isp));
if (FAILED(hr))
{
hr = S_OK;
goto cleanup;
}
hr = isp->QueryService(SID_STopLevelBrowser, IID_IServiceProvider, reinterpret_cast<void **>(&isp2));
if (FAILED(hr))
{
hr = S_OK;
goto cleanup;
}
hr = isp2->QueryService(SID_SWebBrowserApp, IID_IWebBrowser2, reinterpret_cast<void **>(&browser));
if (FAILED(hr))
{
hr = S_OK;
goto cleanup;
}
cleanup:
// Free resources.
COMRELEASE(isp);
COMRELEASE(isp2);
return hr;
}
}
参考
要更多信息,单击下面的文档编号查看微软知识库中的文章
Q172763 信息: 在ActiveX中访问对象模型
要更多关于开发基于Web的Internet Explorer解决方案,请访问下列站点:
http://msdn.microsoft.com/workshop/entry.asp
http://msdn.microsoft.com/ie/
http://support.microsoft.com/highlights/iep.asp?FR=0&SD=MSDN
© 微软公司 2000,保留所有权利
微软公司的Jay Andrew Allen 投稿
额外的查询关键字:IServiceProvider SID_STopLevelBrowser IWebBrowser2
关键字: kbActiveX kbCtrl kbIE kbIE400 kbGrpDSInet kbie500 kbDSupport kbie501 kbie550 文章类型 : kbhowto 技术 : kbIEsearch kbAudDeveloper kbSDKIESearch kbIE500Search kbSDKIE400 kbSDKIE401 kbSDKIE401SP1 kbSDKIE401SP2 kbSDKIE500 kbSDKIE501 kbSDKIE550 kbIE550Search
How To Retrieve the Top-Level IWebBrowser2 Interface from an ActiveX Control
2006/3/27 19:25:36
P {margin:0px;padding:0px;} body {font-size:10pt;font-family:Tahoma;}
http://support.microsoft.com/default.aspx?scid=kb;en-us;Q257717
How To Retrieve the Top-Level IWebBrowser2 Interface from an ActiveX Control
View products that this article applies to.
Article ID:257717Last Review:September 1, 2005Revision:2.2
This article was previously published under Q257717
SUMMARY
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.
MORE INFORMATION
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 <SHLGUID.h>
#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<void **>(&isp));
if (FAILED(hr))
{
hr = S_OK;
goto cleanup;
}
hr = isp->QueryService(SID_STopLevelBrowser, IID_IServiceProvider, reinterpret_cast<void **>(&isp2));
if (FAILED(hr))
{
hr = S_OK;
goto cleanup;
}
hr = isp2->QueryService(SID_SWebBrowserApp, IID_IWebBrowser2, reinterpret_cast<void **>(&browser));
if (FAILED(hr))
{
hr = S_OK;
goto cleanup;
}
cleanup:
// Free resources.
COMRELEASE(isp);
COMRELEASE(isp2);
return hr;
}
}
REFERENCES
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
For more information about developing Web-based solutions for Microsoft Internet Explorer, visit the following Microsoft Web sites:
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)
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