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

发布于 2024-10-31 08:22:08 字数 483 浏览 0 评论 0原文

我正在开发一个 C++ 项目,并且有一个 CPropertyPage::OnOk() 方法。

我希望发生的是,当用户单击确定应用时,程序将执行检查,如果检查错误,它将禁止窗口关闭。

我该如何阻止窗口关闭?

我尝试过简单的返回,但没有成功。

例如:

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();  
}

I'm working on a C++ project and have a CPropertyPage::OnOk() method.

What I'd like to happen is when the user clicks Ok or Apply, the program will perform a check, if the check is wrong, it'll supress the window from closing.

How would I go about to supressing the window from closing?

I've tried a simple return but no go.

For example:

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();  
}

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

半暖夏伤 2024-11-07 08:22:08

使用以下命令检查值 A > 是否为然后值 B 返回 0 以停止关闭!

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

    return CPropertyPage::OnKillActive();
}

Used the following to check if value A > value B then return 0 to stop from closing!

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

    return CPropertyPage::OnKillActive();
}
山有枢 2024-11-07 08:22:08

简单的返回就可以解决问题,如下面来自 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.
}

您绝对确定已正确重写 CPropertyPage 上的 OnOK() 吗?如果没有,则将调用默认的 CPropertyPage::OnOK,这将按照您的描述关闭窗口。

A simple return should do the trick, as demonstrated by the code snippet below from this page on MSDN, which describes the OnOK() function of CDialog (from which CPropertyPage is derived):

/* 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.
}

Are you absolutely sure you've correctly overriden OnOK() on CPropertyPage? If not then the default CPropertyPage::OnOK will be called, which will close the window as you describe.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文