用VS2005建立了一个基于对话框的程序。在OnInitDialog函数里面我加入了一个判断语句if(!语句1)
{}意思就是如果if语句1为假,就提示,然后退出程序。但是如果我在
if(!语句1)
{
AfxMessageBox("提示语句");
return FALSE;
}这样写,程序还是会执行后面的语句。启动程序。请问该怎么解决?

解决方案 »

  1.   

    那你这个就不应该加在对话框的OnInitDialog里面
    就应该加在app里面的InitInstance里面
    在这里面,这样:
    if(!语句1)
    {
    AfxMessageBox("提示语句");
    return FALSE;
    }
    else
    {
    调用对话框
    }
      

  2.   

    OnInitDialog即使return FLASE后,仅仅是阻止此函数后面的语句的执行。但是你要晓得。最后程序还是会执行起来,而且由于你略过了后面的控件初始之类的代码,有可能有些控件的句柄是空的,导致你程序报错。如果你想阻止整个程序的运行,可以在对话框类创建以前就判断。
    所有的MFC进程都是从CWinApp的派生类中出来,然后再在这个进程中生成窗口类。里面有个 virtual BOOL InitInstance();函数,就是初始化MFC实例,你可以在这里面阻断。比如
    BOOL CTestApp::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
    if(判断语句成立)
    {
    CTestDlg 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
    }
    }
    else
    {
    AfxMessageBox("条件不成立,您无法运行软件");
    return FALSE;
    }
    // Since the dialog has been closed, return FALSE so that we exit the
    //  application, rather than start the application's message pump.
    return FALSE;
    }
      

  3.   

    理论上只要条件满足就会退出了,你可以直接return false来重新验证一下这个问题了。
      

  4.   


    if(true)
    {
    AfxMessageBox("提示语句");
    PostQuitMessage(0);
    }
      

  5.   

    return FALSE;
    后应该退出程序的,调试看看执行路径
      

  6.   

    if(!语句1)
    {
        AfxMessageBox("提示语句");
        ::PostMessage(NULL,WM_QUIT,0,0);
        return FALSE;
    }
      

  7.   

    不知道你的程序是怎样的比如OnInitDialog函数中是有以下语句:
    if(!语句1)
    {
    AfxMessageBox("提示语句");
    return FALSE;
    }
    AfxMessageBox("!!!");如果AfxMessageBox("提示语句");执行了
    AfxMessageBox("!!!");这句是绝对不会执行的。
      

  8.   

    你在
    InitInstance()
    里加这个试一下.
    if(true)
    {
    AfxMessageBox("提示语句");
    return FALSE;
    }立马退出
      

  9.   


    如果是OnInitDialog中,
    就用:
    if(!false)
    {
    AfxMessageBox("提示语句");
    PostMessage(WM_CLOSE,0,0);
    return FALSE;
    }
      

  10.   

    3楼的 “OnInitDialog即使return FLASE后,仅仅是阻止此函数后面的语句的执行。但是你要晓得。最后程序还是会执行起来,而且由于你略过了后面的控件初始之类的代码,有可能有些控件的句柄是空的,导致你程序报错。 ”说的很正确。return FALSE;后面的代码都不执行了!