天天看點

如何: 在ActiveX 控件中獲得頂層 IWebBrowser2 接口How To Retrieve the Top-Level IWebBrowser2 Interface from an ActiveX Control

此文章的資訊應用于:

  • 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 : 257717
Last Review : September 1, 2005
Revision : 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
如何: 在ActiveX 控件中獲得頂層 IWebBrowser2 接口How To Retrieve the Top-Level IWebBrowser2 Interface from an ActiveX Control
Back to the top
Keywords: 
kbhowto kbctrl KB257717

繼續閱讀