处理html元素消息( from MSDN)

Handling HTML Element Events


The HTMLElementEvents2 interface is an event sink interface that enables an application to receive events for HTML elements. Your custom application can receive these events, which are fired in response to user actions on HTML elements, when hosting the WebBrowser Control or automating Microsoft® Internet Explorer.

The HTMLElementEvents2 interface is introduced and the steps required to receive notification of HTML element events from the browser are described, using Microsoft® Visual Studio® 5 or later.

It is assumed that a Microsoft Foundation Classes (MFC) application is hosting the WebBrowser Control, or launching Internet Explorer.

This article is divided into the following sections.

Accessing the DHTML Object Model

The IWebBrowser2::Navigate2 method of the IWebBrowser2 interface allows you to navigate the browser to a URL. Once an HTML page is loaded, you can access the HTML elements through the Dynamic HTML (DHTML) Object Model.

The DHTML Object Model is used to access and manipulate the contents of an HTML page and is not available until the page is loaded. Your application determines that the page is loaded by handling the DWebBrowserEvents2::DocumentComplete event of the WebBrowser Control. This event may be fired once for each frame in the page, and once when the top frame of the document is loaded. You can determine if the DWebBrowserEvents2::DocumentComplete event is for the top frame by comparing the IDispatch Online link interface pointer passed by this event with that of the WebBrowser Control.

This sample handler code for the DWebBrowserEvents2::DocumentComplete event demonstrates how to determine if this event is for the top frame, which indicates that the HTML page has loaded. This sample also demonstrates how to create a stream from a block of memory—in this case a string that contains the HTML content to be displayed. The variable m_pBrowser contains the IWebBrowser2 interface pointer obtained from the WebBrowser Control.

Show Example

The IWebBrowser2::get_Document property on the WebBrowser Control retrieves the document object that represents the DHTML Object Model for the top frame.

Accessing an Element on the Page

Using the IHTMLDocument2 interface pointer, you can request a collection of all elements in the HTML document through the IHTMLDocument2::all property.

void CMyClass::ProcessDocument(IHTMLDocument2* pDoc)
{
    IHTMLElementCollection* pElemColl = NULL;

    hr = pDoc->get_all(&pElemColl);
    if (SUCCEEDED(hr))
    {
        // Obtained element collection.
        ProcessElementCollection(pElemColl);
        pElemColl->Release();
    }
}

The IHTMLDocument2::all property returns a collection of all the HTML elements on the page through an IHTMLElementCollection interface pointer. You can use the IHTMLElementCollection interface to call the IHTMLElementCollection::item method and pass the name or id of an element as a parameter, as shown in the following code.

Note  The IHTMLElementCollection::item method will return a collection if there is more than one element with the specified name or id. To prevent a collection from being returned, provide an index as the second parameter of IHTMLElementCollection::item to specify which element should be returned.

Show Example

If you are working with HTML tags of a specific type, such as a tags, the IHTMLElementCollection::tags method returns a collection of all the elements that have the requested HTML tag name, also through an IHTMLElementCollection interface pointer.

Receiving Element Events

Each element in the DHTML Object Model supports an outgoing HTMLElementEvents2 interface. This interface defines the events that an HTML element can fire. You implement this interface to provide an event sink, which is a Component Object Model (COM) object that implements an outgoing interface and is used as the mechanism for firing events.

Note  Interfaces implemented by a server usually have their methods called by the client, but to fire an event, the server calls the respective method on the client event sink. These interface are called outgoing interfaces. A COM object that implements an outgoing interface is also known as a connectable object.

The following steps are required to receive events from an outgoing interface:

  1. Implement the event sink.

    The event sink implements the appropriate outgoing interface and methods. Internet Explorer event interfaces are dispinterfaces, so calls to event methods are made through IDispatch::Invoke Online link. This means that you only need to implement the IDispatch interface to handle events.

  2. Determine if the server is a connectable object.

    Call QueryInterface Online link to retrieve a pointer to the IConnectionPointContainer Online link interface.

  3. Find the appropriate connection point.

    Call the IConnectionPointContainer::FindConnectionPoint Online link method to find the connection point you need. For Internet Explorer WebBrowser Control events, such as DWebBrowserEvents2::DocumentComplete, this is DWebBrowserEvents2. For element events, this is HTMLElementEvents2. You can also call the IConnectionPointContainer::EnumConnectionPoints Online link to enumerate through all the connection points a server supports.

  4. Advise the connection point that you want to receive events.

    Using the IConnectionPoint Online link interface pointer returned in the previous step, call IConnectionPoint::Advise Online link, passing the IUnknown Online link interface pointer of your event sink.

     
    Note  The connectable object will use the IUnknown interface pointer to query the client for the event sink interface. If the event sink does not support the outgoing interface, Internet Explorer will query the client for the IDispatch interface.
  5. When you no longer want to receive events, you can call the IConnectionPoint::Unadvise Online link method, passing the cookie you received from the call to IConnectionPoint::Advise.

The following sample code demonstrates how to begin receiving HTML element events for an element on an HTML page.

Show Example

The following sample code demonstrates how you would detect the firing of an HTMLElementEvents2::onclick event in your implementation of IDispatch::Invoke.

Show Example

Handling Events using MFC

  • The MFC CCmdTarget Online link class implements the IDispatch interface, which means that any class derived from this class can implement an event sink. The IDispatch feature requires a call to the CCmdTarget::EnableAutomation Online link method.
  • The MFC AfxConnectionAdvise Online link and AfxConnectionUnadvise Online link functions find a specified connection point and then advise or unadvise appropriately.
  • The DECLARE_DISPATCH_MAP, BEGIN_DISPATCH_MAP, DISP_FUNCTION, DISP_FUNCTION_ID and END_DISPATCH_MAP macros are used to map each event method to your event handler function.
  • When handling events from a Microsoft® ActiveX® control you are hosting in your MFC applications, use the DECLARE_EVENTSINK_MAP, BEGIN_EVENTSINK_MAP, ON_EVENT and END_EVENTSINK_MAP macros.
  • If you are hosting the WebBrowser Control on a dialog box, you can use the Microsoft® Visual C++® ClassWizard to map events to event handlers.
  • The MFC CHtmlView Online link class hosts the WebBrowser Control and provides overridable methods for the WebBrowser Control events.

Handling Events using ATL

  • When implementing an event sink using the Active Template Library (ATL), your object class will usually derive from the IDispatchImpl Online link class, which provides a default implementation of the IDispatch interface. You can the override the ATL implementation of the IDispatch::Invoke method to handle events.
  • ATL provides two helper functions, AtlAdvise Online link and AtlUnadvise Online link, for advising and unadvising a connection point. These find a specified connection point and then advise or unadvise appropriately.
  • You can also use the ATL IDispEventImpl Online link and IDispEventSimpleImpl Online link classes and BEGIN_SINK_MAP, SINK_ENTRY, SINK_ENTRY_EX and END_SINK_MAP macros to implement an event sink. If you need to host the WebBrowser Control, you can use the ATL Object Wizard to create an HTML object.

Related Topics

The following articles provide information about the WebBrowser Control and MSHTML.

The following articles provide information about Visual Studio.

The following articles provide information about COM.

  • The Component Object Model Specification Online link
  • Inside OLE, 2nd Edition, by Kraig Brockschmidt (Microsoft Press)
  • Understanding ActiveX and OLE, by David Chappell (Microsoft Press)
  • Inside COM, by Dale Rogerson (Microsoft Press)
 
  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
在MFC中实现 EventSink 。 (1) 在MFC中,添加ATL简单对象 CFileMonitorSink (2) 添加继承父类 IDispEventImpl public IDispEventImpl (1) 0 唯一标识符, 用于区别 连接到 事件源的多个客户端 CFileMonitorSink, 当前类名 _IFun1Events, COM 中的事件源接口, 包含各种事件 __ATLEventLib, COM 中Lib类 具体查 MSDN --IDispEventImpl (2) 添加映射项 BEGIN_SINK_MAP(CFileMonitorSink) SINK_ENTRY_EX( 0, __uuidof(_IFun1Events), 1, OnNotify) //0 唯一标识符,用于区别 连接到 事件源的多个客户端 同上 , 1, 事件号 , 发生1号事件 由OnNotify来处理 SINK_ENTRY_EX( 0, __uuidof(_IFun1Events), 2, OnNotify2) //发生2号事件 由OnNotify2来处理 END_SINK_MAP() 并添加方法 STDMETHOD(OnNotify)(void); //事件处理类 STDMETHOD(OnNotify2)(CHAR* lszContent); (3) 连接到COM中的事件容器 添加变量 CComPtr m_Object; //COM 中的事件源对象 添加方法 STDMETHOD(Start)(IUnknown* pSinkThisObject, VARIANT_BOOL* succeeded) { AFX_MANAGE_STATE(AfxGetAppModuleState()); // TODO: 在此添加实现代码 if ( DispEventAdvise(pSinkThisObject) == S_OK ) { m_Object = pSinkThisObject; *succeeded = VARIANT_TRUE; } else { *succeeded = VARIANT_FALSE; } return S_OK; } STDMETHOD(Stop)(void) //解除连接 { AFX_MANAGE_STATE(AfxGetAppModuleState()); DispEventUnadvise(m_Object); return S_OK; } 在其他类中的 使用方法: CComPtr m_FileMonitorSink; CComPtr m_FileMonitor; //COM中导出接口 CoInitialize(0); HRESULT lRt = m_FileMonitorSink.CoCreateInstance( __uuidof(FileMonitorSink) ); lRt = m_FileMonitor.CoCreateInstance(__uuidof(Fun1)); //创建COM接口实例 VARIANT_BOOL succeeded; lRt = m_FileMonitorSink->Start(m_FileMonitor, &succeeded); //把 m_FileMonitorSink 连接到COM中的事件容器上 m_FileMonitor->HelloWorld(); //调用COM接口,接口中触发事件s m_FileMonitorSink->stop(); //从COM接口中解除连接 CoUninitialize(); // ################# CFileMonitorSink 类代码 ################# class ATL_NO_VTABLE CFileMonitorSink : public CComObjectRootEx, public CComCoClass, public IDispatchImpl, public IDispEventImpl { public: CFileMonitorSink() { } DECLARE_REGISTRY_RESOURCEID(IDR_FILEMONITORSINK) BEGIN_COM_MAP(CFileMonitorSink) COM_INTERFACE_ENTRY(IFileMonitorSink) COM_INTERFACE_ENTRY(IDispatch) END_COM_MAP() BEGIN_SINK_MAP(CFileMonitorSink) SINK_ENTRY_EX( 0, __uuidof(_IFun1Events), 1, OnNotify) SINK_ENTRY_EX( 0, __uuidof(_IFun1Events), 2, OnNotify2) END_SINK_MAP() DECLARE_PROTECT_FINAL_CONSTRUCT() HRESULT FinalConstruct() { return S_OK; } void FinalRelease() { } CComPtr m_Object; //COM 事件源对象 public: STDMETHOD(OnNotify)(void); STDMETHOD(Stop)(void); STDMETHOD(Start)(IUnknown* pSinkThisObject, VARIANT_BOOL* succeeded); STDMETHOD(OnNotify2)(CHAR* lszContent); };

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值