做了一个小对话框程序mfc
想在退出的时候显示一个确认退出的对话框 尝试截取WM_CLOSE或在CDialogApp中用虚拟函数onExitInstance() 都不成功 请教:)

解决方案 »

  1.   

    // Diary.cpp : Defines the class behaviors for the application.
    //#include "stdafx.h"
    #include "Diary.h"
    #include "DiaryDlg.h"#ifdef _DEBUG
    #define new DEBUG_NEW
    #undef THIS_FILE
    static char THIS_FILE[] = __FILE__;
    #endif/////////////////////////////////////////////////////////////////////////////
    // CDiaryAppBEGIN_MESSAGE_MAP(CDiaryApp, CWinApp)
    //{{AFX_MSG_MAP(CDiaryApp)
    //}}AFX_MSG_MAP
    ON_COMMAND(ID_HELP, CWinApp::OnHelp)
    END_MESSAGE_MAP()/////////////////////////////////////////////////////////////////////////////
    // CDiaryApp constructionCDiaryApp::CDiaryApp()
    {
    // TODO: add construction code here,
    this->is_old = FALSE;
    this->m_pos = NULL;
    // Place all significant initialization in InitInstance
    }/////////////////////////////////////////////////////////////////////////////
    // The one and only CDiaryApp objectCDiaryApp theApp;/////////////////////////////////////////////////////////////////////////////
    // CDiaryApp initializationBOOL CDiaryApp::InitInstance()
    {
    AfxEnableControlContainer(); // Standard initialization
    // If you are not using these features and wish to reduce the size
    //  of your final executable, you should remove from the following
    //  the specific initialization routines you do not need.#ifdef _AFXDLL
    Enable3dControls(); // Call this when using MFC in a shared DLL
    #else
    Enable3dControlsStatic(); // Call this when linking to MFC statically
    #endif CDiaryDlg dlg;
    m_pMainWnd = &dlg;
    int nResponse = dlg.DoModal();
    if (nResponse == IDOK)
    {
    // TODO: Place code here to handle when the dialog is
    //  dismissed with OK
    }
    else if (nResponse == IDCANCEL)
    {
    // TODO: Place code here to handle when the dialog is
    //  dismissed with Cancel
    } // Since the dialog has been closed, return FALSE so that we exit the
    //  application, rather than start the application's message pump.
    return FALSE;
    }void CDiaryApp::Serialize(CArchive& ar) 
    {
    if (ar.IsStoring())
    { // storing code
    }
    else
    { // loading code
    }
    }void CDiaryApp::OnSave()
    {
    CFile file;
    if(file.Open("Diary.dia",CFile::modeCreate|CFile::modeWrite))
    {
    CArchive ar(&file, CArchive::load);
    Serialize(ar);
    ar.Close();
    file.Close();
    }
    }int CDiaryApp::ExitInstance() 
    {
    // TODO: Add your specialized code here and/or call the base class
    ::AfxMessageBox("");
    return CWinApp::ExitInstance();
    }
      

  2.   

    你在PreTranslateMessage(MSG* pMsg)拦截
    可以成功
    BOOL CXXXDlg::PreTranslateMessage(MSG* pMsg) 
    {
    if (pMsg->message==WM_CLOSE)
    MessageBox(...);
    }
      

  3.   

    ExitInstance()函数在最后
    运行成功 我点小叉叉 程序自做主张 直接退出了 根本没有显示对话框。
      

  4.   

    截获WM_CLOSE消息。
    不过我估计你是按默认的那个确定键退出的,所以可能在OnOK()里面也要处理一下。
      

  5.   

    消息肯定收到了,不过在你ExitInstance直接return了,所以程序也就退出了,MessageBox做为子窗口,刚显示后就被删除了!
    你可以在窗口的OnClose里这么写if ( MessageBox("你确认真的要退出吗 ?","提示" ,MB_OKCANCEL)==IDOK)
        CDialog::OnClose();
      

  6.   

    同意楼上...
    要不就Sleep下,绝对行滴OnClose