问题的起因:
应用程序内,加入了WebBrowser。并已经成功的在DocumentComplete事件中获得了IHTMLDocument2接口。问题的描述:
现在需要对WebBrowser中的动作进行监控,那么需要建立 应用程序和IHTMLDocument2之间的连接,但是却找不到 IHTMLDocument2 的dispid。做过尝试:下载了 “在你的应用程序中集成WebBrowser控件”的源代码,发现,其中少了好几个关键文件,而且该代码也是从底层堆积木似的作上来的,太复杂。附上部分代码://头文件class COFDialog :
public IDispEventImpl<IDC_EXPLORER1,COFDialog>
{
public:
BEGIN_SINK_MAP(COFDialog)
SINK_ENTRY(IDC_EXPLORER1, 259, DocumentCompleteExplorer1)
END_SINK_MAP() void __stdcall DocumentCompleteExplorer1(LPDISPATCH pDisp, VARIANT* URL);
CComPtr<IWebBrowser2> m_spBrowser;
VARIANT_BOOL m_init;
IHTMLDocument2* m_pHTMLDocument2;
}//CPP文件void __stdcall COFDialog::DocumentCompleteExplorer1(LPDISPATCH pDisp, VARIANT* URL)
{
if(m_init)
m_pHTMLDocument2->Release();
CComPtr<IDispatch> spDispatch;
m_spBrowser->get_Document(&spDispatch);
spDispatch->QueryInterface(IID_IHTMLDocument2,(void**)&m_pHTMLDocument2);
m_init = VARIANT_TRUE;//下面这些代码是后来我参照“在你的应用程序中集成WebBrowser控件”加入的,接下来不知道怎么做了。
CComPtr<IConnectionPointContainer> spConnectionPointContainer;
HRESULT hr = m_pHTMLDocument2->QueryInterface(IID_IConnectionPointContainer,(void**)&spConnectionPointContainer);
CComPtr<IConnectionPoint> spConnectionPoint;
hr = spConnectionPointContainer->FindConnectionPoint(DIID_HTMLDocumentEvents,&spConnectionPoint);
}
望各位同仁熟悉这一块的给个解决办法,在下实在不胜感激。
应用程序内,加入了WebBrowser。并已经成功的在DocumentComplete事件中获得了IHTMLDocument2接口。问题的描述:
现在需要对WebBrowser中的动作进行监控,那么需要建立 应用程序和IHTMLDocument2之间的连接,但是却找不到 IHTMLDocument2 的dispid。做过尝试:下载了 “在你的应用程序中集成WebBrowser控件”的源代码,发现,其中少了好几个关键文件,而且该代码也是从底层堆积木似的作上来的,太复杂。附上部分代码://头文件class COFDialog :
public IDispEventImpl<IDC_EXPLORER1,COFDialog>
{
public:
BEGIN_SINK_MAP(COFDialog)
SINK_ENTRY(IDC_EXPLORER1, 259, DocumentCompleteExplorer1)
END_SINK_MAP() void __stdcall DocumentCompleteExplorer1(LPDISPATCH pDisp, VARIANT* URL);
CComPtr<IWebBrowser2> m_spBrowser;
VARIANT_BOOL m_init;
IHTMLDocument2* m_pHTMLDocument2;
}//CPP文件void __stdcall COFDialog::DocumentCompleteExplorer1(LPDISPATCH pDisp, VARIANT* URL)
{
if(m_init)
m_pHTMLDocument2->Release();
CComPtr<IDispatch> spDispatch;
m_spBrowser->get_Document(&spDispatch);
spDispatch->QueryInterface(IID_IHTMLDocument2,(void**)&m_pHTMLDocument2);
m_init = VARIANT_TRUE;//下面这些代码是后来我参照“在你的应用程序中集成WebBrowser控件”加入的,接下来不知道怎么做了。
CComPtr<IConnectionPointContainer> spConnectionPointContainer;
HRESULT hr = m_pHTMLDocument2->QueryInterface(IID_IConnectionPointContainer,(void**)&spConnectionPointContainer);
CComPtr<IConnectionPoint> spConnectionPoint;
hr = spConnectionPointContainer->FindConnectionPoint(DIID_HTMLDocumentEvents,&spConnectionPoint);
}
望各位同仁熟悉这一块的给个解决办法,在下实在不胜感激。
File/View TypeLib...然后查看其连接点出接口信息,
,自己实现一个继承于IDispatch的
接口,重载虚函数Invoke,根据传入
的Dispid调用相应的你的函数。最后生成该接口的实例,连接到WebBrowser的连接点上
范例代码如下:HRESULT hr = m_smsWrap->QueryInterface(IID_IConnectionPointContainer,(void**)&m_pConnectionPointContainer);
if(!m_pConnectionPointContainer)
{
m_smsWrap->Release();
return false;
}
hr = m_pConnectionPointContainer->FindConnectionPoint(DIID___SmsGwWrap,&m_pConnectionPoint);
if(!m_pConnectionPoint)
{
m_pConnectionPointContainer->Release();
m_smsWrap->Release();
return false;
}
hr = m_pConnectionPoint->Advise((IUnknown*)((CMyUnknown*)this),&m_dwCookie);
if(hr != S_OK)
{
m_pConnectionPointContainer->Release();
m_pConnectionPoint->Release();
m_smsWrap->Release();
return false;
}
class xxx :public IDispatch
{
protected:
DWORD m_dwCookie;
IConnectionPointContainer *m_pConnectionPointContainer;
IConnectionPoint *m_pConnectionPoint;
.......
}
IDispatch 接口的所有虚函数都必须
重载,包括IUnknown接口的AddRef(),Release()和
QueryInterface()。IDispatch 接口的Invoke函数必须重载(根据传入的dispIdMember
来进行你自己对应的事件处理),其他的几个虚函数只简单的
返回FALSE/TRUE就可以了。