龙盟编程博客 | 无障碍搜索 | 云盘搜索神器
快速搜索
主页 > 软件开发 > VC开发 >

vc++技术内幕(第四版)笔记(第8章)(4)

时间:2009-12-30 15:42来源:未知 作者:admin 点击:
分享到:
代码二:(是为了比较代码一做些说明的) void CActiveXDialog::DoDataExchange(CDataExchange* pDX) { CDialog::DoDataExchange(pDX);//调用基类的DoDataExchange //{{AFX_DATA_MAP(CActiv

代码二:(是为了比较代码一做些说明的)

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.

收藏文章
表情删除后不可恢复,是否删除
取消
确定
图片正在上传,请稍后...
评论内容为空!
还没有评论,快来抢沙发吧!
按钮 内容不能为空!
立刻说两句吧! 查看0条评论
精彩图集

赞助商链接