用VS2005建立了一个基于对话框的程序。在OnInitDialog函数里面我加入了一个判断语句if(!语句1)
{}意思就是如果if语句1为假,就提示,然后退出程序。但是如果我在
if(!语句1)
{
AfxMessageBox("提示语句");
return FALSE;
}这样写,程序还是会执行后面的语句。启动程序。请问该怎么解决?
{}意思就是如果if语句1为假,就提示,然后退出程序。但是如果我在
if(!语句1)
{
AfxMessageBox("提示语句");
return FALSE;
}这样写,程序还是会执行后面的语句。启动程序。请问该怎么解决?
就应该加在app里面的InitInstance里面
在这里面,这样:
if(!语句1)
{
AfxMessageBox("提示语句");
return FALSE;
}
else
{
调用对话框
}
所有的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;
}
if(true)
{
AfxMessageBox("提示语句");
PostQuitMessage(0);
}
后应该退出程序的,调试看看执行路径
{
AfxMessageBox("提示语句");
::PostMessage(NULL,WM_QUIT,0,0);
return FALSE;
}
if(!语句1)
{
AfxMessageBox("提示语句");
return FALSE;
}
AfxMessageBox("!!!");如果AfxMessageBox("提示语句");执行了
AfxMessageBox("!!!");这句是绝对不会执行的。
InitInstance()
里加这个试一下.
if(true)
{
AfxMessageBox("提示语句");
return FALSE;
}立马退出
如果是OnInitDialog中,
就用:
if(!false)
{
AfxMessageBox("提示语句");
PostMessage(WM_CLOSE,0,0);
return FALSE;
}