设为首页 加入收藏

TOP

21.3.1 实现更新模式(2)
2013-10-07 12:43:23 来源: 作者: 【 】 浏览:57
Tags:21.3.1 实现 更新 模式

21.3.1  实现更新模式(2)

2. 修改按钮标签

可以给视图类添加一个控件数据成员m_EditOrderCtrl,从而得到对应于Edit Order按钮的对象;添加方法与为编辑控件添加变量的方法完全相同。该变量的类型是CButton-- 这是定义按钮的MFC类。可以使用该变量,调用CButton类中从CWnd继承的SetWindowText()成员,从而设置按钮标签:

  1. void COrderDetailsView::OnEditorder()  
  2. {  
  3. if(m_Mode == UPDATE)  
  4. { // When button was clicked we were in update  
  5. // Disable input to edit controls  
  6. m_QuantityCtrl.SetReadOnly();  
  7. m_DiscountCtrl.SetReadOnly();  
  8.  
  9. // Change the Update button text to Edit Order  
  10. m_EditOrderCtrl.SetWindowText(_T("Edit Order"));  
  11.  
  12. // Make the Cancel button invisible  
  13. // Enable Record menu items and toolbar buttons  
  14. // Complete the update  
  15. m_Mode = READ_ONLY; // Change to read-only mode  
  16. }  
  17. else  
  18. { // When button was clicked we were in read-only mode  
  19.  
  20. // Enable input to edit controls  
  21. m_QuantityCtrl.SetReadOnly(FALSE);  
  22. m_DiscountCtrl.SetReadOnly(FALSE);  
  23.  
  24. // Change the Edit Order button text to Update  
  25. m_EditOrderCtrl.SetWindowText(_T("Update"));  
  26.  
  27. // Make the Cancel button visible  
  28. // Disable Record menu items and toolbar buttons  
  29. // Start the update  
  30. m_Mode = UPDATE; // Switch to update mode  
  31. }  

每次调用SetWindowText()函数,都会将按钮上显示的文本设置为给该函数提供的实参字符串。形参的类型是LPCTSTR,可以给它提供CString类型的实参或_T类型的字符串常量。

3. 控制Cancel按钮的可见性

为了使Cancel按钮可见或不可见,需要使用某个对应于该按钮的控件变量,因此就像前面为Edit Order按钮添加变量那样,添加一个名为m_CancelEditCtrl的控件变量。因为CButton类是从CWnd类派生的,所以可以调用CButton对象中继承的ShowWindow()成员,将该按钮设定为可见或不可见,相应的代码如下所示:

  1. void COrderDetailsView::OnEditorder()  
  2. {  
  3. if(m_Mode == UPDATE)  
  4. { // When button was clicked we were in update  
  5. // Disable input to edit controls  
  6. m_QuantityCtrl.SetReadOnly();  
  7. m_DiscountCtrl.SetReadOnly();  
  8.  
  9. // Change the Edit Order button text to Update  
  10. m_EditOrderCtrl.SetWindowText(_T("Edit Order"));  
  11.  
  12. // Make the Cancel button invisible  
  13. m_CancelEditCtrl.ShowWindow(SW_HIDE);  
  14.  
  15. // Enable Record menu items and toolbar buttons  
  16. // Complete the update  
  17. m_Mode = READ_ONLY;     // Change to read-only mode  
  18. }  
  19. else  
  20. { // When button was clicked we were in read-only mode  
  21.  
  22. // Enable input to edit controls  
  23. m_QuantityCtrl.SetReadOnly(FALSE);  
  24. m_DiscountCtrl.SetReadOnly(FALSE);  
  25.  
  26. // Change the Edit Order button text to Update  
  27. m_EditOrderCtrl.SetWindowText(_T("Update"));  
  28.  
  29. // Make the Cancel button visible  
  30. m_CancelEditCtrl.ShowWindow(SW_SHOW);  
  31.  
  32. // Disable Record menu items and toolbar buttons  
  33. // Start the update  
  34. m_Mode = UPDATE;    // Switch to update mode  
  35. }  

CButton类从CWnd继承的ShowWindow()函数要求实参是int类型,还必须是一组固定值中的一个(参见文档了解完整的数值集)。如果m_Mode的值是UPDATE,我们就使用实参值SW_HIDE使该按钮消失;当应用程序进入编辑模式时,我们使用实参值SW_SHOW使该按钮可见,并将其激活。

】【打印繁体】【投稿】【收藏】 【推荐】【举报】【评论】 【关闭】 【返回顶部
分享到: 
上一篇21.3.1 实现更新模式(3) 下一篇21.4.3 创建记录集

评论

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