我在设计一个登陆对话框弹出一个单文档视图,用google没有搜到这方面的源代码。我记得以前有人在坛子里问过这个问题,但搜不到相关的帖子。 我的初步设想是这样的:
BOOL CDlgTestApp::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 // Change the registry key under which our settings are stored.
// TODO: You should modify this string to be something appropriate
// such as the name of your company or organization.
SetRegistryKey(_T("Local AppWizard-Generated Applications")); LoadStdProfileSettings(); // Load standard INI file options (including MRU) // Register the application's document templates. Document templates
// serve as the connection between documents, frame windows and views.
CLogsys TestDlg;
if(TestDlg.DoModal()==IDOK) // 弹出登陆对话框
{
if(TestDlg.m_strPwd=="1234"||TestDlg.m_strUser=="Admin") //假如密码和用户名正确
{
CSingleDocTemplate* pDocTemplate;
pDocTemplate = new CSingleDocTemplate(
IDR_MAINFRAME,
RUNTIME_CLASS(CDlgTestDoc),
RUNTIME_CLASS(CMainFrame), // main SDI frame window
RUNTIME_CLASS(CDlgTestView));
AddDocTemplate(pDocTemplate);
// Parse command line for standard shell commands, DDE, file open
CCommandLineInfo cmdInfo;
ParseCommandLine(cmdInfo); // Dispatch commands specified on the command line
if (!ProcessShellCommand(cmdInfo))
return FALSE;
// The one and only window has been initialized, so show and update it.
m_pMainWnd->ShowWindow(SW_SHOW);
m_pMainWnd->UpdateWindow();
return TRUE;
}
else return FALSE;
}
else
return FALSE;
} 基本可以实现我的功能。但是我觉得不好。一是假如使用错误的密码和用户名登陆时应提示,并有登陆次数限制(限制在3次).但不知怎样设计? 我的改进想法是在InitInstance()里
CLogsys TestDlg;
TestDlg.DoModal(); 然后在CLogsys里添加OnOK和OnCancel函数进行控制。
比如CLogsys::OnOK()
{
if((TestDlg.m_strPwd=="1234"||TestDlg.m_strUser=="Admin")&&(m_nLogtime<3))
{
MessageBox("Error");
m_nLogtime++;
}
if(m_nLogtime>3)
{
Messagebox("登陆错误次数大于3");
CDialog::EndDialog;// 关键是这里,如何能够在退出对话框后不向下执行,即不执行单文档视图的初始化代码。
}
}
BOOL CDlgTestApp::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 // Change the registry key under which our settings are stored.
// TODO: You should modify this string to be something appropriate
// such as the name of your company or organization.
SetRegistryKey(_T("Local AppWizard-Generated Applications")); LoadStdProfileSettings(); // Load standard INI file options (including MRU) // Register the application's document templates. Document templates
// serve as the connection between documents, frame windows and views.
CLogsys TestDlg;
if(TestDlg.DoModal()==IDOK) // 弹出登陆对话框
{
if(TestDlg.m_strPwd=="1234"||TestDlg.m_strUser=="Admin") //假如密码和用户名正确
{
CSingleDocTemplate* pDocTemplate;
pDocTemplate = new CSingleDocTemplate(
IDR_MAINFRAME,
RUNTIME_CLASS(CDlgTestDoc),
RUNTIME_CLASS(CMainFrame), // main SDI frame window
RUNTIME_CLASS(CDlgTestView));
AddDocTemplate(pDocTemplate);
// Parse command line for standard shell commands, DDE, file open
CCommandLineInfo cmdInfo;
ParseCommandLine(cmdInfo); // Dispatch commands specified on the command line
if (!ProcessShellCommand(cmdInfo))
return FALSE;
// The one and only window has been initialized, so show and update it.
m_pMainWnd->ShowWindow(SW_SHOW);
m_pMainWnd->UpdateWindow();
return TRUE;
}
else return FALSE;
}
else
return FALSE;
} 基本可以实现我的功能。但是我觉得不好。一是假如使用错误的密码和用户名登陆时应提示,并有登陆次数限制(限制在3次).但不知怎样设计? 我的改进想法是在InitInstance()里
CLogsys TestDlg;
TestDlg.DoModal(); 然后在CLogsys里添加OnOK和OnCancel函数进行控制。
比如CLogsys::OnOK()
{
if((TestDlg.m_strPwd=="1234"||TestDlg.m_strUser=="Admin")&&(m_nLogtime<3))
{
MessageBox("Error");
m_nLogtime++;
}
if(m_nLogtime>3)
{
Messagebox("登陆错误次数大于3");
CDialog::EndDialog;// 关键是这里,如何能够在退出对话框后不向下执行,即不执行单文档视图的初始化代码。
}
}
然后在InitInstance()中,根据对话框返回的值做相应的处理就得了
if(IDOK == dlg.DoModal())
{
//OK
}
else if(IDCANCEL == dlg.DoModal())
{
//FAILED
}
我的代码是这样的,在InitInstance()中函数里:CLogsys TestDlg;
if(TestDlg.DoModal()==IDOK) // 弹出登陆对话框
{
CSingleDocTemplate* pDocTemplate;
pDocTemplate = new CSingleDocTemplate(
IDR_MAINFRAME,
RUNTIME_CLASS(CDlgTestDoc),
RUNTIME_CLASS(CMainFrame), // main SDI frame window
RUNTIME_CLASS(CDlgTestView));
AddDocTemplate(pDocTemplate);
// Parse command line for standard shell commands, DDE, file open
CCommandLineInfo cmdInfo;
ParseCommandLine(cmdInfo); // Dispatch commands specified on the command line
if (!ProcessShellCommand(cmdInfo))
return FALSE;
// The one and only window has been initialized, so show and update it.
m_pMainWnd->ShowWindow(SW_SHOW);
m_pMainWnd->UpdateWindow();
return TRUE;
}
else if (IDCANCEL == TestDlg.DoModal())
{
return FALSE;
}void CLogsys::OnOK()
{
// TODO: Add extra validation here
if((m_strUser!="Admin"||m_strPwd!="1234")&&(m_Time<3)) //假如密码和用户名正确
{
AfxMessageBox("用户名或密码不正确");
m_Time++;
}
if(m_Time>2)
{
AfxMessageBox("登陆错误次数超过3次");
CDialog::OnCancel();
}
}void CLogsys::OnCancel()
{
// TODO: Add extra cleanup here
CDialog::OnCancel();
}
}
{
return TRUE;
}
else if(IDCANCEL == dlg.DoModal())
{
return FALSE;
}
{
return TRUE;
}
else if(IDCANCEL == dlg.DoModal())
{
return FALSE;
} 这样做可以吗?那在哪儿生成初始化单文档视图呢?
解决方法见http://blog.csdn.net/clever101/archive/2006/06/01/768515.aspx