ActiveX控件

创建一个MFC ActiveX控件项目,会得到整个程序的基本架构,下面以msdn下载的button为例,介绍下ActiveX的制作:

ActiveX控件

工程自动为我们生成了框架,有3个类和两个接口。CButtonApp做了注册等工作,我们一般不需要改动;CButtonCtrl是实现ActiveX控件的主体;CButtonPropPage是一个对话框,实现自定义的属性页;DButton接口是控件属性的接口;DButtonEvents是事件的接口。在函数OnDraw中绘制控件的外观,如果我们想使用一个公共Windows控件的外观,在创建工程时,可以指定基于哪个现有控件,生成的代码如下:

BOOL CButtonCtrl::PreCreateWindow(CREATESTRUCT& cs)
{
 cs.lpszClass = _T("BUTTON");
 return COleControl::PreCreateWindow(cs);
}

void CButtonCtrl::OnDraw(
   CDC* pdc, const CRect& rcBounds, const CRect& )
{
 DoSuperclassPaint(pdc, rcBounds);
}

一个ActiveX控件就像MFC工具栏上的那些控件一样,通过它的属性来自定义控件的样式,通过写事件处理函数来响应该控件发出的事件。下面描述属性和事件的添加以及它们的原理。

一.属性的添加

右击DButton->添加->属性,库存属性可以用常规或get\set的方式实现,自定义属性以成员变量或get\set的方式实现。当创建的ActiveX控件继承自公共Windows控件时,我们创建控件就具备了该公共Windows控件的外观:

void CButtonCtrl::OnDraw(
   CDC* pdc, const CRect& rcBounds, const CRect& )
{
 DoSuperclassPaint(pdc, rcBounds);
}

此时,库存属性使用常规的方式,属性的值会直接反映到该公共Windows控件上。成员变量的方式创建属性会自动创建OnXXXXChanged方法,当该属性变动时,此方法便会调用,注意方法结束时要调用SetModifiedFlag()通知控件属性的改变。get\set方式创建的属性会构造get、set方法。

二.事件的添加

ActiveX控件的事件处理是这样的(摘自msdn):"Windows controls typically send certain window messages to their parent window. Some of these messages, such as WM_COMMAND, provide notification of an action by the user. Others, such as WM_CTLCOLOR, are used to obtain information from the parent window. An ActiveX control usually communicates with the parent window by other means. Notifications are communicated by firing events (sending event notifications), and information about the control container is obtained by accessing the container's ambient properties. Because these communication techniques exist, ActiveX control containers are not expected to process any window messages sent by the control.

To prevent the container from receiving the window messages sent by a subclassed Windows control,COleControl creates an extra window to serve as the control's parent. This extra window, called a "reflector," is created only for an ActiveX control that subclasses a Windows control and has the same size and position as the control window. The reflector window intercepts certain window messages and sends them back to the control. The control, in its window procedure, can then process these reflected messages by taking actions appropriate for an ActiveX control (for example, firing an event). See Reflected Window Message IDs for a list of intercepted windows messages and their corresponding reflected messages.”

大意是这样的:控件收到windows message会传递给它的父类,当ActiveX继承自一个公共Windows控件时,为了防止这个ActiveX控件的容器(理解为父类)接收到windows message,COleControl创建了一个"reflector" 来拦截Windows message,翻译后再传给ActiveX控件。所以我们在ActiveX控件中处理的都是OCM_XXX message,而不是WM_XXX message。

BOOL CButtonCtrl::PreTranslateMessage(MSG* pMsg)
{
 // give container a shot at processing accelerators
 if (pMsg->message >= WM_KEYFIRST && pMsg->message <= WM_KEYLAST)
  if (::OleTranslateAccelerator(m_pInPlaceFrame,&m_frameInfo,pMsg)==S_OK)
   return TRUE;

 return COleControl::PreTranslateMessage(pMsg);
}

这便是ActiveX处理Windows message的原理,那么ActiveX控件暴露给容器处理的事件是什么呢?

右击CButtonCtrl->添加->事件,用以在DButtonEvents接口添加暴露给容器的事件,我们会看到源程序中多了两行:

EVENT_CUSTOM_ID("Click", DISPID_CLICK, FireClick, VTS_NONE)

void FireClick()
  {FireEvent(DISPID_CLICK,EVENT_PARAM(VTS_NONE));}

EVENT_CUSTOM_ID宏定义了事件名和事件标号的对应关系,FireClick中只调用FireEvent用以触发DISPID_CLICK事件。事件是这样调用的,ActiveX控件捕获OCM_XXX message(BN_CLICKED)再调用FireClick函数触发DISPID_CLICK事件,而DISPID_CLICK的触发会调用容器中的响应方法,这是一个回调的过程。为了建立这样的关系链,我们还需添加代码:

ON_MESSAGE(OCM_COMMAND, OnOcmCommand)

LRESULT CButtonCtrl::OnOcmCommand(WPARAM wParam, LPARAM lParam)
{
 #ifdef _WIN32
  WORD wNotifyCode = HIWORD(wParam);
  lParam;
 #else
  WORD wNotifyCode = HIWORD(lParam);
  wParam;
 #endif
   switch (wNotifyCode)
   {
   case BN_CLICKED:
    // Fire click event when button is clicked
    //FireClick();
    break;
   }
 return 0;
}

至此,事件就从控件传到了容器,中间经过了2次转换:WM message->OCM message,OCM message->自定义事件。

 

以下关于ActiveX的介绍摘自msdn:

An ActiveX control is a reusable software component based on the Component Object Model (COM) that supports a wide variety of OLE functionality and can be customized to fit many software needs. ActiveX controls are designed for use both in ordinary ActiveX control containers and on the Internet, in World Wide Web pages. You can create ActiveX controls either with MFC, described here, or with the Active Template Library (ATL).

An ActiveX control can draw itself in its own window, respond to events (such as mouse clicks), and be managed through an interface that includes properties and methods similar to those in Automation objects.

These controls can be developed for many uses, such as database access, data monitoring, or graphing. Besides their portability, ActiveX controls support features previously not available to ActiveX controls, such as compatibility with existing OLE containers and the ability to integrate their menus with the OLE container menus. In addition, an ActiveX control fully supports Automation, which allows the control to expose read\write properties and a set of methods that can be called by the control user.

You can create windowless ActiveX controls and controls that only create a window when they become active. Windowless controls speed up the display of your application and make it possible to have transparent and nonrectangular controls. You can also load ActiveX control properties asynchronously.

An ActiveX control is implemented as an in-process server (typically a small object) that can be used in any OLE container. Note that the full functionality of an ActiveX control is available only when used within an OLE container designed to be aware of ActiveX controls. See Port ActiveX Controls to Other Applications for a list of containers that support ActiveX controls. This container type, hereafter called a "control container," can operate an ActiveX control by using the control's properties and methods, and receives notifications from the ActiveX control in the form of events. The following figure demonstrates this interaction.

Interaction Between an ActiveX Control Container and a Windowed ActiveX Control:



ActiveX控件

Basic Components of an ActiveX Control

An ActiveX control uses several programmatic elements to interact efficiently with a control container and with the user. These are class COleControl, a set of event-firing functions, and a dispatch map.

Every ActiveX control object you develop inherits a powerful set of features from its MFC base class, COleControl. These features include in-place activation, and Automation logic.COleControl can provide the control object with the same functionality as an MFC window object, plus the ability to fire events. COleControl can also provide windowless controls, which rely on their container for help with some of the functionality a window provides (mouse capture, keyboard focus, scrolling), but offer much faster display.

Because the control class derives from COleControl, it inherits the capability to send, or "fire," messages, called events, to the control container when certain conditions are met. These events are used to notify the control container when something important happens in the control. You can send additional information about an event to the control container by attaching parameters to the event. For more information about ActiveX control events, see the article MFC ActiveX Controls: Events.

The final element is a dispatch map, which is used to expose a set of functions (called methods) and attributes (called properties) to the control user. Properties allow the control container or the control user to manipulate the control in various ways. The user can change the appearance of the control, change certain values of the control, or make requests of the control, such as accessing a specific piece of data that the control maintains. This interface is determined by the control developer and is defined using Class View. For more information on ActiveX control methods and properties, see the articles MFC ActiveX Controls: Methods and Properties.


Interaction Between Controls with Windows and ActiveX Control Containers

When a control is used within a control container, it uses two mechanisms to communicate: it exposes properties and methods, and it fires events. The following figure demonstrates how these two mechanisms are implemented.



ActiveX控件

 

The previous figure also illustrates how other OLE interfaces (besides automation and events) are handled by controls.

All of a control's communication with the container is performed by COleControl. To handle some of the container's requests, COleControl will call member functions that are implemented in the control class. All methods and some properties are handled in this way. Your control's class can also initiate communication with the container by calling member functions of COleControl. Events are fired in this manner.

Active and Inactive States of an ActiveX Control

A control has two basic states: active and inactive. Traditionally, these states were distinguished by whether the control had a window. An active control had a window; an inactive control did not. With the introduction of windowless activation, this distinction is no longer universal, but still applies to many controls.

When a windowless control goes active, it invokes mouse capture, keyboard focus, scrolling, and other window services from its container. You can also provide mouse interaction to inactive controls, as well as create controls that wait until activated to create a window.

When a control with a window becomes active, it is able to interact fully with the control container, the user, and Windows. The figure below demonstrates the paths of communication between the ActiveX control, the control container, and the operating system.

 

 



ActiveX控件 


转载于:https://my.oschina.net/u/1469992/blog/264755

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值