荔园在线

荔园之美,在春之萌芽,在夏之绽放,在秋之收获,在冬之沉淀

[回到开始] [上一篇][下一篇]


发信人: EDE (Thinker醒客), 信区: SoftDev
标  题: [转]VC++技术内幕(第四版)笔记(第8章)
发信站: 荔园晨风BBS站 (Wed Mar  8 09:13:23 2006), 站内


 /************************************/
第八章:使用ActiveX控件


1,ActiveX控件是一个直接插入到C++程序中的软件模块,以前常称OLE控件(OCX),是基
于MS-COM技术。

2,ActiveX控件与普通Windows控件比较:
相同点:ActiveX控件也可看成是一个子窗口(可以看成这样的)。
如果想在对话框上加入ActiveX控件,则只要在对话框编辑器中,把ActiveX控件放在适当的
位置上,并在资源模板中标识该控件。如果要在运行的过程中建立ActiveX控件,则可以调
用响应控件类的Create成员函数,而且通常在父窗口的WM_CREATE消息控制函数中调用。
不同点:属性和方法。
ActiveX控件不像普通控件那样发送以WM_打头的通知消息给它的包容器窗口,而是激发事件
。事件实际上是由控件调用包容器函数。像普通的控件通知消息一样,事件并没有返回值传
给ActiveX控件。事件如lick,KeyDown。但对于客户来说时间与控件的通知消息是一样的。

3,在MFC库中,ActiveX控件就像子窗口一样,但在控件窗口和包容器窗口之间有一层重要
代码。实际上,ActiveX控件可能没有窗口。当调用Create函数时,并不是直接建立控件窗
口,而是把控件代码载入进来,并激发一个“实地激活”(in-place activation)命令。然
后ActiveX控件再建立它自己的窗口,通过MFC的CWnd类指针我们可以访问该窗口。不过客户
程序最好不要使用ActiveX控件的hWnd句柄。

4,通常ActiveX控件会保存在扩展名为OCX的动态连接库中。包容器程序回根据Windows注册
表利用COM技术在需要的时候装入动态连接库。
说明:
1)暂时可以这样认为,如果使用了ActiveX控件,那么在运行时候要装入该ActiveX控件代
码。显然在发布含有ActiveX控件的程序时候,必须要包含相应的OCX文件,而且还得提供一
个合适的安装程序。

5,安装ActiveX控件:
1)把找到的ActiveX控件动态连接库拷到硬盘上。
2)在WINDOWS注册表中登记注册。(可使用Regsvr32命令行命令)
3)在使用该控件的项目中安装该控件。(选择Project菜单,再选择Add To Project,再选
择Components And Controls,再选择Registered ActiveX Controls,这时列表框列出系统
已经注册所有的ActiveX控件,选择需要的控件INSERT即可。)

6,ActiveX控件包容器编程:
1)不管ActiveX控件是作为对话框控件,还是做为“子窗口”,MFC和ClassWizard都支持。
2)ActiveX控件编写者设计了ActiveX控件属性供使用者在设计时访问。所有的ActiveX控件
属性(包括设计时属性),在运行时都是可以访问的,不过有些属性可能被设计成只读的。
3)当在项目中插入ActiveX控件时,ClassWizard就会产生相应的CWnd的派生类C++类,来满
足对空间的方法和属性进行访问要求。控件的属性和方法都有相应的成员函数,同时生成的
类还有一个构造函数可用以动态创建ActiveX控件的事例。
4)当在项目中插入ActiveX控件ClassWizard生成的CWnd的派生类C++类中,可以看到其成员
函数的代码中都有对InvokeHelper函数的调用,InvokeHelper函数的第一个参数都和对应的
属性或方法在ActiveX控件中的分发(dispatch)ID(标识ActiveX控件的方法或属性的)相对
应。通过查看ActiveX控件hlp文件可以发现,ActiveX控件的方法在生存的C++类中都有同名
的成员函数与之对应,ActiveX控件的属性都有一组Get和Set函数对其操作,其中ActiveX控
件的方法和属性操作与生成的C++类成员函数相关联都是通过InvokeHelper函数的调用来完
成的,InvokeHelper函数的第一个参数是由Component Gallery(控件提供者)提供的。因
为经过这样的处理,所以我们如果要调用ActiveX控件的方法或对其属性进行取和设置操作
,只需调用生成的C++类对应的成员函数便可。
下面对InvokeHelper单独说明:
CWnd::InvokeHelper
void InvokeHelper( DISPID dwDispID, WORD wFlags, VARTYPE vtRet, void* pvRet,
const BYTE* pbParamInfo, ... );
说明:
Call this member function to invoke the OLE control method or property
specified by dwDispID, in the context specified by wFlags.
其中参数:
dwDispID:
//Identifies the method or property to be invoked. This value is usually
supplied by Component Gallery.

wFlags:可以为下面些值,指明调用InvokeHelper的目的。
//[ DISPATCH_METHOD ]   The member is invoked as a method. If a property has
the same name, both this and the DISPATCH_PROPERTYGET flag may be set.
[ DISPATCH_PROPERTYGET ] The member is retrieved as a property or data member.
[ DISPATCH_PROPERTYPUT ] The member is changed as a property or data member.
[ DISPATCH_PROPERTYPUTREF ] The member is changed by a reference assignment,
rather than a value assignment. This flag is valid only when the property
accepts a reference to an object.

vtRet:
//Specifies the type of the return value.
VT_EMPTY  void
VT_I2  short
VT_I4  long
VT_R4  float
VT_R8  double
VT_CY  CY
VT_DATE  DATE
VT_BSTR  BSTR
VT_DISPATCH  LPDISPATCH
VT_ERROR  SCODE
VT_BOOL  BOOL
VT_VARIANT VARIANT
VT_UNKNOWN  LPUNKNOWN

pvRet:
//Address of the variable that will that will receive the property value or
return value. It must match the type specified by vtRet.

pbParamInfo:一般都设置为NULL
//Pointer to a null-terminated string of bytes specifying the types of the
parameters following pbParamInfo.
specifies the types of the parameters passed to the method or property.
...:
//Variable List of parameters, of types specified in pbParamInfo.

5)AppWizard对ActiveX控件的支持是通过在生成的应用程序类的成员函数InitInstance中
插入(AfxEnableControlContainer();),同时在响应项目文件的StdAfx.h文件中插入(
#include<afxdisp.h>)(原因可参考书P38一些说明)。
如果项目中不包含这两行,而又要加入ActiveX控件,则只要手工加入上面两行代码即可。

6)可以对话框编辑器来生成对话框的模板中加入一个或多个ActiveX控件,这样我们可以在
对话框模板生成的类中添加数据成员或事件控制函数来获取ActiveX控件的属性或对其控制

注意:(详细见书P159页的[致WIN32程序员])
实际上,资源模板并不是在对话框编辑器中所看的那样。函数CDialog::DoModal在把对话框
模板交给WINDOWS内部的对话框过程之前,要先对模板进行预处理,即:它先会去掉所有的
ActiveX控件,有剩下的控件建立对话框窗口,然后再装入ActiveX控件,激活它们并在正确
的位置上创建它的窗口。
当模式对话框运行时候,MFC处理所有送给对话框消息时,是不管是有普通控件发送的,还
是ActiveX控件发送的。故ActiveX控件虽然不是对话框模板一部分,但用户仍然可以用TAB
键在所有的控件间切换。

7)调用UpdateData(FALSE)将会从所有的对话框控件中读取所有属性值。如果只需要得到
ActiveX控件属性的话,可以调用ActiveX控件生成的C++类中Get函数(同样设置调用Set函
数),这样就提高了效率。

8)事例部分代码对比说明:
代码一:
   CDataExchange dx(this,TRUE);
   DDX_Text(&dx,IDC_DAY,m_sDay);
   DDX_Text(&dx,IDC_MONTH,m_sMonth);
   DDX_Text(&dx,IDC_YEAR,m_sYear);
说明一:
CDataExchange类构造函数:(注,在MFC|SRC|AFXWIN.H中可以看到其构造函数声明,在
MFC|SRC|WINCORE.CPP文件中可以看到其构造函数的定义。)
原型:CDataExchange::CDataExchange(CWnd* pDlgWnd, BOOL bSaveAndValidate);
定义:
CDataExchange::CDataExchange(CWnd* pDlgWnd, BOOL bSaveAndValidate)
{
 ASSERT_VALID(pDlgWnd);
 m_bSaveAndValidate = bSaveAndValidate;
 m_pDlgWnd = pDlgWnd;
 m_hWndLastControl = NULL;
}

//其中m_pDlgWnd和m_bSaveAndValidate是CDataExchange数据成员,可以通过这中方式给它
们赋值。
m_pDlgWnd:The dialog box or window where the data exchange takes place.
m_bSaveAndValidate Flag for the direction of DDX and DDV. 详见说明二。

说明二:
//CDataExchange does not have a base class.
//The CDataExchange class supports the dialog data exchange (DDX) and dialog
data validation (DDV) routines used by the Microsoft Foundation classes. Use
this class if you are writing data exchange routines for custom data types or
controls, or if you are writing your own data validation routines.
//A CDataExchange object provides the context information needed for DDX and
DDV to take place. The flag m_bSaveAndValidate is FALSE when DDX is used to
fill the initial values of dialog controls from data members. The flag
m_bSaveAndValidate is TRUE when DDX is used to set the current values of dialog
controls into data members and when DDV is used to validate the data values. If
the DDV validation fails, the DDV procedure will display a message box
explaining the input error. The DDV procedure will then call Fail to reset the
focus to the offending control and throw an exception to stop the validation
process.


代码二:(是为了比较代码一做些说明的)
void CActiveXDialog::DoDataExchange(CDataExchange* pDX)
{
 CDialog::DoDataExchange(pDX);//调用基类的DoDataExchange
 //{{AFX_DATA_MAP(CActiveXDialog)
 DDX_Control(pDX, IDC_CALENDAR1, m_calendar);
 DDX_Text(pDX, IDC_DAY, m_sDay);
 DDX_Text(pDX, IDC_MONTH, m_sMonth);
 DDX_Text(pDX, IDC_YEAR, m_sYear);
 //}}AFX_DATA_MAP
}
说明一:
//DoDataExchange Called by the framework to exchange and validate dialog data.
//DoDataExchange Never call this function directly. It is called by the
UpdateData member function. Call UpdateData to initialize a dialog box’s
controls or retrieve data from a dialog box.
说明二:
//在MFC|SRC|WINCORE.CPP文件中可以看到UpdateData函数的定义
BOOL CWnd::UpdateData(BOOL bSaveAndValidate)
{ ...
 CDataExchange dx(this, bSaveAndValidate);//创建了一个CDataExchange对象,与当前
窗口相关联
 ...
 DoDataExchange(&dx); //注意:DoDataExchange是个虚函数。子类中如果有重写了,则调
用子类的。
 ...
}
说明三:
//在MFC|Include|AFXWIN2.INL文件中可有看到CWnd::DoDataExchange的如下定义(内联)

// CWnd dialog data support
_AFXWIN_INLINE void CWnd::DoDataExchange(CDataExchange*)
 { } // default does nothing
由此可见代码二中CDialog::DoDataExchange(pDX)调用好象是个‘摆设’,不起做任何事情
。框架设置DoDataExchange函数目的是在我们子窗口类(这里是对话筐)中重写它,添加代
码完成子窗口类(这里是对话筐)中数据成员与对话筐上控件的交互。
说明四:
如果结合UpdateData和DoDataExchange两函数整体来看,应该体会到这里代码一与代码二实
质上是一会事情。代码二只是借助了框架兜了些圈子。

说明四:
摘录MSDN中Dialog Data Exchange一些E文段落对上讨论做个总结:
Dialog Data Exchange

If you use the DDX mechanism, you set the initial values of the dialog object’
s member variables, typically in your OnInitDialog handler or the dialog
constructor. Immediately before the dialog is displayed, the framework’s DDX
mechanism transfers the values of the member variables to the controls in the
dialog box, where they appear when the dialog box itself appears in response to
DoModal or Create. The default implementation of OnInitDialog in CDialog calls
the UpdateData member function of class CWnd to initialize the controls in the
dialog box.

The same mechanism transfers values from the controls to the member variables
when the user clicks the OK button (or whenever you call the UpdateData member
function with the argument TRUE). The dialog data validation mechanism
validates any data items for which you specified validation rules.

UpdateData works in both directions, as specified by the BOOL parameter passed
to it. To carry out the exchange, UpdateData sets up a CDataExchange object and
calls your dialog class’s override of CDialog’s DoDataExchange member
function. DoDataExchange takes an argument of type CDataExchange. The
CDataExchange object passed to UpdateData represents the context of the
exchange, defining such information as the direction of the exchange.

When you (or ClassWizard) override DoDataExchange, you specify a call to one
DDX function per data member (control). Each DDX function knows how to exchange
data in both directions based on the context supplied by the CDataExchange
argument passed to your DoDataExchange by UpdateData.

MFC provides many DDX functions for different kinds of exchange.

If the user cancels a modal dialog box, the OnCancel member function terminates
the dialog box and DoModal returns the value IDCANCEL. In that case, no data is
exchanged between the dialog box and the dialog object.

9)当输入焦点在某ActiveX控件上时,按下F1键引起OnHelpInfo函数调用,可在
OnHelpInfo函数中设置帮助信息。
说明:
ClassWizard不能修改生成的ActiveX控件类,因而必须手工加入消息映射代码。事例代码如
下:
//在ActiveX控件类头文件中加入函数原型并声明消息映射表:
 protected:
  afx_msg BOOL OnHelpInfo(HELPINFO* pHelpInfo);
  DECLARE_MESSAGE_MAP()//在ActiveX控件类代码文件中添加消息映射及OnHelpInfo函数定
义:
 BEGIN_MESSAGE_MAP(CCalendar,CWnd)
  ON_WM_HELPINFO()
 END_MESSAGE_MAP()
/**
 BOOL CCalendar::OnHelpInfo(HELPINFO *pHelpInfo)
{
 ::WinHelp(GetSafeHwnd(),"C:\WINDOWS\system32\MSCAL.hlp",
    HELP_FINDER,0);
 return FALSE;
}

/////////////////
//////////////
///////////

7,在运行时创建ActiveX控件:
1)在项目中插入ActiveX控件。ClassWizard会生存相应的ActiveX控件类的文件。
2)在使用ActiveX控件的对话框或窗口类中添加ActiveX控件类数据成员。
3)重写CDialog::OnInitDialog(或其它窗口中响应WM_CREAT消息),在新的函数中调用
ActiveX控件类Create函数。
4)在父窗口类中,手工添加必要的与新控件有关的事件消息处理函数及原型,和相应的消
息映射。

/////////
8,更多的ActiveX控件编程参见P160-167页(ActiveX控件在HTML文件中使用 和 在运行时
创建ActiveX控件)。

--
            │
╭─-╮╭─-┤╭─-╮ 我不存在,存在的只有我的思想
│★ ││╭ ││★ │ 我要做一棵强壮的树,足以保护我爱的你April
├─-┘│╰ │├─-┘
╰─-╯╰─-┴╰─-╯


※ 来源:·荔园晨风BBS站 bbs.szu.edu.cn·[FROM: 192.168.111.118]


[回到开始] [上一篇][下一篇]

荔园在线首页 友情链接:深圳大学 深大招生 荔园晨风BBS S-Term软件 网络书店