简体   繁体   English

如果有错误,CPropertyPage 对话 OnOk 不应关闭对话

[英]CPropertyPage dialogue OnOk should not close the dialogue if there is an error

I have a class which inherits from CPropertyPage class.我有一个继承自CPropertyPage类的类。 I have a OnOk() method and a OnKillActive() method.我有一个OnOk()方法和一个OnKillActive()方法。 Whenever I press Ok on the dialogue.每当我在对话上按 Ok 时。 OnKillActive() gets called but OnOk() is never called. OnKillActive()被调用,但OnOk()从未被调用。 The problem is depending on a condition I do not want to close the dialogue on pressing Ok.问题取决于我不想在按下 Ok 时关闭对话的条件。 But the dialogue is closing on pressing Ok.但是对话即将结束,按下 Ok。

How do I prevent the dialogue from closing when I press Ok?当我按下 Ok 时,如何防止对话框关闭?

Code:代码:

In MyClass.h:
    class MyClass : public CPropertyPage {
    }

In MyClass.cpp:
    void MyClass::OnOK(){
        if (condition true) {
            return; // This should prevent the dialogue from closing but still      the dialogue closes
        }
        return CPropertyPage::OnOk();
    }

    BOOL MyClass::OnKillActive() {
        if (condition true) {
            CDialog::DoModal();
            return FALSE; // This should prevent the dialogue from closing but   still the dialogue closes
        }
        return CPropertyPage::OnKillActive();
    }

I am not sure if you can call CDialog::DoModal();我不确定你是否可以调用CDialog::DoModal(); as your property page is not closed yet.因为您的属性页面尚未关闭。

When this event( OnKillActive() ) occurs your property page is inactive .当此事件( OnKillActive() )发生时,您的属性页处于非活动状态 But your property page still exists and the data in the property page also exists for validation.但是您的属性页仍然存在,并且属性页中的数据也存在用于验证。

To bring your page back, just set the focus in one of the dialog item.要返回您的页面,只需将焦点设置在对话框项目之一中即可。 Use " GetDlgItem " to get an object and set the focus using " SetFocus "使用“ GetDlgItem ”获取对象并使用“ SetFocus ”设置焦点

An example is available here.此处提供了一个示例。

https://msdn.microsoft.com/en-us/library/2122ct0z.aspx https://msdn.microsoft.com/en-us/library/2122ct0z.aspx

Actually in the OnClickedOk() function of the PropertySheet class, there was an EndDialog(IDOK) .实际上在PropertySheet类的OnClickedOk()函数中,有一个EndDialog(IDOK) This is why it was closing everytime when Ok was pressed.这就是为什么每次按下 Ok 时它都会关闭的原因。

I just gave a condition check before EndDialog() and it worked.我只是在EndDialog()之前进行了条件检查并且它起作用了。 Thanks for your reply.感谢您的回复。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM