在单文档程序中,比如有些数据库应用程序,在程序运行时先出现一个用户登陆对话框,如果用户输入的密码正确则进入主界面,否则退出。我看了一下是在 InitInstence函数中加入了这几句:CDialog logindlg;
if(logindlg.Domodal() == IDOK)
{
}
else
{
  return FASLE; //退出程序
}我想在基于对话框的程序里也实现这个效果,但是我把那些代码放在InitInstence中,可以通过编译,但是运行就报错。请问大家怎么做,谢谢

解决方案 »

  1.   

    加在app的InitInstence中的CSingleDocTemplate* pDocTemplate;一句前面,报错可能是你程序其它地方的错误。调试一下看看。
      

  2.   

    怎么会呢?这样即可:
    CLoginDlg LoginDlg;
    if(LoginDlg.DoModal() == IDOK)
    {
    CDialogTestDlg 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
    }
    }
      

  3.   

    To: stavck(在河之洲) 是对话框程序,不是单文档,单文档no problem
      

  4.   

    假设你的程序名为Szhr,则在如下函数中这样处理(我将MFC的许多东东一起COPY下来了)
    BOOL CSzhrApp::InitInstance()
    {
    // 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 AfxOleInit();  //初始化 COM 环境 CLogDlg dlg; //你自己的登录对话框
    int nResponse = dlg.DoModal();
             m_pMainWnd = &dlg;
    if (nResponse == IDOK)
    {
    // TODO: Place code here to handle when the dialog is
    //  dismissed with OK
                    CSzhrDlg dlg; //原程序的对话框
                    dlg.DoModal();
                    ......
    }
    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;
    }
      

  5.   

    vcleaner(我没做大哥已经很久了.......)  
    你的方法也不行啊
      

  6.   

    在对话框中的的 OnInitDialog()中添加就行了..
      

  7.   

    CDialog logindlg;
    if(logindlg.Domodal() == IDOK)   //DoModal() 
    {
    }
    else
    {
      return FASLE; //退出程序
    }编译不通过是不因为上面的写错了,对于Single Document 也应该可以吧~关注!
      

  8.   

    if(logindlg.Domodal() == IDOK)   //-->DoModal()  是不是这写错了的原因
      

  9.   

    可以BOOL CCTestApp::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 CNewDlg newdlg;
    if (newdlg.DoModal() == IDCANCEL)
    {
    return FALSE;
    } CCTestDlg 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;
    }
      

  10.   

    在对话框的 OnInitDialog()中就可以啊,没问题的