设为首页 加入收藏

TOP

21.4.4 创建记录集视图(2)
2013-10-07 12:43:47 来源: 作者: 【 】 浏览:65
Tags:21.4.4 创建 记录

21.4.4  创建记录集视图(2)

这里的显式强制转换是必要的,因为转换是在类层次结构中从基类向派生类进行的。稍后即可实现其他两个重写的函数。

因为CCustomerSet对象是在堆上创建的,所以需要在CCustomerView类的析构函数中将其删除:

  1. CCustomerView::~CCustomerView()  
  2. {  
  3. if (m_pSet)  
  4. delete m_pSet;  

CCustomerView类中的OnInitialUpdate()函数重写应该像前面看到的那样实现:

  1. void CCustomerView::OnInitialUpdate()  
  2. {  
  3. BeginWaitCursor();  
  4. GetRecordset();  
  5. CRecordView::OnInitialUpdate();  
  6. if (m_pSet->IsOpen())  
  7. {  
  8. CString strTitle = m_pSet->m_pDatabase->GetDatabaseName();  
  9. CString strTable = m_pSet->GetTableName();  
  10. if(!strTable.IsEmpty())  
  11. strTitle += _T(":") + strTable;  
  12. GetDocument()->SetTitle(strTitle);  
  13. }  
  14. EndWaitCursor();  

CProductView类的定义与CCustomerView类几乎相同,主要区别在于与其相关的对话框窗体:

  1. // ProductView.h : header file  
  2. #pragma once  
  3.  
  4. class CProductSet;  
  5.  
  6. class CProductView : public CRecordView  
  7. {  
  8. public:  
  9. CProductView();  
  10. virtual ~CProductView();  
  11.  
  12. public:  
  13. enum { IDD = IDD_PRODUCT_FORM }; // Form Data  
  14. CProductSet* m_pSet;  
  15.  
  16. // Operations  
  17. public:  
  18. CProductSet* GetRecordset();  
  19.  
  20. // Implementation  
  21. protected:  
  22. #ifdef _DEBUG  
  23. virtual void AssertValid() const;  
  24. virtual void Dump(CDumpContext& dc) const;  
  25. #endif  
  26. }; 

现在,可以使用为上一个类使用的方法,添加OnGetRecordset()、DoDataExchange()和OnInitialUpdate()函数的重写版本,并在ProductView.cpp文件的开始处添加下面的指令:

  1. #include "stdafx.h"  
  2. #include "DBSimpleUpdate.h"  
  3. #include "ProductView.h"  
  4. #include "ProductSet.h" 

构造函数应该初始化m_pSet成员,析构函数应该删除m_pSet指向的对象-- 因为该对象是在堆上创建的。构造函数和析构函数的定义如下:

  1. CProductView::CProductView()  
  2. : CRecordView(CProductView::IDD),  
  3. m_pSet(NULL)  
  4. {  
  5. }  
  6.  
  7. CProductView::~CProductView()  
  8. {  
  9. if (m_pSet)  
  10. delete m_pSet;  

OnGetRecordset()函数的实现是:

  1. CRecordset* CProductView::OnGetRecordset()  
  2. {  
  3. if(m_pSet == NULL) // If there is no recordset  
  4. {  
  5. m_pSet = new CProductSet(NULL); // create one  
  6. m_pSet->Open(); // then open it  
  7. }  
  8. return m_pSet; // Return the address of the recordset  

CProductView类中GetRecordset()函数的实现基本上也与CCustomerView类的情况相同:

  1. CProductSet* CProductView::GetRecordset()  
  2. {  
  3. return static_cast<CProductSet*>(OnGetRecordset());  

还可以添加将在调试模式中使用的诊断函数的定义:

  1. #ifdef _DEBUG  
  2. void CProductView::AssertValid() const  
  3. {  
  4. CRecordView::AssertValid();  
  5. }  
  6.  
  7. void CProductView::Dump(CDumpContext& dc) const  
  8. {  
  9. CRecordView::Dump(dc);  
  10. }  
  11. #endif //_DEBUG 

我们将在本章稍后再返回到需要在CProductView类中实现的函数,在此期间可以给对话框添加所需要的控件。

】【打印繁体】【投稿】【收藏】 【推荐】【举报】【评论】 【关闭】 【返回顶部
分享到: 
上一篇21.4.5 给对话框资源添加控件 下一篇21.4.4 创建记录集视图(1)

评论

帐  号: 密码: (新用户注册)
验 证 码:
表  情:
内  容: