教学文库网 - 权威文档分享云平台
您的当前位置:首页 > 精品文档 > 学前教育 >

VC++技术内幕(windows编程篇)(11)

来源:网络收集 时间:2026-07-12
导读: 原型:CDataExchange::CDataExchange(CWnd* pDlgWnd, BOOL bSaveAndValidate); 定义: CDataExchange::CDataExchange(CWnd* pDlgWnd, BOOL bSaveAndValidate) { ASSERT_VALID(pDlgWnd); m_bSaveAndValidate = bSave

原型: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(),\ HELP_FINDER,0);

…… 此处隐藏:3160字,全部文档内容请下载后查看。喜欢就下载吧 ……
VC++技术内幕(windows编程篇)(11).doc 将本文的Word文档下载到电脑,方便复制、编辑、收藏和打印
本文链接:https://www.jiaowen.net/wendang/592946.html(转载请注明文章来源)
Copyright © 2020-2025 教文网 版权所有
声明 :本网站尊重并保护知识产权,根据《信息网络传播权保护条例》,如果我们转载的作品侵犯了您的权利,请在一个月内通知我们,我们会及时删除。
客服QQ:78024566 邮箱:78024566@qq.com
苏ICP备19068818号-2
Top
× 游客快捷下载通道(下载后可以自由复制和排版)
VIP包月下载
特价:29 元/月 原价:99元
低至 0.3 元/份 每月下载150
全站内容免费自由复制
VIP包月下载
特价:29 元/月 原价:99元
低至 0.3 元/份 每月下载150
全站内容免费自由复制
注:下载文档有可能出现无法下载或内容有问题,请联系客服协助您处理。
× 常见问题(客服时间:周一到周五 9:30-18:00)