积极答复者
关于Handling HTML Element Events 例子的问题

问题
-
http://msdn.microsoft.com/zh-sg/library/bb508508.aspx#CommunityContent
我按照上面的做了个例子,测试百度页面的搜索文本框(wd),但是为什么在
void CMyClass::ConnectEvents(IHTMLElement* pElem)
{
HRESULT hr;
IConnectionPointContainer* pCPC = NULL;
IConnectionPoint* pCP = NULL;
DWORD dwCookie;
// Check that this is a connectable object.
hr = pElem->QueryInterface(IID_IConnectionPointContainer, (void**)&pCPC);
if (SUCCEEDED(hr))
{
// Find the connection point.
hr = pCPC->FindConnectionPoint(DIID_HTMLElementEvents2, &pCP); //这里用DIID_HTMLElementEvents2找不到事件,用DIID_HTMLInputTextElementEvents2就可以,为什么?
if (SUCCEEDED(hr))
{
// Advise the connection point.
// pUnk is the IUnknown interface pointer for your event sink
hr = pCP->Advise(pUnk, &dwCookie);
if (SUCCEEDED(hr))
{
// Successfully advised
}
pCP->Release();
}
pCPC->Release();
}
}因为我要实现不单只文本框的事件,还有下拉框等其他控件的事件,所以我想找个通用的入口的,但是貌似DIID_HTMLElementEvents2不行。何解?
答案
-
你好,
IConnectionPointContainer::FindConnectionPoint方法可以查找你需要的连接点。但是你需要定义具体的链接点的IID。DIID_HTMLElementEvents2 可以截取一个元素触发的事件。具体截获的事件您可以参考HTMLElementEvents2调度接口:http://msdn.microsoft.com/en-us/library/aa769636(VS.85).aspx.如果你需要调用所有的链接点,你可以通过IConnectionPointContainer::EnumConnectionPoints 去枚举所有的连接点。
希望我的回答能够帮助您解决您的问题。
Rob Pan [MSFT]
MSDN Community Support | Feedback to us
Get or Request Code Sample from Microsoft
Please remember to mark the replies as answers if they help and unmark them if they provide no help.
- 已标记为答案 VisualElevenModerator 2012年2月1日 4:55
全部回复
-
你好,
IConnectionPointContainer::FindConnectionPoint方法可以查找你需要的连接点。但是你需要定义具体的链接点的IID。DIID_HTMLElementEvents2 可以截取一个元素触发的事件。具体截获的事件您可以参考HTMLElementEvents2调度接口:http://msdn.microsoft.com/en-us/library/aa769636(VS.85).aspx.如果你需要调用所有的链接点,你可以通过IConnectionPointContainer::EnumConnectionPoints 去枚举所有的连接点。
希望我的回答能够帮助您解决您的问题。
Rob Pan [MSFT]
MSDN Community Support | Feedback to us
Get or Request Code Sample from Microsoft
Please remember to mark the replies as answers if they help and unmark them if they provide no help.
- 已标记为答案 VisualElevenModerator 2012年2月1日 4:55
-
谢谢你的答复,貌似网站打不开。。。
另外我sink出一个接口,但是以下会报错:
STDMETHODIMP CTest::Invoke(DISPID dispidMember, REFIID riid,LCID lcid, WORD wFlags,DISPPARAMS* pDispParams, VARIANT*pvarResult, EXCEPINFO* pexcepinfo,UINT* puArgErr)
{
HRESULT hr = S_OK;
if (pDispParams)
{
switch (dispidMember)
{
case DISPID_HTMLELEMENTEVENTS2_ONDBLCLICK:
{
CComQIPtr<IWebBrowser2, &IID_IWebBrowser2> m_spHtmlDoc;
CComQIPtr<IHTMLWindow2,&IID_IHTMLWindow2> m_pHtmlWindow;
IDispatch* pDocDisp = NULL;
IHTMLDocument2* pDoc = NULL;
m_spHtmlDoc = (IWebBrowser2*)pDispParams->rgvarg[pDispParams->cArgs - 1].byref;
//CComQIPtr<IWebBrowser2, &IID_IWebBrowser2> m_spHtmlDoc = (IWebBrowser2*)pDispParams->rgvarg[6].pdispVal;
if(m_spHtmlDoc)
{
hr = m_spHtmlDoc->get_Document(&pDocDisp);//这里为什么获取不到?是不是安全限制?
if(SUCCEEDED(hr) && pDocDisp)
{
hr = pDocDisp->QueryInterface(IID_IHTMLDocument2, (void**)&pDoc);
if(SUCCEEDED(hr) && pDoc)
{
hr = pDoc->get_parentWindow(&m_pHtmlWindow);
if(SUCCEEDED(hr) && m_pHtmlWindow)
{
VARIANT ret;
ret.vt = VT_EMPTY;
hr = m_pHtmlWindow->execScript(L"{alert(\"不能为空。\");}", L"javascript", &ret);
}
}
}
}
break;
}
default:
{
hr = DISP_E_MEMBERNOTFOUND;
break;
}
}
}
else
hr = DISP_E_PARAMNOTFOUND;
return hr;
}