编了一个针对特定文件的编辑器,是基于对话框的应用,模式对话框中定义了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()
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()
{
OnOK(); --------->CDialog::OnOK();
}
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.
{
OnOK();
}
你把你的OnClose()函数里的这个if 语句删除就可以了.
{
OnExitButton();
}