编了一个针对特定文件的编辑器,是基于对话框的应用,模式对话框中定义了EXIT按钮,程序按EXIT按钮弹出信息框“exit? don't forget save your data!",点击OK后能正常退出程序,但是点对话框的右上”X“退出按钮,弹出“exit? don't forget save your data!",点击OK只是信息框消息,对话框不能关闭。代码如下:
void CEditorDlg::OnExitButton() 
{
// TODO: Add your control notification handler code here if(IDYES==AfxMessageBox("exit? don't forget save your data!",MB_YESNO,0))
{
OnOK();
}
}
把OnOK()改为PostMessage(WM_CLOSE,0,0)或者改为OnClose()也不行
其中程序中的消息映射如下:
BEGIN_MESSAGE_MAP(CEditorDlg, CDialog)
//{{AFX_MSG_MAP(CEditorDlg)
ON_WM_PAINT()
ON_WM_QUERYDRAGICON()
ON_WM_CREATE()
         ON_WM_CLOSE()
ON_BN_CLICKED(IDC_OPEN_BUTTON, OnOpenButton)
ON_BN_CLICKED(IDC_SAVE_BUTTON, OnSaveButton)
ON_BN_CLICKED(IDC_SHOW_MODE_BUTTON, OnShowModeButton)
ON_BN_CLICKED(IDC_HELP_BUTTON, OnHelpButton)
ON_BN_CLICKED(IDC_EXIT_BUTTON, OnExitButton)
//}}AFX_MSG_MAP
END_MESSAGE_MAP()

解决方案 »

  1.   

    if(IDYES==AfxMessageBox("exit? don't forget save your data!",MB_YESNO,0))
    {
    OnOK();   --------->CDialog::OnOK();
    }
      

  2.   

    从你上面的MESSAGE_MAP()宏里看,你应该是重写了WM_CLOSE消息啊!看看CYourDlg::OnClose函数里的代码是什么样的,应该是那里的问题。
      

  3.   

    记住:模式对话框的关闭要用 EndDialog() 函数关闭要不然对话框的资源不会被释放 ,浪费内存空间详见MSDN:   CDialog::EndDialog
    void EndDialog( int nResult );ParametersnResultContains the value to be returned from the dialog box to the caller of DoModal.ResCall this member function to terminate a modal dialog box. This member function returns nResult as the return value of DoModal. You must use the EndDialog function to complete processing whenever a modal dialog box is created.You can call EndDialog at any time, even in OnInitDialog, in which case you should close the dialog box before it is shown or before the input focus is set.EndDialog does not close the dialog box immediately. Instead, it sets a flag that directs the dialog box to close as soon as the current message handler returns.
      

  4.   

    if(IDYES==AfxMessageBox("exit? don't forget save your data!",MB_YESNO,0))
    {
    OnOK();   
    }
    你把你的OnClose()函数里的这个if 语句删除就可以了.
      

  5.   

    最好把这个OnClose()函数删除了!!!!!!!!!!
      

  6.   

    在对话框里面最好是重载OnOK和OnCancel两个函数来完成这些功能
      

  7.   

    OnClose()可以这样直接调用exit按钮.
    {
       OnExitButton();
    }