如何防止我的 MFC 对话框的窗口使用 CPropertyPage::OnOk() 关闭?

How do I prevent my MFC dialog's window from closing using CPropertyPage::OnOk()?

本文关键字:OnOk CPropertyPage 关闭 我的 何防止 MFC 对话框 窗口      更新时间:2023-10-16

我正在做一个c++项目,有一个CPropertyPage::OnOk()方法。

我希望发生的是当用户点击OkApply时,程序将执行检查,如果检查错误,它将阻止窗口关闭。

我该如何阻止窗口关闭呢?

我试过一个简单的返回,但没有去。

例如:

void CApptEditGen::OnOK() 
{
    if ( prealloc(&m_ai->apapallocate) || Dummy_aftalloc(m_ai) == REDO ) {
        m_pCtl_ApptEdit_Units->SetFocus();
        m_pCtl_ApptEdit_Units->SetWindowText("");
        return;
    }
    CPropertyPage::OnOK();  
}

使用以下代码检查值A>值B,然后返回0停止关闭!

BOOL CApptEditGen::OnKillActive()
{
    CString inpValue;
    m_pCtl_ApptEdit_Units->GetWindowText(inpValue);
    if (atoi(inpValue) > freeUnitsAvailable)
        return 0;
    return CPropertyPage::OnKillActive();
}

一个简单的返回就可以完成这个任务,如下面的MSDN页面代码片段所示,它描述了CDialog的OnOK()函数(CPropertyPage是从它派生出来的):

/* MyDialog.cpp */
#include "MyDialog.h"
void CMyDialog::OnOK() 
{
   // TODO: Add extra validation here
   // Ensure that your UI got the necessary input 
   // from the user before closing the dialog. The 
   // default OnOK will close this.
   if ( m_nMyValue == 0 ) // Is a particular field still empty?
   {
      AfxMessageBox("Please enter a value for MyValue");
      return; // Inform the user that he can't close the dialog without
              // entering the necessary values and don't close the 
              // dialog.
   }
   CDialog::OnOK(); // This will close the dialog and DoModal will return.
}

你绝对确定你已经正确地覆盖了OnOK()在CPropertyPage?如果没有,那么默认的CPropertyPage::OnOK将被调用,这将关闭窗口,如您所述。