msdn website

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);

        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();
    }
}


如何获得pUnk指针?我已经将CComObject * pUnk定义为全局变量,并将其初始化为CComObject :: CreateInstance(&pUnk);。然后,在上面的代码中使用如此定义的pUnk,该代码给出了以下错误:

错误1错误C2065:'m_dwRef':未声明的标识符c:\ program files \ microsoft visual studio 9.0 \ vc \ atlmfc \ include \ atlcom.h 2575

错误2错误C3861:'FinalRelease':找不到标识符c:\ program files \ microsoft visual studio 9.0 \ vc \ atlmfc \ include \ atlcom.h 2576

哪将是获取pUnk的正确方法?
示例代码将非常有用。
谢谢!

[编辑:我正在尝试在使用ATL处理HTMLElementEvents2的IE扩展应用程序中使用此示例代码。我的类派生自IDispEventImpl以处理Web浏览器事件,而我正尝试派生自另一个IDispEventImpl实例以处理HTMLElementEvents2。

class ATL_NO_VTABLE CMyClass:
    public CComObjectRootEx<CComSingleThreadModel>,
    public CComCoClass<CSecurMailBHO, &CLSID_MyClass>,
    public IObjectWithSiteImpl<CMyClass>,
    public IDispatchImpl<ISecurMailBHO, &IID_IMyClass, &LIBID_MyClassLib, /*wMajor =*/ 1, /*wMinor =*/ 0>,
    public IDispEventImpl<1, CMyClass, &DIID_DWebBrowserEvents2, &LIBID_SHDocVw, 1, 1>, //Safe alternative to Invoke
    public IDispEventImpl<2, CMyClass, &DIID_HTMLElementEvents2, &LIBID_MSHTML, 4, 0>
{
.
.
.
BEGIN_SINK_MAP(CMyClass)
     SINK_ENTRY_EX(1, DIID_DWebBrowserEvents2, DISPID_DOCUMENTCOMPLETE, OnDocumentComplete)//Do stuff OnDocumentComplete
     SINK_ENTRY_EX(1, DIID_DWebBrowserEvents2, DISPID_BEFORENAVIGATE2, BeforeNavigate2)//Handle BeforeNavigate2

     SINK_ENTRY_EX(2, DIID_HTMLElementEvents2, DISPID_HTMLELEMENTEVENTS2_ONSCROLL, OnScroll)//Handle OnScroll Event

END_SINK_MAP()
.
.

}


在我的OnDocumentComplete中,我调用IWebBrowser2 :: get_Document,然后调用IHTMLDocument2 :: get_body以获取IHTMLElement对象主体。这就是上面要发送给ConnectEvents函数的内容。但是到目前为止,还没有运气。我不知道什么是pUnk指针,而且,似乎对FindConnectionPoint的调用失败,即,在点容器中没有HTMLElementEvents2连接点。关于如何处理htmlelementevents的任何想法??????????

结束编辑]

最佳答案

ATLEventHandling Sample

编辑:

void CMyClass::ConnectEvents(IHTMLElement* pElem)
{
    typedef IDispEventImpl<2, CMyClass, DIID_HTMLElementEvents2, LIBID_MSHTML, 4, 0> IMyClassHTMLElementEvents2Sink;
    IMyClassHTMLElementEvents2Sink::DispEventAdvise(pElem);
}


因为在基DispEventAdvise类上具有多个功能IDispEventImpl

同样,似乎定义HTMLElementEvents2的MSHTML的最早版本是5.0。

关于c++ - 通知HTMLElementEvents2接收器(MSDN网站),我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/1127326/

10-16 19:14