开发者

How to end a dialog mfc application from CDialog::OnInitDialog or immediately after this function ends?

开发者 https://www.devze.com 2023-04-12 15:19 出处:网络
In one case of my application, I want to end the application in the dialog\'s OnInitDialog or immediately after this function. Is there any way to do it?

In one case of my application, I want to end the application in the dialog's OnInitDialog or immediately after this function. Is there any way to do it?

I tried using windows messages – in OnInitDialog calling SendMessage and in the handler of the sent message calling OnClose – but it didn't do what I intende开发者_如何学God since the dialog still appeared.


PostQuitMessage(0) in the WM_INITDIALOG handler body will do the job.


You can simulate the user clicking the X in the corner of the dialog by sending a WM_SYSCOMMAND message:

PostMessage(WM_SYSCOMMAND, SC_CLOSE, 0);


The easiest solution:

    CMyDialog::OnInitDialog()
    {
       if( "My Condition is invalid" )
       {
         EndDialog(-1);
         return FALSE;
       }
    }

You have to do "return FALSE" juste after EndDialog because as you can read in the Microsoft documentation CDialog(classe), EndDialog doesn't close the dialog immediately, but generates a notification which when handled by Microsoft has to effect to end the dialog.

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号