设为首页 加入收藏

TOP

21.4.6 实现对话框切换(1)
2013-10-07 12:42:55 来源: 作者: 【 】 浏览:70
Tags:21.4.6 实现 对话 切换

21.4.6  实现对话框切换(1)

在图21-12中已经了解了该程序中切换对话框的基本逻辑。按钮单击是从一个对话框切换到下一个对话框的机制,因此按钮处理程序应该包含使切换发生的代码。首先定义视图ID来标识这3个对话框中的每一个,因此添加一个名为ViewConstants.h的头文件。这次可以尝试使用枚举声明来标识这些视图,因此该文件应该包含下面的代码:

  1. // Definition of constants identifying the record views  
  2.  
  3. #pragma once  
  4.  
  5. enum ViewID{ ORDER_DETAILS, NEW_ORDER, SELECT_PRODUCT}; 

在CMainFrame类中需要有一个ViewID类型的变量来记录当前视图的ID,因此通过在Class View中右击CMainFrame,并从弹出菜单中选择Add | Add Variable,添加变量m_CurrentViewID。需要初始化该变量,因此将CMainFrame类的构造函数修改成下面这样:

  1. CMainFrame::CMainFrame() : m_CurrentViewID(ORDER_DETAILS)  
  2. {  
  3. // TODO: add member initialization code here  

这样将确定应用程序开始时总是要显示的视图。ViewConstants.h头文件的#include指令被自动添加到MainFrm.h文件中,从而使视图ID的定义在MainFrm.cpp文件中可用。

现在可以给CMainFrame类添加执行对话框之间切换操作的成员函数SelectView()。其返回类型是void,单个形参的类型是ViewID,因为实参是枚举声明中定义的视图ID之一。SelectView()函数的实现如下所示:

  1. // Enables switching between views. The argument specifies the new view  
  2. void CMainFrame::SelectView(ViewID viewID)  
  3. {  
  4. CView* pOldActiveView = GetActiveView(); // Get current view  
  5.  
  6. // Get pointer to new view if it exists  
  7. // if it doesn't the pointer will be null  
  8. CView* pNewActiveView = static_cast<CView*>(GetDlgItem(viewID));  
  9.  
  10. // If this is first time around for the new view, the new view  
  11. // won't exist, so we must create it  
  12. // The Order Details view is always created first so we don't need  
  13. // to provide for creating that.  
  14. if (pNewActiveView == NULL)  
  15. {  
  16. switch(viewID)  
  17. {  
  18. case NEW_ORDER:     // Create view to add new order  
  19. pNewActiveView = new CCustomerView;  
  20. break;  
  21. case SELECT_PRODUCT:    // Create view to add product to order  
  22. pNewActiveView = new CProductView;  
  23. break;  
  24. default:  
  25. AfxMessageBox(_T("Invalid View ID"));  
  26. return;  
  27. }  
  28.  
  29. // Switching the views  
  30. // Obtain the current view context to apply to the new view  
  31. CCreateContext context;  
  32. context.m_pCurrentDoc = pOldActiveView->GetDocument();  
  33. pNewActiveView->Create(NULL, NULL, 0L, CFrameWnd::rectDefault,  
  34. this, viewID, &context);  
  35. pNewActiveView->OnInitialUpdate();  
  36. }  
  37. SetActiveView(pNewActiveView); // Activate the new view  
  38. pOldActiveView->ShowWindow(SW_HIDE); // Hide the old view  
  39. pNewActiveView->ShowWindow(SW_SHOW); // Show the new view  
  40. pOldActiveView->SetDlgCtrlID(m_CurrentViewID); // Set the old view ID  
  41. pNewActiveView->SetDlgCtrlID(AFX_IDW_PANE_FIRST);  
  42. m_CurrentViewID = viewID; // Save the new view ID  
  43. RecalcLayout();  

这里的代码引用了CCustomerView和CProductView类,因此源文件中需要有嵌入CustomerView.h和ProductView.h的#include指令。

从订单细节对话框切换到开始新订单创建的对话框,该操作是在COrderDetailsView类的OnNeworder()处理程序中完成的:

  1. void COrderDetailsView::OnNeworder()  
  2. {  
  3. static_cast<CMainFrame*>(GetParentFrame())->SelectView(NEW_ORDER);  

】【打印繁体】【投稿】【收藏】 【推荐】【举报】【评论】 【关闭】 【返回顶部
分享到: 
上一篇21.4.6 实现对话框切换(2) 下一篇9.7 类类型之间的强制转换

评论

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