如何从一个窗口句柄获取iwebbrowser2接口
1个回答
推荐于2016-07-14 · 知道合伙人互联网行家
关注
展开全部
开发者实际上经常需要的是饱含框架集的顶层IWebBrowser2的引用。例如,当你在网页载入之前调用statusText() 命令设置状态栏的值时可以用到。因为这个属性对WebBrowser控件无效,所以调用所在框架的IWebBrowser2的函数会产生错误。
做这件事情最适合的地方是在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; } }
做这件事情最适合的地方是在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; } }
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询