2011-04-08 102 views
3

我正在開發一個C++項目,並有一個CPropertyPage::OnOk()方法。如何防止使用CPropertyPage :: OnOk()關閉MFC對話框的窗口?

我想什麼發生的是,當用戶點擊應用,程序將執行檢查,如果檢查是錯誤的,它會從關閉剿窗口。

我該如何去關閉窗戶?

我試過簡單的回報,但沒有去。

例如:

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

回答

2

使用以下命令檢查值A>值B然後返回0以停止關閉!

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

    return CPropertyPage::OnKillActive(); 
} 
1

一個簡單的迴歸應該做的伎倆,通過從this page on 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,這將按照你所描述的關閉窗口。

+1

如果您想查看,我對第一篇文章進行了編輯。 – 2011-04-08 14:07:19

+0

謝謝。你可以在你的CApptEditGen :: OnOK()函數的第一行添加一個AfxMessageBox調用,以便我們可以明確地看到它被調用嗎? (或者通過調試器來完成它。) – razlebe 2011-04-08 14:16:58

+0

它被稱爲我知道的。我相信我發現了我的問題,prealloc(&m_ai-> apapallocate)|| Dummy_aftalloc(m_ai)== REDO返回不正確的值,從而導致問題。 – 2011-04-08 14:18:08