强大的文章必须转载保留 Handling HTML Element Events

深入解析XTEA加密算法的核心机制与优化策略 本文深入解析XTEA加密算法的核心机制与优化策略,详细介绍了XTEA在嵌入式系统和资源受限环境中的优势,包括其高效的加解密速度和低资源消耗。文章还探讨了XTEA的轮函数设计、关键参数优化策略以及实战应用中的性能对比,为开发者提供了安全增强方案和优化技巧。 阅读详情

Handling HTML Element Events

9 out of 9 rated this helpful - Rate this topic

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 Windows 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 theDWebBrowserEvents2::DocumentComplete event is for the top frame by comparing the IDispatch 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.

void CMyClass::DocumentComplete(LPDISPATCH pDisp, VARIANT* URL)
{
    HRESULT hr;
    IUnknown* pUnkBrowser = NULL;
    IUnknown* pUnkDisp = NULL;
    IDispatch* pDocDisp = NULL;
    IHTMLDocument2* pDoc = NULL;

    // Is this the DocumentComplete event for the top frame window?
    // Check COM identity: compare IUnknown interface pointers.
    hr = m_pBrowser->QueryInterface(IID_IUnknown, (void**)&pUnkBrowser);

    if (SUCCEEDED(hr))
    {
        hr = pDisp->QueryInterface(IID_IUnknown, (void**)&pUnkDisp);

        if (SUCCEEDED(hr))
        {
            if (pUnkBrowser == pUnkDisp)
            {
                // This is the DocumentComplete event for the top frame.
                // This page is loaded, so we can access the DHTML Object Model.
                hr = m_pBrowser->get_Document(&pDocDisp);

                if (SUCCEEDED(hr))
                {
                    // Obtained the document object.
                    pDocDisp->QueryInterface(IID_IHTMLDocument2, (void**)&pDoc);
                    if (SUCCEEDED(hr))
                    {
                        // Obtained the IHTMLDocument2 interface for the document object
                        ProcessDocument(pDoc);
                    }

                    pDocDisp->Release();
                }
            }

            pUnkDisp->Release();
        }

        pUnkBrowser->Release();
    }
}

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 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 all property returns a collection of all the HTML elements on the page through an IHTMLElementCollection interface pointer. You can use theIHTMLElementCollection interface to call the item method and pass the name or id of an element as a parameter, as shown in the following code.

Note  The  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  item to specify which element should be returned.
void CMyClass::ProcessElementCollection(IHTMLElementCollection* pElemColl)
{
    IDispatch* pElemDisp = NULL;
    IHTMLElement* pElem = NULL;
    _variant_t varID("myID", VT_BSTR);
    _variant_t varIdx(0, VT_I4);

    hr = pElemColl->item(varID, varIdx, &pElemDisp);

    if (SUCCEEDED(hr))
    {
        hr = pElemDisp->QueryInterface(IID_IHTMLElement, (void**)&pElem);

        if (SUCCEEDED(hr))
        {
            // Obtained element with ID of "myID".
            ConnectEvents(pElem);
            pElem->Release();
        }

        pElemDisp->Release();
    }
}

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. 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 to retrieve a pointer to the IConnectionPointContainer interface.

  3. Find the appropriate connection point.

    Call the IConnectionPointContainer::FindConnectionPoint 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 to enumerate through all the connection points a server supports.

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

    Using the IConnectionPoint interface pointer returned in the previous step, call IConnectionPoint::Advise, passing the IUnknown 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 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.

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

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

STDMETHODIMP CEventSink::Invoke(DISPID dispidMember,
                                REFIID riid,
                                LCID lcid,
                                WORD wFlags,
                                DISPPARAMS* pdispparams,
                                VARIANT* pvarResult,
                                EXCEPINFO* pexcepinfo,
                                UINT* puArgErr)
{
    switch (dispidMember)
    {
        case DISPID_HTMLELEMENTEVENTS2_ONCLICK:
        OnClick();
        break;

        default:
        break;
    }

    return S_OK;
}

Handling Events using MFC

  • The MFC CCmdTarget 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 method.
  • The MFC AfxConnectionAdvise and AfxConnectionUnadvise 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 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 class, which provides a default implementation of the IDispatch interface. You can the override the ATL implementation of the IDispatch::Invokemethod to handle events.
  • ATL provides two helper functions, AtlAdvise and AtlUnadvise, 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 and IDispEventSimpleImpl 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
  • 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)
数据资产可视化分析:PowerBI在大数据管理中的实战 目的:帮你搞懂“为什么数据资产需要可视化?”“PowerBI能帮企业解决什么问题?”,并掌握用PowerBI做大数据可视化的实战技巧。范围:覆盖数据资产定义、可视化分析原理、PowerBI核心功能(数据连接、建模、可视化)、零售企业实战案例,以及未来趋势与挑战。背景:为什么企业的数据像乱堆的玩具?核心概念:数据资产、可视化分析、PowerBI分别是什么?它们怎么配合?实战步骤:用PowerBI帮零售企业整理“销售玩具箱”,做出能指导决策的仪表盘;应用场景:不同部门怎么用这个“整理好的玩具箱”? 阅读详情

相关推荐

从特斯拉到蔚来:拆解主流智能汽车的车联网架构,看它们如何实现‘车、路、云’协同

本文深度解析了特斯拉、蔚来等主流智能汽车的车联网架构,详细拆解了其‘车、路、云’协同的技术实现。从车内网络、车际通信到云端协同,分析了通信技术、系统架构等关键技术选择,揭示了不同厂商如何通过差异化布局构建竞争优势。

weixin_42525289的博客 580

DISPID_HTMLELEMENTEVENTS2_ONCLICK 定义所在的头文件

DISPID_HTMLELEMENTEVENTS2_ONCLICK  ...等宏的 头文件 mshtmdid.h 你只要在你的工程中如下包含即可 #include "mshtmdid.h"

张翼惊云 1773

AGV避坑指南:海康控制系统与Modbus设备对接的那些‘坑’

本文深入解析海康AGV控制系统与Modbus设备对接中的常见问题及解决方案,涵盖通信协议对接、信号干扰、硬件层问题和软件优化策略。通过真实案例和实用技巧,帮助工程师规避典型陷阱,提升系统稳定性和效率。

l3m4n的博客 741

vue的源码学习之一——Snabbdom源码学习

参考资料: 1、vue2源码学习开胃菜——snabbdom源码学习(二) 2、vue2源码学习开胃菜——snabbdom源码学习(一) 3、Vue 原理解读系列(一) 之 Virtual DOM and Diff 1、Snabbdom 简介 Snabbdom 非常地简洁,其核心代码只有大约 200 行,并且支持可扩展性良好,它拥有一个模块化的结构,允许自定模块,而为了保持核心部分的简洁,...

路在脚下,幸福在手中,莫贪!!!!! 613

机器人全局路线规划导读

认知有限,望大家多多包涵,有什么问题也希望能够与大家多交流,共同成长!本文先对全局路线规划导读做个简单的介绍,具体内容后续再更,其他模块可以参考去我其他文章提示:以下是本篇文章正文内容。

qq_35635374的博客 1504

Vue2 - 详细实现接入使用海康威视监控云端显示及控制功能,网站集成安装海康监控及实时监控显示、回放、放大缩小画面、视频画面截图、截屏抓拍、摄像头控制,实时推流及播放器解决延迟卡顿等问题(详细代码)

vue2,nuxt2,海康威视,海康监控,摄像头监控,vue对接海康处理接口认证规则,监控实时预览和回放功能,海康api接口,安全认证,海康开放平台,官方文档,播放海康摄像头的监控视频,海康实时监控播放详细步骤,web,黑屏,特别卡不流畅怎么办,解决无法推流,录像,连接不上,vue,海康,vue海康视频监控,海康开放平台,实时监控画面,海康插件,视频播放,回放,视频流地址,取流协议,视频sdk播放,hik,rtsp,rtmp,hls,ws,网站,网页,h5页面,后台管理系统,vue2对接海康监控设备,无法连

🏆 前端领域优质创作者 8248

VC 监视网页中的元素事件

通过近两个月的学习《Internet Explorer 5.0程序设计》,我终于知道该怎样来挂接一个网页中各元素的事件,现将我解决方法告知如下: 本例利用C++来捕获IE网页中的元素事件. 一、建立一个MFC单文档应用程序,选择视图类为CEditView. 二、新建一个C++类,该类派生于IDispatch接口。例如:(我的类为CIESpyEvent)   #pra

wxinstudent的专栏 2718

使用Python实现深度学习模型:智能光污染监测与管理

本项目将通过以下几个步骤实现智能光污染监测:收集光污染图像数据数据预处理构建深度学习模型训练模型评估模型部署模型用于实时监测通过本文介绍的步骤,我们实现了一个使用Python和深度学习技术的智能光污染监测与管理系统。该系统能够自动识别光污染源,为相关管理部门提供科学依据,以便更好地规划和管理城市照明。同时,这一方法还可以扩展到其他环境监测领域,为环境保护贡献力量。

Echo_Wish 602

CHtmlView 和 Webbrowser 控件中超链接点击事件的获取与检测

在自己的程序中使用 CHtmlView 或直接嵌入 Webbrowser 控件显示网页时,常常需要获取网页元素的一些事件,以实现对网页显示的控制或与网页元素进行交互。最常见的莫过于获取用户对网页上超链接的所有点击事件。要实现这个需求,在 MSDN 中描述了接收网页元素事件的基本方法,但这篇文章的一些细节语焉不详,让人摸不着头脑。在 CodeProject 这篇文章中提出了一种替代的方法,但是存在一

mail_cm的专栏 1479

WOFOST作物模型(7):6.0.6版本的PCSE使用(多层水分平衡和SNOMIN碳氮模型的集成)

wofost81文件结构如上,其中所需的参数可从github上获取然后input下有两个需要调用的功能函数py文件,需要拖到自己的运行py文件同级目录下在使用土壤模块SNOMIN进行作物生长模型模拟时,需要为土壤剖面的不同层次提供多种输入变量。这些变量通常不随年份变化,因此会统一在土壤文件中定义。本节将说明如何将这些输入变量整合到一个字典中,进而生成SNOMIN能够读取的土壤文件。

所有专家都曾始于新手 638

Python地理数据处理 十七:植被物候提取和分析(Savitzky-Golay)

植被是陆地生态系统中最重要的组分之一,也是对气候变化最敏感的组分,其在全球变化过程中起着重要作用,能够指示自然环境中的大气、水、土壤等成分的变化,其年际和季节性变化可以作为地球气候变化的重要指标。此外,由于生态工程保护建设和植被自然生长等因素,中国陆地生态系统发挥了重要的碳汇作用。因此,定量评估植被时空动态变化是制定生态系统可持续发展目标和衡量生态系统固碳潜力的重要前提,卫星遥感数据衍生的生态参量产品为研究长时间序列全球及区域植被时空变化提供了重要数据源。SG滤波能够对物候信息进行平滑,并提取其中的物候信息

amyniez的博客,欢迎交流讨论 4248

HTML DOM Event 对象

事件句柄 (Event Handlers) 属性         此事件发生在何时... onabort     图像的加载被中断。 onerror     在加载文档或图像时发生错误。 onload         一张页面或一幅图像完成加载。 onresiz

Donny的专栏 648

YOLO26改进策略【YOLO和Mamba】| CVPR 2025:EfficientViM 压缩隐藏状态空间进行通道混合、单头设计减少内存操作,提升目标检测效率与精度

本文记录的是利用模块优化的目标检测网络模型。EfficientViM Block(基于隐藏状态混合器的状态空间对偶性模块) 的设计旨在,提升特征提取效率,同时兼顾全局依赖捕捉与局部细节保留。本文将深入研究的原理,并将其应用到中,通过重构特征提取流程、减少内存绑定操作、融合多阶段特征,增强模型在资源受限环境下的检测性能与速度。EfficientViM: Efficient Vision Mamba with Hidden State Mixer based State Space DualityEfficie

Limiiiing的博客 401

从TypeScript视角看HTML DOM(六):Scripting

这一篇可能和TypeScript没太大关系。 看到TypeScript实现的script接口(去掉了deprecate的属性),除了我们平时经常用的几个属性之外,还可以添加监听,倒是让人耳目一新。 interface HTMLScriptElement extends HTMLElement { async: boolean; defer: boolean; src: ...

HermitSun的博客 1374

VC中sqlite的使用

在VC中对sqlite的使用,实现数据库大批数据的快速插入,读取数据库中的数据在ListCtrl中显示

IDEA的使用指南

IDEA基于java的使用手册,可以最快速的上手,在使用中不断的丰富自己的使用技巧.

那些关于DOM的常见Hook封装(一)

本文是深入浅出 ahooks 源码系列文章的第十四篇,这个系列的目标主要有以下几点:加深对 React hooks 的理解。学习如何抽象自定义 hooks。构建属于自己的 React hooks 工具库。培养阅读学习源码的习惯,工具库是一个对源码阅读不错的选择。上一篇我们探讨了 ahooks 对 DOM 类 Hooks 使用规范,以及源码中是如何去做处理的。接下来我们就针对关于 DOM 的各个 Hook 封装进行解读。...

油墨香^-^的博客 578

Probability and Statistics - 4th edition

Probability and Statistics - 4th edition

webbrowser处理HTML Element Event事件

http://blog.csdn.net/sstower/article/details/6253409?reload Example of Handling HTML Element Events in Microsoft Internet Explorer The technique makes use of the Microsoft Foundation Class

sx5486510的专栏 5543

假人有限元模型

假人模型,车辆碰撞假人有限元模型,适合做乘员安全的研究者利用

处理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 thes

kh 2042

dom的typescript类型定义

typescript、dom 、ts、type如何定义dom

weixin_42283818的博客 2290

挂接浏览器事件

        前几天在《 一个基于MFC的自动化(Automation)实例》上说最近会发一个关于如何挂接浏览器事件的教程,现在如期兑现承诺啦。说实话,解决这个问题花了我近一年的时间,虽然期间不是每天都在想这个问题,但无论如何这听起来绝对是一段不短的时间!也许因为我是生物系的吧,不能像计算机系的朋友那样有那么多现成的资源可以利用,一切都靠自学,碰到不懂的问题就在浩瀚的网络世界中寻找答案,

握固 5395
上一篇: 小点点成为是最强大的鼠标模拟点击软件
下一篇: 【小点点】上架了他们的官方Windows 8应用
普通网友
博客等级 码龄22年 1万+粉丝 3156原创
评论
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

个

红包个数最小为10个

元

红包金额最低5元

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

抵扣说明:

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

余额充值